城堡动态代理不写自定义属性代理

城堡动态代理不写自定义属性代理

问题描述:

我有简单的单元测试重现情况:城堡动态代理不写自定义属性代理

[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)); 

使用Attribute.GetCustomAttributes(...)相反,the method you're using doesn't work on properties

+0

谢谢!这个对我有用。 – devi