tags:

views:

200

answers:

1

I've got some aspect like this:

public class MyAttribute : OnMethodInvocationAspect
{
    public int Offset { get; internal set; }

    public MyAttribute(int offset)
    {
        this.Offset = offset;
    }

    public override void OnInvocation(MethodInvocationEventArgs eventArgs)
    {
         //do some stuff
    }
}

Now I'm having my class, and I add my attribute to it:

class MyClass
{
    [MyAttribute(0x10)]
    public int MyProp { get; set; }
}

Works all fine. Yet now I want to use reflection to get my offset; when I do

typeof(MyClass).GetProperty("MyProp").GetCustomAttributes(true);

It returns nothing. How can I access my original Offset value (the property on my attribute)?

+2  A: 

Ah, I fixed it this way:

First add an attribute to your attribute definition like:

[MulticastAttributeUsage(MulticastTargets.Method, PersistMetaData=true)]
public class MyAttribute : OnMethodInvocationAspect

And then I can call the get_ method of my property to get the data I want:

        foreach (PropertyInfo pi in typeof(T).GetProperties())
        {
            var entityAttribute = (MyAttribute)(typeof(T).GetMethod("get_" + pi.Name).GetCustomAttributes(typeof(MyAttribute), true).FirstOrDefault());
        }
Jan Jongboom
Hmm can't accept my own answer yet :-)
Jan Jongboom
I accept it :) 1234
Gael Fraiteur