CodeDomSerializer输出排序

时间:2012-04-19 22:04:05

标签: c# custom-controls designer

我已经创建了一个组件来为我们的项目做一些本地化的东西。我遇到的问题是我输出的代码被注入到InitializeComponent中发生得太晚了。

示例

this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ucHome));
this.localizationResourceManager1 = new Compass0.Localization.LocalizationResourceManager(this.components);
//Other designer code to create controls (this is all in the first block of code in InitializeComponent 
Compass0.Localization.XMLResourceManager.Create(typeof(ucViewHome), ref resources);  //created by my code serializier
//
// btnLogin
//

等等

但是设计师正在发生的事情是

this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ucHome));
this.btnLogin = new ComponentFactory.Krypton.Toolkit.KryptonButton();
this.localizationResourceManager1 = new Compass0.Localization.LocalizationResourceManager(this.components);
//Other designer code to create controls (this is all in the first block of code in InitializeComponent 
//
// btnLogin
//
this. btnLogin.Name = "btnLogin"; 
this. btnLogin.Text = resources.GetString("btnLogin.Text");
Compass0.Localization.XMLResourceManager.Create(typeof(ucViewHome), ref resources);  //created by my code serializier

我基本上需要强制设计者在资源初始化后将我的代码放在第3行。有没有办法我可以直截了当地做到这一点?我的代码生成类如下。

public override object Deserialize( IDesignerSerializationManager manager, object codeDomObject )
    {
        CodeDomSerializer baseSerializer = ( CodeDomSerializer )manager.GetSerializer( typeof( LocalizationResourceManager ).BaseType, typeof( CodeDomSerializer ) );

        return baseSerializer.Deserialize( manager, codeDomObject );
    }

    public override object Serialize( IDesignerSerializationManager manager, object value )
    {
        CodeDomSerializer baseSerializer = ( CodeDomSerializer )manager.GetSerializer( typeof( LocalizationResourceManager ).
            BaseType, typeof( CodeDomSerializer ) );

        object codeObject = baseSerializer.Serialize( manager, value );

        if ( codeObject is CodeStatementCollection )
        {
            CodeStatementCollection statements = ( CodeStatementCollection )codeObject;

            CodeTypeDeclaration classTypeDeclaration = ( CodeTypeDeclaration )manager.GetService(typeof( CodeTypeDeclaration ) );

            CodeExpression typeofExpression = new CodeTypeOfExpression( classTypeDeclaration.Name );

            CodeDirectionExpression resourceRef = new CodeDirectionExpression( FieldDirection.Ref, new CodeVariableReferenceExpression( "resources" ) );

            CodeExpression ResourceManagerAssignment = new CodeMethodInvokeExpression(
                new CodeTypeReferenceExpression( typeof(XMLResourceManager).ToString() ),
                "Create", new CodeExpression[] { typeofExpression, resourceRef } );


            statements.Insert( 0, new CodeExpressionStatement( ResourceManagerAssignment ) );               
        }

        return codeObject;
    }

1 个答案:

答案 0 :(得分:3)

我知道这已经很晚了,但对于那些偶然发现这个问题的人来说......

我正在做同样的事情,解决这个问题我只是通过创建一个继承自ComponentDesigner的类来确保我的本地化控制首先被创建/序列化,该类使用获取控件列表的Initialize方法,删除自身并重新创建 - 添加到列表的顶部。然后我将Designer属性添加到我的自定义组件。

它在VB中,如果有人需要它翻译成C#,请告诉我......

<Designer(GetType(LocalizerDesigner))>
<DesignerSerializer(GetType(LocalizerSerializer), GetType(CodeDomSerializer))>
Public Class Localizer
    Inherits Component

    Public Sub GetResourceManager(type As Type, ByRef manager As ComponentResourceManager)
        ' Replace resource manager w/ new one.
    End Sub

End Class

Public Class LocalizerSerializer
    Inherits CodeDomSerializer

    Public Overrides Function Deserialize(manager As IDesignerSerializationManager, codeObject As Object) As Object
        Dim baseSerializer As CodeDomSerializer = DirectCast(manager.GetSerializer(GetType(Component), GetType(CodeDomSerializer)), CodeDomSerializer)
        Return baseSerializer.Deserialize(manager, codeObject)
    End Function

    Public Overrides Function Serialize(manager As IDesignerSerializationManager, value As Object) As Object
        Dim baseSerializer As CodeDomSerializer = DirectCast(manager.GetSerializer(GetType(Component), GetType(CodeDomSerializer)), CodeDomSerializer)

        Dim codeObject As Object = baseSerializer.Serialize(manager, value)

        Dim statementCollection As CodeStatementCollection = TryCast(codeObject, CodeStatementCollection)

        If statementCollection IsNot Nothing Then
            Dim formClassTypeDeclaration As CodeTypeDeclaration = DirectCast(manager.GetService(GetType(CodeTypeDeclaration)), CodeTypeDeclaration)
            Dim typeofFormExpression As New CodeTypeOfExpression(formClassTypeDeclaration.Name)

            Dim outResourcesExpression As New CodeDirectionExpression(FieldDirection.Out, New CodeVariableReferenceExpression("resources"))
            Dim getResourceManagerExpression As New CodeMethodInvokeExpression(MyBase.SerializeToExpression(manager, value), "GetResourceManager",
                                                                     {typeofFormExpression, outResourcesExpression})

            statementCollection.Add(New CodeExpressionStatement(getResourceManagerExpression))
        End If
        Return codeObject
    End Function
End Class

Public Class LocalizerDesigner
    Inherits ComponentDesigner

    Public Overrides Sub Initialize(component As IComponent)
        MyBase.Initialize(component)
        Dim designerHost As IDesignerHost = TryCast(GetService(GetType(IDesignerHost)), IDesignerHost)
        If designerHost Is Nothing Then
            Return
        End If

        Dim innerListProperty As Reflection.PropertyInfo = designerHost.Container.Components.GetType().GetProperty("InnerList", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.FlattenHierarchy)
        Dim innerList As ArrayList = TryCast(innerListProperty.GetValue(designerHost.Container.Components, Nothing), ArrayList)

        If innerList Is Nothing OrElse innerList.IndexOf(component) <= 1 Then
            Return
        End If

        innerList.Remove(component)
        innerList.Insert(0, component)
    End Sub
End Class