4

状況を再現するための簡単な単体テストがあります。

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

1 に答える 1

4

Attribute.GetCustomAttributes(...)代わりに、使用しているメソッドはプロパティでは機能しません

于 2012-06-24T20:24:44.147 に答える