How does an instance of Winforms Designer instantiate a form?

I am developing my own WinForms designer. It should be able to upload existing custom forms. One of the problems I click on is forms without a default ctor: My code is currently creating a form before loading it into the constructor, which requires ctor by default.

OTOH, VS2008 is capable of loading such forms. I believe that this does not actually create an instance of my form (as noted in this question ): Even standard ctors do not execute. And that really does not execute InitializeComponent (). I simply added a message to this function and did not show it.

It seems to dynamically mimic a custom form type and execute only parts of the code in the InitializeComponent, which, in his opinion, matters.

Does anyone know where I can find more information on how the VS constructor works.

TIA.

Note: I found this question without satisfying answers

EDIT: More info: Steve points me to CodeDom, which is very interesting. My problem is that the types that I need to load into my designer are already compiled instead of being available as source code. I cannot find a way to apply CodeDom deserialization to compiled code.

+4
source share
2 answers

Found here here :

When you open a new Windows Application Project in VS, you see an empty Form1 in the View design. Now you have not created a project yet, since the designer is able to create an instance of Form1 and show it? Well, the designer is not really creating Form1 at all. This creates an instance of the base class of form 1, that is, System.Windows.Forms.Form. With a basic knowledge of object-oriented programming, you will find that it makes sense intuitively. When you are designing Form1, you start with the base class, form, and customize it. This is exactly what a designer helps you do.

Now let's say you added a bunch of controls to the form and closes the designer. When you open the designer again, the controls are still there. However, the form of the base class does not have these controls on it, so if the designer does not start the constructor of Form1, how was the control shown? The designer does this by deserializing the code in the InitializeComponent. Each language that designer support has a CodeDomProvider, which is responsible for providing an analyzer that analyzes the code in the InitializeComponent and creates a CodeDom view of It. The constructor then calls a set of CodeDomSerializers to deserialize this into actual controls (or, more broadly, Components) that it can add to development time. Now I have a sparkle over a lot of details in this description, but here that the Form1 and InitializeComponent constructor is never called. Instead, the designer analyzes the claims in the InitializeComponent to find out what controls to instantiate and add to the form.


The above describes how a Windows Forms designer in Visual Studio loads a form. If what you are looking for is a way to create an instance of a form that does not have a default constructor and still have access to the contained components / controls, I don’t know about the solution. The only method I know about this circumvents the lack of a default constructor , FormatterServices.GetUninitializedObject , but beware ...

Since a new instance of the object is initialized to zero, and no constructors are executed, the object may not represent a state that is considered to be valid for this object.

I also have an application that requires creating instances of compiled forms, but always used Activator.CreateInstance and required other developers to include at least a private default constructor if they wanted their form to be available in my application. Since we own the entire code base and everyone knows about the requirement, this is not a problem and is well suited for us.

+12
source

As an addition to Steve, answer, if you add a new version of Windows to the project, but make it abstract, you can still open it in the designer. However, if you add another form and get it from the first (abstract) form, you will receive an error message when you try to open the form in the constructor.

0
source

All Articles