?
Solved

dynamic list of classes

Posted on 2003-03-27
18
Medium Priority
?
178 Views
Last Modified: 2010-04-15
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
Comment
Question by:econner20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 7
18 Comments
 
LVL 4

Expert Comment

by:Nebulus_
ID: 8218512
See: System.Reflection Namespace
0
 

Author Comment

by:econner20
ID: 8218567
Thanks
0
 
LVL 4

Expert Comment

by:Nebulus_
ID: 8218681
If you need more info ask me.
:)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:econner20
ID: 8218803
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
 
LVL 4

Expert Comment

by:Nebulus_
ID: 8219019
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
 

Author Comment

by:econner20
ID: 8219210
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
 

Author Comment

by:econner20
ID: 8222831
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
 
LVL 4

Expert Comment

by:Nebulus_
ID: 8223295
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
 
LVL 4

Expert Comment

by:Nebulus_
ID: 8223390
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
 

Author Comment

by:econner20
ID: 8225339
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
 

Author Comment

by:econner20
ID: 8236406
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
 

Author Comment

by:econner20
ID: 8236422
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
 

Author Comment

by:econner20
ID: 8236431
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
 

Author Comment

by:econner20
ID: 8236436
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
 
LVL 4

Accepted Solution

by:
Nebulus_ earned 300 total points
ID: 8236956
return value of InvokeMethod is your result:

Object retval = type2.InvokeMember("Execute", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
MessageBox.Show((string)retval);
0
 

Author Comment

by:econner20
ID: 8261310
Can you recommend a good book for C# that covers Assembly, InvokeMember, etc as to what was used in this solution?

Thank you.
0
 
LVL 4

Expert Comment

by:Nebulus_
ID: 8261497
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
 

Author Comment

by:econner20
ID: 8261591
Thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question