城堡动态代理不要将自定义属性写入代理
作者:互联网
我有简单的单元测试来重现情况:
[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; }
}
测试失败,因为Castle动态代理未写入自定义属性,
是否可以将父属性写入生成的代理?
解:
使用Attribute.GetCustomAttributes(…)
var attrs = Attribute.GetCustomAttributes(prop, typeof(DescriptionAttribute));
解决方法:
请改用Attribute.GetCustomAttributes(…)the method you’re using doesn’t work on properties.
标签:castle-dynamicproxy,c 来源: https://codeday.me/bug/20191201/2078659.html