• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

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
0
econner20
Asked:
econner20
  • 11
  • 7
1 Solution
 
Nebulus_Commented:
See: System.Reflection Namespace
0
 
econner20Author Commented:
Thanks
0
 
Nebulus_Commented:
If you need more info ask me.
:)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
econner20Author Commented:
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
0
 
Nebulus_Commented:
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);
0
 
econner20Author Commented:
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?

0
 
econner20Author Commented:
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();
}
0
 
Nebulus_Commented:
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.
0
 
Nebulus_Commented:
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"});
0
 
econner20Author Commented:
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.
0
 
econner20Author Commented:
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"});
 
0
 
econner20Author Commented:
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"});
 
0
 
econner20Author Commented:
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"});
 
0
 
econner20Author Commented:
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"});
 
0
 
Nebulus_Commented:
return value of InvokeMethod is your result:

Object retval = type2.InvokeMember("Execute", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
MessageBox.Show((string)retval);
0
 
econner20Author Commented:
Can you recommend a good book for C# that covers Assembly, InvokeMember, etc as to what was used in this solution?

Thank you.
0
 
Nebulus_Commented:
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"
0
 
econner20Author Commented:
Thank you.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now