user2665267
user2665267

Reputation: 25

How to use Reflection GetMethods on protected readonly method

I have several protected readonly methods in a class that I need to retrieve so I can get the custom attributes. I am comfortable getting the custom attributes but how do I get the methods? This is the boiled down class:

public class TheParser
{
    [myAttribute("test")]
    protected readonly Parser<String> MyKeyWord;
}

And to get the methods I have tried many combinations using different BindingFlags but obviously I haven't found the right combination. Here is one attempt:

MethodInfo[] methods = typeof(TheParser).GetMethods(BindingFlags.NonPublic);

Upvotes: 0

Views: 43

Answers (1)

Simon Whitehead
Simon Whitehead

Reputation: 65077

That isn't a method.. its a field.

Use GetFields with NonPublic and Instance:

var fields = typeof(TheParser)
                 .GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

foreach (var field in fields)
    Console.WriteLine(field.Name); // prints "MyKeyWord"

Upvotes: 2

Related Questions