私がやろうとしているのは、既存のattributesものをあるものpropertyから別のものにコピーすることです。今のところ私のコードは次のとおりです。
foreach (var prop in typeof(Example).GetProperties())
{
FieldBuilder field = typeBuilder.DefineField("_" + prop.Name, prop.PropertyType, FieldAttributes.Private);
PropertyBuilder propertyBuilder =
typeBuilder.DefineProperty(prop.Name,
PropertyAttributes.HasDefault,
prop.PropertyType,
null);
object[] attributes = prop.GetCustomAttributes(true);
foreach (var attr in attributes)
{
//Here I need to get value of constructor parameter passed in declaration of Example class
ConstructorInfo attributeConstructorInfo = attr.GetType().GetConstructor(new Type[]{});
CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(attributeConstructorInfo,new Type[]{});
propertyBuilder.SetCustomAttribute(customAttributeBuilder);
}
}
attributesそれは機能していますが、パラメーターなしの場合のみですconstructor。たとえば、「DataTypeAttribute」には のみconstructorsがありparameterます。
現在の値を取得する方法があるかどうか疑問に思っていますattribute constructor
私がこのモデルを持っているとしましょう:
public class Example
{
public virtual int Id { get; set; }
[Required]
[MaxLength(50)]
[DataType(DataType.Text)]
public virtual string Name { get; set; }
[MaxLength(500)]
public virtual string Desc { get; set; }
public virtual string StartDt { get; set; }
public Example()
{
}
}
今のところ、 parameterless があるからこそできるのcopyです。できません。だから私は私のモデル例からこれを取得したいと思います。RequiredAttributeconstructorcopy DataTypeAttributevalue DataType.Text
誰でもそれを機能させる方法についていくつかのアイデアがありますか?