Link to home
Start Free TrialLog in
Avatar of econner20
econner20

asked on

dynamic list of classes

Hello,

I am looking for a way in C# to be able to programatically build a list of available classes in the applcation that I can search the list and execute a desired class method based on a passed parameter to the program. I could do this by building a case statement with all available class names, but would like it to be more dynamic.

Thanks
Avatar of Nebulus_
Nebulus_

See: System.Reflection Namespace
Avatar of econner20

ASKER

Thanks
If you need more info ask me.
:)
Do you have any quick examples of using the System.Reflection Namespace to list the available class names in an application?

If not, no problem.

Thanks
Assembly a = Assembly.GetExecutingAssembly();
Type[] b = a.GetTypes();
//Get types
foreach(Type c in b)
  MessageBox.Show(c.Name);
//Get a type semple (I have a class MyClass)
MemberTypes Mymembertypes;
Type Mytype = Type.GetType("WindowsApplication4.MyClass");
//Get the MemberInfo array
MemberInfo[] Mymembersinfoarray = Mytype.GetMembers();
//Get and display the name and the MemberType for each member
string text ="";
foreach (MemberInfo Mymemberinfo in Mymembersinfoarray) {
  text = text + "\n" + Mymemberinfo.Name;
  Mymembertypes = Mymemberinfo.MemberType;  
  text = text + " is a " + Mymembertypes.ToString();
}
MessageBox.Show(text);
Making progress. I see the .name of Form1 and MyClass, but I get a System.NullReferenceException' at this line:

MemberInfo[] Mymembersinfoarray = Mytype.GetMembers();

However, if I change Type.GetType("WindowsApplication1.Form1"); I see all of the types in the message box. Am I not defining something in MyClass?


BTW, is there something I need to do to ensure you get the points for this example?

This worked on returning the class names. I now have populated a string collection with the names of the matching classes. In doing so, I ran into another question.  If I have several classes that have a method of .execute(), how can I create an instance and call the .execute of the function that matches the function name that is in the string collection? for example:


public class MyTestFuction
{
 public void Execute()
 {
   // do something here
 }
}

string s = "MyTestFuction"


if (s == "MyTestFuction")
{
   //How to use the string name to
   //call the matching class
   s ????? .Execute();
}
for error in line:

Type Mytype = Type.GetType("WindowsApplication4.MyClass");

You must prefix the class name (here MyClass is a class that I defined) with the correct name of namespace. In my sample the namespace is "WindowsApplication4". You must put yours.
second answer to execute a method of a class.

MyClass has this method:

public void Show(string msg) {
  MessageBox.Show(msg);
}

I can call this method from reflection like this:

// create an instance of "MyClass"
Type type1 = Type.GetType("WindowsApplication4.MyClass");
Object obj = type1.InvokeMember("", BindingFlags.CreateInstance, null, null, null);
// execute Show method
Type type2 = obj.GetType();
type2.InvokeMember("Show", BindingFlags.InvokeMethod, null, obj, new object[] {"test string for Show"});
Thanks. This seems to be working.

I am not familar with the InvokeMemeber, but will do a little more research.

I have successfully created a base class and created a new class from it and overrode the execute function and called it from your example.

Thanks again.
With using InvokeMember as per the above example, how can you get back results from a fuction that returns a value.

if the direct way to call the class of:

string myValue = MyClass.Execute("testvariable");

how would this bee done with:
type2.InvokeMember("Execute", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
 
With using InvokeMember as per the above example, how can you get back results from a fuction that returns a value.

if the direct way to call the class of:

string myValue = MyClass.Execute("testvariable");

how would this bee done with:
type2.InvokeMember("Execute", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
 
With using InvokeMember as per the above example, how can you get back results from a fuction that returns a value.

if the direct way to call the class of:

string myValue = MyClass.Execute("testvariable");

how would this bee done with:
type2.InvokeMember("Execute", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
 
With using InvokeMember as per the above example, how can you get back results from a fuction that returns a value.

if the direct way to call the class of:

string myValue = MyClass.Execute("testvariable");

how would this bee done with:
type2.InvokeMember("Execute", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
 
ASKER CERTIFIED SOLUTION
Avatar of Nebulus_
Nebulus_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you recommend a good book for C# that covers Assembly, InvokeMember, etc as to what was used in this solution?

Thank you.
I used MSDN for this solution.
I can recommend you two O'Reilly books:

"Programming C#"
"Thuan L. Thai, Hoang Lam - .Net Framework Essentials, Edition 2"
Thank you.