Question

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));
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top