By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,892 Members | 1,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,892 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
http://msdn.microsoft.com/library/de...hodstopic2.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" <sr***@hotmail.com> wrote in message
news:Hg**********************@news.easynews.com...
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

Nov 15 '05 #2

P: n/a

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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #3

P: n/a
Urs
Thanks for the very quick anser.

"Philip Rieck" <st***@mckraken.com> wrote in
news:ud**************@TK2MSFTNGP12.phx.gbl:
http://msdn.microsoft.com/library/de...y/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" <sr***@hotmail.com> wrote in message
news:Hg**********************@news.easynews.com...
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



Nov 15 '05 #4

P: n/a
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
--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Urs" <sr***@hotmail.com> wrote in message
news:Hg**********************@news.easynews.com...
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

Nov 15 '05 #5

P: n/a
Urs
Mattias Sjögren <ma********************@mvps.org> wrote in
news:Ov**************@tk2msftngp13.phx.gbl:

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
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.