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

后端 未结 1 719
闹比i
闹比i 2021-02-20 16:00

I\'m trying to customize the Windows Forms Designer\'s code generation for InitializeComponent. The MSDN article \"Customizing Code Generation in the .NET Framework

1条回答
  •  我在风中等你
    2021-02-20 16:46

    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;
    }
    

    0 讨论(0)
提交回复
热议问题