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



我正在尝试为InitializeComponent自定义Windows窗体设计器的代码生成。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;
    }
}

现在我把它连接到我的表格SomeForm

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

然后,窗体设计器可能会生成以下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`) */ 
}

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

如果我希望能够修改整个方法的生成的 CodeDOM,而不仅仅是处理特定对象的部分,我该怎么办?

背景:我为什么要这样做?在 Windows 窗体中,如果要在数据绑定期间进行灵活的值转换,通常必须求助于订阅某些特定Binding对象的FormatParse事件。所以我正在创建一个专门的Binding子类(我们称之为ConvertingBinding),它稍微简化了这个过程。

现在,问题是,在Windows窗体设计器中设置数据绑定时,生成的代码会创建Binding的实例;但是,我希望设计器实例化我的专用子类。我目前的方法是让设计器先创建一个 CodeDOM 树,然后遍历该树,并将Binding的所有实例替换为 ConvertingBinding 的实例化。

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

以下是一些简单的步骤。
包括库:

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

创建新表单并添加DesignerSerializerAttribute

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

创建CustomForm子体并向其添加一些控件或组件:

class CustomForm1 : CustomForm { … }

将方法添加到CustomFormSerializer以处理CodeStatementCollection,例如:

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

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

最新更新