This is part 2 of an issue.
Initially I wanted to take a TextBox value and run that class. The following code is what I have been tolod to do. It took a while to get it to compile. However, now I need to know how to run/execute the class.
TheClass = textBoxClass.Text;
Assembly TheAssembly;
TheAssembly = Assembly.GetExecutingAssembly();
object MyDynamicClass = TheAssembly.CreateInstance("ICEPack.Class" + TheClass);
MyDynamicClass(); //<<
Jaish MathewsPosted Apr 29, 2010, 1:53 PM
Type[] signature = new Type[] { typeof(string)};
It should be
Type[] signature = new Type[] { typeof(string), typeof(string) };
Because you have 2 parameters.
GustavoPosted Apr 29, 2010, 1:59 PM
THANK YOU... It worked.
I am learning slowly but surely. Thanks again.
GustavoPosted Apr 29, 2010, 1:37 PM
Yes, I am probaly using the wrong terminology. Sorry.
Question: The Method is the section in the Class? If so, my class only has one method, 'Enter". I need to learn the terminology.
GustavoPosted Apr 29, 2010, 1:35 PM
Jaish:
This is the code with the hard coded class name and it works:
ClassCategory UpdateClass;
UpdateClass = new ClassCategory();
UpdateClass.Enter("From", "RunAs");
This is what I have to try to make it Dynamic. What is wrong?
string TheClass = "Category";
//
Assembly TheAssembly;
TheAssembly = Assembly.GetExecutingAssembly();
object MyDynamicClass = TheAssembly.CreateInstance("ICEPack.Class" + TheClass);
//
string MyMethod = "Enter";
Type[] signature = new Type[] { typeof(string) };
MethodInfo method = MyDynamicClass.GetType().GetMethod(MyMethod, signature);
object[] parameters = new object[] { "From" , "RunAs"};
//string nameDesc = (string)method.Invoke(MyDynamicClass, parameters);
method.Invoke(MyDynamicClass, parameters); //<<
//<<
Jaish MathewsPosted Apr 29, 2010, 1:31 PM
I confused the term "Run the class". You may meant some thing else, but normally we are runnng methods.
Simply asking that you already have class object, then whay again need to run or load the class. Aftr object creation, we are normally calling the methods in side it. Have you any methods inside it as a samle? More over debug to
your dynamic class object and make sure that it's not null during run time.
GustavoPosted Apr 29, 2010, 12:47 PM
I looked at your code and I can not get it to work. I am new to C#.
For now, I just want to run the class MyDynamicClass();
I will play with it for a while to try to make it work.
Jaish MathewsPosted Apr 29, 2010, 11:43 AM
Hi,
You already created you class object, MyDynamicClass
So nothing to do further with class. You need to call methods inside the class. If your method is like below,
string MyMethod(string)
Use below code
//Defining the parameter types for method I need to load
Type[] signature = new Type[] {typeof(string) };
//Loading method by mentioning the parameter types defined
MethodInfo method = MyDynamicClass.GetType().GetMethod("MyMethod", signature);
//Defining the parameter value for the method before calling it
object[] parameters = new object[] {"Jaish Mathews"};
//Calling the method with parameter value. Return tye always object to support all types. So I just casted the type to string
string nameDesc = (string)method.Invoke(MyDynamicClass, parameters);