2012-06-24 68 views
2

我有簡單的單元測試重現情況:城堡動態代理不寫自定義屬性代理

[Test] 
public void Castle_Writes_Attribute_To_Proxy() 
{ 
    var generator = new ProxyGenerator(); 
    var proxy = generator.CreateClassProxy<MyType>(); 

    var type = proxy.GetType(); 

    var prop = type.GetProperty("SomeProp"); 

    var attrs = prop.GetCustomAttributes(typeof(DescriptionAttribute), true); 

    Assert.That(attrs.Length, Is.Not.EqualTo(0)); 
} 

public class MyType 
{ 
    [Description("some description here")] 
    public virtual string SomeProp { get; set; } 
} 

測試失敗,因爲城堡動態代理不寫入自定義屬性,

有可能將父屬性寫入生成的代理?

SOLUTION: 使用Attribute.GetCustomAttributes(...)

var attrs = Attribute.GetCustomAttributes(prop, typeof(DescriptionAttribute)); 

回答