2013-05-14 82 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),並不能使它發揮作用。最後,我將設計器屬性設爲私有,並使用我想要的屬性在外部部分類中定義公共屬性,只是獲取並設置私有屬性。