如何自定义InitializeComponent的代码生成?更具体地说,如何对所有生成的代码进行后处理?

sta*_*ica 10 c# serialization initializecomponent designer winforms

我正在尝试自定义Windows窗体设计器的代码生成InitializeComponent.MSDN文章"在.NET Framework可视化设计器中自定义代码生成"包含"控制代码生成"部分,该部分解释了如何执行此操作的基础知识.

我在上面的文章中密切关注了一个例子:

//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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我把它连接到我的表单SomeForm:

[DesignerSerializer(typeof(SomeFormSerializer), typeof(CodeDomSerializer))]
class SomeForm : Form { … }
Run Code Online (Sandbox Code Playgroud)

然后,Forms Designer可能会生成以下InitializeComponent代码:

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`) */ 
}
Run Code Online (Sandbox Code Playgroud)

请注意,注释// CODEDOM WAS HERE未添加为第一行InitializeComponent,而是仅作为处理表单对象本身属性的代码块的第一行.

我会怎么做,如果我想成为能够改变整个方法的产生CodeDOM的,而不仅仅是与特定对象交易的一部分?

背景:为什么我要这样做?在Windows窗体,如果一个数据时要灵活值换算结合,人们通常不得不诉诸订阅的FormatParse一些特殊的事件Binding对象.所以我正在创建一个专门的Binding子类(让我们称之为ConvertingBinding),这简化了这个过程.

现在,问题是当在Windows窗体设计器中设置数据绑定时,生成的代码会创建实例Binding; 但是,我希望设计者实例化我的专用子类.我目前的做法是,让设计人员首先创建一个CodeDOM的树,然后走那棵树和替换的所有实例Binding通过的实例化ConvertingBinding.

nem*_*Bu4 12

你需要创建两个Form类.首先Form是一个DesignerSerializerAttribute.第二个Form是第一个的后代.之后,您可以自定义InitializeComponent()第二个Form及其控件或组件.为此,您应该使用manager.Context来获取所有StatementContextCodeStatementCollection包含的代码序列化对象Form的控制.

这是一些简单的步骤.
包括库:

using System.CodeDom;
using System.ComponentModel.Design.Serialization;
using System.Collections;
Run Code Online (Sandbox Code Playgroud)

创建新表单并添加DesignerSerializerAttribute:

[DesignerSerializer(typeof(CustomFormSerializer), typeof(CodeDomSerializer))]
class CustomForm : Form { … }
Run Code Online (Sandbox Code Playgroud)

创建CustomForm后代并向其添加一些控件或组件:

class CustomForm1 : CustomForm { … }
Run Code Online (Sandbox Code Playgroud)

添加方法以CustomFormSerializer进行处理CodeStatementCollection,例如:

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

Serialize方法使用循环中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;
}
Run Code Online (Sandbox Code Playgroud)