2011-04-30 116 views
1

任何人都可以指向我的解決方案以下? 我想通過使用CustomAttributeBuilder複製使用對象初始化器的屬性屬性;C#對象初始化程序和構造函數

即。

[Display(Order = 0, Name = "UserNameLabel", ResourceType = typeof(RegistrationDataResources))] 

爲..

//Add Display Attribute 
ConstructorInfo displayCtor = typeof(DisplayAttribute).GetConstructor(new Type[] { /* something here? */ }); 
CustomAttributeBuilder displayAttrib = new CustomAttributeBuilder(displayCtor, new object[] { /*..*/}); 
propertyBuilder.SetCustomAttribute(displayAttrib); 

回答

1

看來你需要使用CustomAttributeBuilder的構造函數,它允許您指定的屬性和它們的值。你試過這個嗎?

 ConstructorInfo displayCtor = typeof(TestAttribute).GetConstructor(new Type[] {}); 
     PropertyInfo conProperty = typeof (TestAttribute).GetProperty("TestProperty"); 
     CustomAttributeBuilder displayAttrib = new CustomAttributeBuilder(displayCtor, new object[] {}, new[] {conProperty}, new object[] {"Hello"}); 

上面的代碼也適用於:

[Test(TestProperty = "Hello")] 

還要注意的是,在你的榜樣你的屬性 「Display」 不匹配的構造函數 「DataValidationAttribute

編輯:

完整樣本:

using System; 
using System.Reflection; 
using System.Reflection.Emit; 
using System.Threading; 

namespace SO5841769 
{ 

    class TestAttribute : Attribute 
    { 
     public string TestProperty { get; set; } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      AppDomain myDomain = Thread.GetDomain(); 
      AssemblyName myAsmName = new AssemblyName(); 
      myAsmName.Name = "MyDynamicAssembly"; 
      AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave); 
      ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule(myAsmName.Name, myAsmName.Name + ".dll"); 
      TypeBuilder myTypeBuilder = myModBuilder.DefineType("Data", TypeAttributes.Public); 
      FieldBuilder someFieldBuilder = myTypeBuilder.DefineField("someField", typeof(string), FieldAttributes.Private); 
      PropertyBuilder somePropertyBuilder = myTypeBuilder.DefineProperty("SomeProperty", PropertyAttributes.HasDefault, typeof(string), null); 
      MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; 
      ConstructorInfo displayCtor = typeof(TestAttribute).GetConstructor(new Type[] { }); 
      PropertyInfo conProperty = typeof (TestAttribute).GetProperty("TestProperty"); 
      CustomAttributeBuilder displayAttrib = new CustomAttributeBuilder(displayCtor, new object[] {}, new[] {conProperty}, new object[] {"Hello"}); 
      somePropertyBuilder.SetCustomAttribute(displayAttrib); 
      MethodBuilder somePropertyGetPropMthdBldr = myTypeBuilder.DefineMethod("get_SomeProperty", getSetAttr, typeof(string), Type.EmptyTypes); 
      ILGenerator somePropertyGetIL = somePropertyGetPropMthdBldr.GetILGenerator(); 
      somePropertyGetIL.Emit(OpCodes.Ldarg_0); 
      somePropertyGetIL.Emit(OpCodes.Ldfld, someFieldBuilder); 
      somePropertyGetIL.Emit(OpCodes.Ret); 
      somePropertyBuilder.SetGetMethod(somePropertyGetPropMthdBldr); 
      myTypeBuilder.CreateType(); 
      myAsmBuilder.Save(myAsmName.Name + ".dll"); 

     } 
    } 
} 
+0

>另請注意,在您的示例中,您的屬性「顯示」與構造函數 不匹配是的,抱歉,複製並粘貼併發布清理錯誤。 [編輯上面] – Sam 2011-04-30 13:45:25

+0

完美地工作。非常感謝。 – Sam 2011-04-30 13:45:44