var constructor;
var params = constructor.GetParameters();
object[] args;
foreach( var param in params ) {
var type = param.Parametertype;
args[] += (object)Build<type>();
}
Activator.CreateInstance(Type, args);
问题是,我无法将参数类型作为泛型参数传递。
答案 0 :(得分:1)
不,您还需要使用反射来调用泛型方法:
var constructor = ...;
var parameters = constructor.GetParameters();
object[] args = new object[parameters.Length];
// Adjust this for private methods etc
var buildMethod = typeof(ClassContainingBuild).GetMethod("Build");
for (int i = 0; i < args.Length; i++)
{
var genericBuild = buildMethod.MakeGenericMethod(parameters[i].ParameterType);
// Adjust appropropriately for target etc
args[i] = genericBuild.Invoke(this, null);
}