Skip to main content
gamedev.net
🔒 Locked

[.net] C# reflection

Started by Thrump Jun 29, 2004 at 6:52 PM 6 replies 1.7k views
Original Post
Thrump
Thrump
I'm trying to get a list of all the member functions of my class, but nothing has worked as of yet. Here's my most recent attempt:

Type type = Type.GetType("BackgroundGenerator.Generator");
MemberInfo[] arMemberInfo = type.GetMembers(
			//BindingFlags.LookupAll |
			BindingFlags.DeclaredOnly 
			);
The GetType call seems to work, but arMemberInfo ends up with nothing in it. Notice also I've commented out LookupAll, as it doesn't seem to exist anymore (but lots of good examples use it). Has this been deprecated? I can get single methods like this: MethodBase method = type.GetMethod("Method1"); Generator is the class I'm interested in, and has plenty of member functions. I can post more code if that would be helpful. I'm new to C#, so any help would be great.
SiCrane
SiCrane
From the System.Type.GetMembers() documentation on MSDN:

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

Basically, do an or with your BindingFlags parameter with either BindingFlags.Instance or BindingFlags.Static and you should get a return.
yaroslavd
yaroslavd
I think you must also specify BindingFlags.Public | BindingFlags.NonPublic to access the private members as well as the public ones. You should double-check that, though 'cause I'm not sure.
Arild Fines
Arild Fines
[Consider putting this kind of question in the .NET forum in the future ;-)]

The docs state the following:
Quote:

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

Since you have neither, I am guessing this is your main problem. The following works for me:
Type t = Type.GetType( "System.String" );            MemberInfo[] info = t.GetMembers( BindingFlags.Instance | BindingFlags.Public );            Console.WriteLine( info.Length );

It seems that the BindingFlags.Public was also necessary.

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Thrump
Thrump
Quote:
Original post by Arild Fines
[Consider putting this kind of question in the .NET forum in the future ;-)]

I was not aware there is a .NET forum... I guess I should start scrolling down on the forum listing. ;)

BindingFlags.Instance | BindingFlags.Public works great, thanks!
Oluseyi
Oluseyi
Quote:
Original post by Arild Fines
[Consider putting this kind of question in the .NET forum in the future ;-)]
Even better, I moved it and left a link in General Programming, so others gradually get to know about it.
DrGUI
DrGUI
Quote:
Original post by Thrump
Quote:
Original post by Arild Fines
[Consider putting this kind of question in the .NET forum in the future ;-)]

I was not aware there is a .NET forum... I guess I should start scrolling down on the forum listing. ;)

BindingFlags.Instance | BindingFlags.Public works great, thanks!

I only noticed it today :~)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.