How can I customize the code generation of InitializeComponent? More specifically, how can I post-process all of the generated code?

廉价感情. 提交于 2019-12-05 17:13:48

问题


I'm trying to customize the Windows Forms Designer's code generation for InitializeComponent. The MSDN article "Customizing Code Generation in the .NET Framework Visual Designers" contains a section "Controlling Code Generation" that explains the basics of how this can be done.

I've closely followed an example in the above article:

//using System.ComponentModel.Design.Serialization;

class SomeFormSerializer : CodeDomSerializer
{
    public override object Serialize(IDesignerSerializationManager manager,
                                     object value)
    {
        // first, let the default serializer do its work:
        var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
                             typeof(Form).BaseType, typeof(CodeDomSerializer));
        object codeObject = baseSerializer.Serialize(manager, value);

        // then, modify the generated CodeDOM -- add a comment as the 1st line:
        if (codeObject is CodeStatementCollection)
        {
            var statements = (CodeStatementCollection)codeObject;
            statements.Insert(0, new CodeCommentStatement("CODEDOM WAS HERE"));
        }

        // finally, return the modified CodeDOM:
        return codeObject;
    }
}

Now I hook this up to my form SomeForm:

[DesignerSerializer(typeof(SomeFormSerializer), typeof(CodeDomSerializer))]
class SomeForm : Form { … }

The Forms Designer might then generate the following InitializeComponent code:

private void InitializeComponent()
{
    … /* (general setup code, such as a call to `this.SuspendLayout`) */ 

    // 
    // someButton
    // 
    … /* (someButton's properties are set) */

    // CODEDOM WAS HERE!
    // 
    // SomeForm
    // 
    … /* (form's properties are set) */

    … /* (general setup code, such as a call to `this.ResumeLayout`) */ 
}

Note that the comment // CODEDOM WAS HERE was not added as the very first line in InitializeComponent, but only as the first line of the code block that deals with the properties of the form object itself.

What would I have to do if I wanted to be able to modify the generated CodeDOM of the whole method, and not just of the part that deals with a specific object?

Background: Why do I want to do this? In Windows Forms, if one wants flexible value conversion during data binding, one usually has to resort to subscribing to the Format and Parse events of some particular Binding object. So I'm creating a specialized Binding subclass (let's call it ConvertingBinding) that simplifies this process a bit.

Now, the issue is that when data bindings are set up in the Windows Forms Designer, the generated code creates instances of Binding; however, I would want the designer to instantiate my specialized subclass instead. My current approach is to let the designer create a CodeDOM tree first, then walk that tree and replace all instantiations of Binding by instantiations of ConvertingBinding.


回答1:


You need to create two Form class. First Form with a DesignerSerializerAttribute. Second Form is descendant from first. After that you can customize InitializeComponent() for second Form and it's controls or components. For this you should use manager.Context to get all StatementContext and CodeStatementCollection objects that contains serialized code of Form's controls.

Here is some simple steps.
Include libraries:

using System.CodeDom;
using System.ComponentModel.Design.Serialization;
using System.Collections;

Create new form and add DesignerSerializerAttribute:

[DesignerSerializer(typeof(CustomFormSerializer), typeof(CodeDomSerializer))]
class CustomForm : Form { … }

Create CustomForm descendant and add some controls or components to it:

class CustomForm1 : CustomForm { … }

Add method to CustomFormSerializer for processing CodeStatementCollection, for example:

private void DoSomethingWith(CodeStatementCollection statements)
{
    statements.Insert(0, new CodeCommentStatement("CODEDOM WAS HERE"));
}

In Serialize method use cycle through manager.Context:

public override object Serialize(IDesignerSerializationManager manager,
    object value)
{
    //Cycle through manager.Context            
    for (int iIndex = 0; manager.Context[iIndex] != null; iIndex++)
    {
        object context = manager.Context[iIndex];

        if (context is StatementContext)
        // Get CodeStatementCollection objects from StatementContext
        {
            ObjectStatementCollection objectStatementCollection =
                ((StatementContext)context).StatementCollection;

            // Get each entry in collection.
            foreach (DictionaryEntry dictionaryEntry in objectStatementCollection)
                // dictionaryEntry.Key is control or component contained in CustomForm descendant class
                // dictionartEntry.Value is CodeDOM for this control or component
                if (dictionaryEntry.Value is CodeStatementCollection)
                    DoSomethingWith((CodeStatementCollection)dictionaryEntry.Value);
        }

        //Do something with each collection in manager.Context:
        if (context is CodeStatementCollection)
            DoSomethingWith((CodeStatementCollection)context);
    }

    // Let the default serializer do its work:
    CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.
        GetSerializer(value.GetType().BaseType, typeof(CodeDomSerializer));
    object codeObject = baseClassSerializer.Serialize(manager, value);

    // Then, modify the generated CodeDOM:
    if (codeObject is CodeStatementCollection)
        DoSomethingWith((CodeStatementCollection)codeObject);

    // Finally, return the modified CodeDOM:
    return codeObject;
}


来源:https://stackoverflow.com/questions/10272123/how-can-i-customize-the-code-generation-of-initializecomponent-more-specificall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!