0
我有一个代码生成的类,它有一组属性属性。我想为这些属性添加额外的属性,但是我不能在代码生成的类上这样做。因此,我利用MetadataTypeAttribute
来修饰辅助类中的其他属性;MetadataTypeAttribute不向属性装饰附加属性
// Code generated class - can't touch this
public partial class MyClass
{
public MyType MyProperty { get; set; }
}
// Partial class allowing extended attributes
[MetadataType(typeof(MyClass_AdditionalAttributes))]
public partial class MyClass
{
}
// Defines extra attributes to be appended to
// properties that match in the partial class
public class MyClass_AdditionalAttributes
{
// Do not serialise the MyProperty property
[XmlIgnore]
public MyType MyProperty;
}
但是,这是行不通的。使用.NET反射器,XmlIgnoreAttribute
未被装饰到MyClass.MyProperty
属性。任何人都可以看到我做错了什么?