开发者

C# - How does the compiler find the another part of the partial class?

开发者 https://www.devze.com 2023-03-08 02:40 出处:网络
I am using VS2010 SP1. After creating a demo proj开发者_如何学编程ect for C# Form application, my solution structure has the following information

I am using VS2010 SP1.

After creating a demo proj开发者_如何学编程ect for C# Form application, my solution structure has the following information

Form1.cs
   Form1.Designer.cs
   Form1.resx

// File Form1.cs
namespace WinFormApp
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
  }
}

// File Form1.Designer.cs
namespace WinFormApp
{
  partial class Form1
  {

    private void InitializeComponent()
    {
      ...
    }
    ...
   }
}

Here is the question, I can see the Form1 is a partial class. However, how does the compiler know where to find the another part of it? *In other words, is there a binding between the file Form1.cs and Form1.Designer.cs?* Here, as you can see, the other part is defined inside Form1.Designer.cs. I assume there are hints out there so that the compiler can quickly find all implementation code for a partial class. Please correct me.

Thank you


The C# compiler is a "two pass" compiler. The project system gives the compiler a list of all the files in the project. The compiler first does a "top level" scan of all the files, and builds up a table of which files contain which namespaces, types, methods, properties, events, fields, and so on, but it does not look at method bodies.

When that table is built up we can use it to do "top level" checking of the entire program. We figure out at that time which partial declarations are really "the same class". During this stage we resolve partialness, we resolve base types and constraints on generic parameters, we make sure that all "override" methods actually override a matching virtual method, and so on.

Once the "top level" analysis is done, only then do we analyze the method bodies.

The file names are irrelevant; the compiler only uses them for error reporting. It is perfectly legal to have a dozen classes and a hundred files and a partial declaration of each class in each file. (Though please do not actually do that; it increases the burden tremendously on the IDE when people do stuff like that because it becomes difficult for us to figure out quickly what all the members of a class are while you are typing in changes!)


All files are sent to the compiler, there is no magic here.


There's no requirement in terms of the names of the files. You could have Foo.cs and Bar.cs. (And each of those could contain multiple different partial classes.) The compiler is given all the files to compile for a particular assembly in one go - and it only looks through those files. It doesn't go scouring the file system for other source files, or anything like that.


When C# compiler gets all files,it scans through all the files.If it finds classes having same name and have partial modifier,then it recognizes them as same class and assemble them and compile them.

0

精彩评论

暂无评论...
验证码 换一张
取 消