状況を再現するための簡単な単体テストがあります。
[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));