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
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
See: System.Reflection Namespace
ASKER
Thanks
If you need more info ask me.
:)
:)
ASKER
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
If not, no problem.
Thanks
Assembly a = Assembly.GetExecutingAssem bly();
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("WindowsAppli cation4.My Class");
//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);
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("WindowsAppli
//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);
ASKER
Making progress. I see the .name of Form1 and MyClass, but I get a System.NullReferenceExcept ion' at this line:
MemberInfo[] Mymembersinfoarray = Mytype.GetMembers();
However, if I change Type.GetType("WindowsAppli cation1.Fo rm1"); 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?
MemberInfo[] Mymembersinfoarray = Mytype.GetMembers();
However, if I change Type.GetType("WindowsAppli
BTW, is there something I need to do to ensure you get the points for this example?
ASKER
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();
}
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("WindowsAppli cation4.My Class");
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.
Type Mytype = Type.GetType("WindowsAppli
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("WindowsAppli cation4.My Class");
Object obj = type1.InvokeMember("", BindingFlags.CreateInstanc e, null, null, null);
// execute Show method
Type type2 = obj.GetType();
type2.InvokeMember("Show", BindingFlags.InvokeMethod, null, obj, new object[] {"test string for Show"});
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("WindowsAppli
Object obj = type1.InvokeMember("", BindingFlags.CreateInstanc
// execute Show method
Type type2 = obj.GetType();
type2.InvokeMember("Show",
ASKER
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.
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.
ASKER
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("testvaria ble");
how would this bee done with:
type2.InvokeMember("Execut e", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
if the direct way to call the class of:
string myValue = MyClass.Execute("testvaria
how would this bee done with:
type2.InvokeMember("Execut
ASKER
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("testvaria ble");
how would this bee done with:
type2.InvokeMember("Execut e", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
if the direct way to call the class of:
string myValue = MyClass.Execute("testvaria
how would this bee done with:
type2.InvokeMember("Execut
ASKER
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("testvaria ble");
how would this bee done with:
type2.InvokeMember("Execut e", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
if the direct way to call the class of:
string myValue = MyClass.Execute("testvaria
how would this bee done with:
type2.InvokeMember("Execut
ASKER
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("testvaria ble");
how would this bee done with:
type2.InvokeMember("Execut e", BindingFlags.InvokeMethod, null, obj, new object[] {"testvariable"});
if the direct way to call the class of:
string myValue = MyClass.Execute("testvaria
how would this bee done with:
type2.InvokeMember("Execut
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you recommend a good book for C# that covers Assembly, InvokeMember, etc as to what was used in this solution?
Thank you.
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"
I can recommend you two O'Reilly books:
"Programming C#"
"Thuan L. Thai, Hoang Lam - .Net Framework Essentials, Edition 2"
ASKER
Thank you.