c# - How to do dynamic object creation and method invocation in .NET 3.5 -


How the code looks which will create an object of the class:

  string myClass = "my class";  

Call the above type, and then

  string myMethod = "MyMethod";  

to obtain

  • on that object Use
  • Use to create an example.
  • Use to recover the method.
  • Use the object to invoke the method

Example, but with no error checking:

  Using; Using the system. Namespace Fu {Class Test {Static Zero Main () (Type Type = Type GateType ("Foo. Miklass"); Object instance = Activator. Create Instance (Type); MethodInfo method = type.GetMethod ("MyMethod"); Method Inquoke (example, zero);}} class MyClass {public Zero MyMythype () {Console.WriteLine (in "MyClass.MyMethod")}}}}  

Carefully check each step Required - you can not find the type, it can not be a parametric constructor, You can not find the method, you can add it to the wrong logic type.

One thing to note: type GateType (string) requires a legislative name of that type That he is currently not in the assembly or mscorlib executed.


Comments