I used the code above to create a dinamic instace os a class "MyType":
Code:
using System;
using System.Reflection;
class MyType {
public void Hello() {
Console.WriteLine("Hello world!");
}
}
class TheApp {
public static void Main() {
Console.Write("Create an object using Activator.CreateInstance");
String typeName = "MyType";
String methodName = "Hello";
// create the MyType object, by the name!
Type t = Type.GetType(typeName);
// create an instance of that type, ok the actual type is known only at run time ('dinamically')
Object obj = Activator.CreateInstance(t);
// call the requested method
t.GetMethod(methodName).Invoke(obj, null);
}
}
But if the method "Hello()" have an overload like this:
Code:
public void Hello() {
Console.WriteLine("Hello world!");
}
}
public void Hello(int Id) {
Console.WriteLine("Hello world"+ Id.ToString() + "!");
}
}
the code return the error "Ambiguous match found" when the line
Code:
t.GetMethod(methodName).Invoke(obj, null);
is executed.
I have tried use an object[] with the params of the method hello but don´t work too.
What´s the problem with the code??
Thank´s