Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more: (untagged)
Starting with an arbitrary object that is known to have a public static method "foobar", I have seen that both these forms will find the method.
C#
obj.GetType().GetMethod("foobar")
obj.GetType().GetMethod("foobar", BindingFlags.Static | BindingFlags.Public).
But these two forms both return null.
C#
obj.GetType().GetMethod("foobar", BindingFlags.Static)
obj.GetType().GetMethod("foobar", BindingFlags.Public)
The MS description of this signature for Type.GetMethod tells me that all four should find that method. It looks like an implementation error to me. Am I right?
Posted

Whilst it feels like the code should work if you only pass Public or Static, the documentation clearly states that it won't:

You must specify Instance or Static along with Public or NonPublic or no members will be returned.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Mar-14 15:55pm    
The second line of the OP's code is valid and unambiguous. Simply, there are no such methods. First line, too; it defaults to public.
Please see my answer.
—SA
Richard Deeming 26-Mar-14 16:04pm    
The first two lines aren't the issue. The question is why the second two lines don't return anything.

It *feels* like they should - passing only "Static" should return any public or private static methods, and passing only "Public" should return any static or instance public methods - but the documentation makes it clear that this doesn't work. If you don't specify either "Instance" or "Static", the default is to return nothing. Similarly, if you don't pass either "Public" or "NonPublic", nothing will be returned.
Sergey Alexandrovich Kryukov 26-Mar-14 16:11pm    
Sure. Probably you had to express it more directly that you are talking about two last lines.
The real problem is that OP tried all the variants and did not get desirable return, so the practical issue is different.

I voted 4 for your post.

—SA
John Whitmire 27-Mar-14 10:15am    
Too bad the documentation page I found (not in the vstudio path) didn't have that statement. It makes the answer very clear. Thanks for pointing it out.
There is nothing buggy here, tested too many times. You flag BindingFlags.Static | BindingFlags.Public means getting all methods of the type (in you case, matching specified simple name) which are static and public at the same time. This method can really return null. If it happens, it means that no methods meet these criteria. Check it up.

Note the following
  • In your code sample, you ignore (throw out) result returned by the method;
  • Names are case-sensitive.


Good luck,
—SA
 
Share this answer
 
Comments
Espen Harlinn 26-Mar-14 17:56pm    
5'ed :-)
Sergey Alexandrovich Kryukov 26-Mar-14 19:08pm    
Thank you, Espen.
—SA
Sergey Alexandrovich Kryukov 1-Apr-14 8:57am    
How have you been, by the way.

please look at my recent article, you may find it interesting: http://www.codeproject.com/Articles/752137/Power-Over-IP-Testing-of-the-First-Experimental-Fa.

I hope you will participate in discussion and ask me some difficult questions. :-)

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900