Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过反射实例化泛型类_C#_.net - Fatal编程技术网

C# 通过反射实例化泛型类

C# 通过反射实例化泛型类,c#,.net,C#,.net,我想通过反射实例化一个类,如下所示: public class TestConsume<T> : BaseExecutor<T> { public overide SomeMethod(T input) { } } 我还使用了Type.GetType()等,我完全不知所措。要使Assembly.GetType正常工作,需要使用CLR名称,其中包括泛型arity—它具有的类型参数数: Type type = Assembly.GetExecutingAs

我想通过反射实例化一个类,如下所示:

public class TestConsume<T> : BaseExecutor<T>
{
   public overide SomeMethod(T input)
   {
   }
}

我还使用了Type.GetType()等,我完全不知所措。

要使
Assembly.GetType
正常工作,需要使用CLR名称,其中包括泛型arity—它具有的类型参数数:

Type type = Assembly.GetExecutingAssembly().GetType("Test.TestConsume`1");
这是假设您需要使用字符串表示来完成它。如果没有,您可以使用:

Type type = typeof(Test.TestConsume<>);

该算术必须是
1
?我只能看到一个类型参数。或我遗漏了什么?@SriramSakthivel:对不起,只是一个打字错误。Fixed.yea,我想我确实需要使用字符串表示法,因为我不知道我将要创建Test.testconsumer,或者可能是另一个名为Test.testconsumerb的类,它也继承自BaseExecutor当jon输入错误时,我觉得我的理解是错误的:)很高兴知道这是一个输入错误。。
Type type = Assembly.GetExecutingAssembly().GetType("Test.TestConsume");
Type type = Assembly.GetExecutingAssembly().GetType("Test.TestConsume`1");
Type type = typeof(Test.TestConsume<>);
Type constructed = type.MakeGenericType(typeof(string)); // Or whatever