Hello:
I posted this before but never got a solution, so I am asking again. Maybe someone else can complete this issue.
I want to be able to call a class with a variable name. The name of the class will be in a TextBox.
This is the code that I have at the moment:
string TheClass = textBoxClass_ID.Text;
object myDynamicClass = new Assembly.CreateInstance(TheClass);
myDynamicClass();
Can someone tell me what I am doing wrong?
Hirendra SisodiyaPosted Apr 26, 2010, 1:57 AM
No need to do big thing... try these lines:
string TheClass = textBoxClass_ID.Text;
Assembly Asm;
Asm = Assembly.GetExecutingAssembly();
object myDynamicClass = Asm.CreateInstance("ICEPack." + TheClass);
Note:
don't use '.cs' with classname
thanks
Please mark as answere if it helps
GustavoPosted Apr 26, 2010, 11:07 AM
Thank You... It worked.
Jaish MathewsPosted Apr 26, 2010, 4:04 AM
I could not download your deatils from office. Will check once out of office.
For calling from any other external classes use the below lines. Because GetExecutingAssembly(); will do only if your are using that assembly at run time. You may need flexibility of calling from any class in a later time. Here used class named "ClassDB". Just check that "myAssembly" is loading without a null. That's the 1st and mandatory thing.
textBoxClass_ID.Text = <
Assembly myAssembly = Assembly.LoadFrom(<
object obj = myAssembly.CreateInstance(textBoxClass_ID.Text );
GustavoPosted Apr 25, 2010, 7:01 PM
I am enclosing the word document with the screen shot answers... I hope.
Jaish MathewsPosted Apr 25, 2010, 11:01 AM
GustavoPosted Apr 25, 2010, 9:45 AM
I am a newbie to c#, so sorry for my ignorance.
The project name is "ICEPack".
The Class detail is:
Name: "ClassDB.cs" or "ClassCompnay.cs" or "ClassReport.cs" and a lot more.
Let assume you only need two string variables to be sent: string AAA and string BBB.
Hope this answers your question.
When I hard code the acces of the class Its:
ClassDB ClassDB = new ClassDB();
ClassDB.Inq(AAA, BBB);
Jaish MathewsPosted Apr 25, 2010, 9:30 AM
We will not use .CS file any where. We will use full assemblyname and full class name.
AssemblyName - Go to your project where your class residing. Take it's properties. From ApplicationTab you can see the Assembly name. Assembly is like .EXE of C/C++ languages, even though internally differes. So you need to use the project assembly name with full path and it's not .CS name like ClassValidateTable.cs. Assembly may contain many classes and .CS files.
If you still problem, send me your project name and class details.
GustavoPosted Apr 25, 2010, 8:09 AM
I did a cut/paste of your code to see if it would compile and it did.
Now I am changing it for my use.
Your line:
Assembly myAssembly = Assembly.LoadFrom(@"C:\Users\Administrator\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe");
Changed to:
textBoxClass_ID.Text = "ClassValidateTable.cs";
Assembly myAssembly = Assembly.LoadFrom(textBoxClass_ID.Text);
It says it can not find it. The class is in my project. Do I havwe to give it the full path?
Jaish MathewsPosted Apr 25, 2010, 12:22 AM
Hi,
You need to use reflection to load classes and to call it's methods dynamically.
Below is a sample class which is residing in an assembly named WindowsFormsApplication1.exe.
I put all the steps to call this class and it's method dynamically. Hope this will help. More over I think your class assembly is local rather than a shared assembly.
namespace WindowsFormsApplication1
{
public class MyClass
{
public string MyMethod(string name)
{
return "My name is " + name;
}
}
}
//Loading the assembly with full path in which my desired class exists
Assembly myAssembly = Assembly.LoadFrom(@"C:\Users\Administrator\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe");
//Creating an instance of me desired class by referring fullname i.e. including NameSpace
object obj = myAssembly.CreateInstance("WindowsFormsApplication1.MyClass");
//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 = obj.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(obj, parameters);