Reputation: 6444
I have a class with a definition of a private member like this:
[MyCustomAttribute]
private Func<String, String> MyFuncMember = (val) => val + " World! ";
and I'm trying to get the attribute that I put over it. Now, I have tried with Type.GetMembers()
, Type.GetFields()
and Type.GetMethods
with the appropriate BindingFlags
(BindingFlags.NonPublic
) and I just cannot get that member. How can I retrieve it? Could it be a problem if the class where is defined is a sealed
class?
Thanks in advance for you answers.
Upvotes: 4
Views: 300
Reputation: 837886
Try using this as your binding flags:
BindingFlags.NonPublic | BindingFlags.Instance
Without the BindingFlags.Instance
flag it won't be able to find your instance field.
In general when you use Type.GetField
you need to set:
one (or both) of BindingFlags.Instance
and BindingFlags.Static
and
BindingFlags.Public
and BindingFlags.NonPublic
.The |
operator combines the flags using a binary or operation, meaning that both flags are set.
Upvotes: 3
Reputation: 245389
typeof(YourType)
.GetMember("MyFuncMember", BindingFlags.Instance | BindingFlags.NonPublic)
.GetCustomAttributes(true);
Upvotes: 1