How to edit a Partial Class?

Asked

Viewed 170 times

1

I took a project from a client, and there’s a class that’s on a partial. When I click to go in the reference opens the file, but in the project I can’t find the file, and has the following text in the class file:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

I never used partial class, how it works and how to edit?

  • You want to know the concept of partial class or wondering if you can edit the file?

  • would like to edit as I am not finding the file, what would be that auto generated.

2 answers

4


Definition

Partial class is just a way to separate two parts of the code of a class into separate files. It has no secret or special semantics. It is essentially a way to facilitate code organization and indicate to the compiler that the two codes available in the two files with a class of the same name is not a duplication error but one code is complementary to the other and should be compiled as if it were one thing only.

Use

In general this type of artifice should only be used by code generators, so a part is generated in a file and no one should touch this file. In another file have the parts of class code that the programmer can mess with without causing problems.

Of course nothing prevents the programmer from touching the generated part, but it shouldn’t. It is an organizational protection only to make clear which parts can move and which parts can not, but does not protect in fact.

If you are going to create a class that needs to be partial, just use the modifier partial before class To indicate that there will be another part, do this in the two files that have the two parts, you need nothing else. But remember that you should only use this if you create a generator and want a part not to be modified.

Partial Method

The above documentation refers to partial method which is a method that only has the declaration without a body. In this case the body will be in the other file. It is a way to ensure that the method signature is not changed in the other file. If it is changed, the compiler will indicate problems.

Do not mess with generated code

So the file you want to move shouldn’t be moved.

Where to find the files

The files usually get together in the project, if not in the same folder, in some other related. Try to check through Windows Explorer all the files available in the project. The file name will probably make some mention of partial or designer or at least have a name similar to the file you want to move and can’t find. If you find it in Windows Explorer, it can be easier to locate it in VS.

See in VS if the files are not grouped in the project. Click on the plus sign [+] to open the related files. Depending on the summer it may be a triangle or other symbol, but it is the one that opens the file tree.

But I will stress, do not edit this part unless you know a lot what you are doing and know all the implications, which should not be the case.

3

Maniero explained very well - that a partial class is only one class divided into multiple files.

Example:

auto/Pessoa.Cs

namepsace App.Gente {
  public partial class Pessoa {
      public Pessoa() {
      }
  }
}

manual/Person.Cs

namespace App.Gente {
  public partial class Pessoa {
     //não pode ter construtor aqui, pois já está definido no outro arquivo
     //porém pode ter tantas quantas funções/métodos que queira

     public void FazAlgo() {

     }
  }
}

Main.Cs

public static class Main {
   Pessoa p = new Pessoa(); //chama o consturtor em auto/Pessoa.cs
   p.FazAlgo(); //chama o método definido em manual/Pessoa.cs
}

Obs: The partial class should be in same namespace, independent of the physical file location. If in another namespace, is another class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.