我有这堂课:
public class StructTest
{
public StructTest()
{
}
public void Start()
{
// do something
}
}
我想动态创建类 StructTest 的实例并使用反射执行其方法“Start”。但是下面的代码抛出了一个异常:
Assembly current = Assembly.GetExecutingAssembly();
string nomeClasse = "StructTest";
foreach (var classInAssembly in current.GetTypes().Where(p => p.IsClass).Where(p => p.Name.Equals(nomeClasse)))
{
Type type = classInAssembly.GetType();
var classe = Activator.CreateInstance(type, null); // Here the VS says theres no paramterless contructor for this class without parameters
IEnumerable<MethodInfo> methodList = classInAssembly.GetMethods().Where(p => p.Name.Equals("Start"));
MethodInfo method = methodList.First();
method.Invoke(classe, null);
}