Reflection: GetMethods() vs GetMethods(BindingFlags.Public)

U

Urs

Hi all,

I'm rather new to C# so please forgive my ignorance.

I'd like to list the public, non-inherited methods of a class. I thought
MethodInfo[] mi = typeof(string).GetMethods(
BindingFlags.Public | BindingFlags.DeclaredOnly);
would do just that, but no methods at all are returned.

Going on, I tried typeof(string).GetMethods(BindingFlags.Public),
which I believe is equivalent to typeof(string).GetMethods(),
but again, nothing is returned.

What am I doing wrong here ?


Thanks a lot for any help
 
M

Mattias Sjögren

Have you tried including BindingFlags.Instance and/or
BindingFlags.Static, depending on if you want instance methods, static
methods or both?



Mattias
 
P

Philip Rieck

http://msdn.microsoft.com/library/d...html/frlrfsystemtypeclassgetmethodstopic2.asp

States
"You must specify either BindingFlags.Instance or BindingFlags.Static in
order to get a return".

MethodInfo[] mi = typeof(string).GetMethods( BindingFlags.Public |
BindingFlags.DeclaredOnly | BindingFlags.Instance);

will get all instance (non-static) public members declared by System.String
(note that this includes virtual methods that have implementations in
string, like GetHashCode() )
 
U

Urs

Thanks for the very quick anser.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/
html/frlrfsystemtypeclassgetmethodstopic2.asp

States
"You must specify either BindingFlags.Instance or BindingFlags.Static
in order to get a return".

MethodInfo[] mi = typeof(string).GetMethods( BindingFlags.Public |
BindingFlags.DeclaredOnly | BindingFlags.Instance);

will get all instance (non-static) public members declared by
System.String (note that this includes virtual methods that have
implementations in string, like GetHashCode() )


Urs said:
Hi all,

I'm rather new to C# so please forgive my ignorance.

I'd like to list the public, non-inherited methods of a class. I
thought MethodInfo[] mi = typeof(string).GetMethods(
BindingFlags.Public | BindingFlags.DeclaredOnly);
would do just that, but no methods at all are returned.

Going on, I tried typeof(string).GetMethods(BindingFlags.Public),
which I believe is equivalent to typeof(string).GetMethods(),
but again, nothing is returned.

What am I doing wrong here ?


Thanks a lot for any help
 
R

Richard A. Lowe

This is counter-intuitive, for sure, but you have to specify all
BindingFlags relevant to the kind of methods you want. It's not just
subtractive such that the BindingFlags you pass *restrict* the methods
returned, you actually have to add in all the binding flags you want. In
your case, you have to specify BindingFlags.Instance to return ALL methods
that are instance, declared and public.


Richard
 
U

Urs

Have you tried including BindingFlags.Instance and/or
BindingFlags.Static, depending on if you want instance methods, static
methods or both?



Mattias

No, I haven't :-(
This does the trick, as Philipp Rieck explained.

Thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top