MetadataTypeAttribute不为属性添加其他属性

时间:2013-05-14 08:17:38

标签: c# attributes metadata code-generation

我有一个代码生成的类,它在属性上有一组属性。我想为这些属性添加其他属性,但我不能在代码生成的类上执行此操作。因此,我使用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属性。谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题(使用 ScriptIgnore 而不是 XMLIgnore )并且无法使其正常工作。最后,我将设计器属性设为私有,并在外部分部类中使用我想要的属性定义公共属性,只需获取并设置私有属性。

相关问题