简体   繁体   English

通过TypeBuilder添加DisplayAttribute

[英]Add DisplayAttribute through TypeBuilder

Please help me adding in DisplayAttribute for an propery I tried but getting consturtor error as null 请帮助我在DisplayAttribute中添加我尝试过的属性,但构造函数错误为null

            var fieldName = string.Format("Prop{0}", fieldName);
            FieldBuilder fieldBuilder = segmentBuilder.DefineField(fieldName, typeof(string), FieldAttributes.Private);
            PropertyBuilder propertyBuilder = segmentBuilder.DefineProperty(fieldName, PropertyAttributes.HasDefault, typeof(string), null);
            MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

            MethodBuilder getPropertyMethodBuilder = segmentBuilder.DefineMethod("get_" + fieldName, getSetAttr, typeof(string), Type.EmptyTypes);
            ILGenerator ilSetGenerator = getPropertyMethodBuilder.GetILGenerator();
            ilSetGenerator.Emit(OpCodes.Ldarg_0);
            ilSetGenerator.Emit(OpCodes.Ldfld, fieldBuilder);
            ilSetGenerator.Emit(OpCodes.Ret);                

            MethodBuilder setPropertyMethodBuilder = segmentBuilder.DefineMethod("set_" + fieldName, getSetAttr, null, new Type[] { typeof(string) });
            ILGenerator ilGetGenerator = setPropertyMethodBuilder.GetILGenerator();
            ilGetGenerator.Emit(OpCodes.Ldarg_0);
            ilGetGenerator.Emit(OpCodes.Ldarg_1);
            ilGetGenerator.Emit(OpCodes.Stfld, fieldBuilder);
            ilGetGenerator.Emit(OpCodes.Ret);

            propertyBuilder.SetGetMethod(getPropertyMethodBuilder);
            propertyBuilder.SetSetMethod(setPropertyMethodBuilder);

Actually I am expecting a class like the below with attributes on property 实际上,我期望这样的类具有以下属性

public class ModelClass
{
    [Display(Name = "Propery Name A")]
    public string ProperyA { get; set; }

    [Display(Name = "Propery Name B")]
    public string ProperyB { get; set; }
}

Use CustomAttributeBuilder to build your attribute, and SetCustomAttribute method to apply it to properties: 使用CustomAttributeBuilder构建属性,并使用SetCustomAttribute方法将其应用于属性:

Type[] constructorParameters = new Type[] { typeof(string)};
ConstructorInfo constructorInfo =   typeof(DisplayNameAttribute).GetConstructor(constructorParameters);
CustomAttributeBuilder displayNameAttributeBuilder = new CustomAttributeBuilder(constructorInfo, new object[] { "Property Name A"});

propertyBuilder .SetCustomAttribute(displayNameAttributeBuilder);

if you want to set other attribute properties, you need to set properties and values using another constructor of CustomAttributeBuilder: 如果要设置其他属性属性,则需要使用另一个CustomAttributeBuilder构造函数设置属性和值:

Type[] constructorParameters = new Type[0];
ConstructorInfo constructorInfo = typeof(DisplayAttribute).GetConstructor(constructorParameters);

PropertyInfo nameProperty = typeof (DisplayAttribute).GetProperty("Name");
PropertyInfo orderProperty = typeof (DisplayAttribute).GetProperty("Order");

CustomAttributeBuilder displayAttributeBuilder = new CustomAttributeBuilder(constructorInfo, new object[] {  }, new PropertyInfo[]{ nameProperty, orderProperty}, new object[]{"Prop Name", 1} );

custNamePropBldr.SetCustomAttribute(displayAttributeBuilder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM