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

  • Thread starter Thread starter Urs
  • Start date Start date
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
 
Have you tried including BindingFlags.Instance and/or
BindingFlags.Static, depending on if you want instance methods, static
methods or both?



Mattias
 
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() )
 
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
 
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
 
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
 
Back
Top