gdbdable
gdbdable

Reputation: 4501

Castle dynamic proxy don't write custom attributes to proxy

I have simple unit test to reproduce situation:

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

Test fails because Castle dynamic proxy don't writes custom attributes,

It is possible to write parent attributes to generated proxies?

SOLUTION: use Attribute.GetCustomAttributes(...)

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

Upvotes: 4

Views: 1389

Answers (1)

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27374

Use Attribute.GetCustomAttributes(...) instead, the method you're using doesn't work on properties.

Upvotes: 4

Related Questions