2011-04-30 3 views
1

누구든지 다음을위한 해결책을 제시 할 수 있습니까? CustomAttributeBuilder를 사용하여 개체 이니셜 라이저를 사용하는 속성 특성을 복제하려고합니다.C# 개체 이니셜 라이저 및 ConstructorInfo

즉.

[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의 생성자를 사용해야 할 것으로 보인다. 이거 해봤 니?

:

[Test(TestProperty = "Hello")] 

는 또한 귀하의 예제에서 속성이 "Display"생성자 "DataValidationAttribute"

편집과 일치하지 않습니다 : 대한

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

위의 코드는 간다

전체 샘플 :

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

+0

완벽하게 작동합니다. 대단히 감사합니다. – Sam

관련 문제