2013-05-14 75 views
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属性。任何人都可以看到我做错了什么?

回答

0

我有同样的问题(与ScriptIgnore而不是XMLIgnore),并不能使它发挥作用。最后,我将设计器属性设为私有,并使用我想要的属性在外部部分类中定义公共属性,只是获取并设置私有属性。