Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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#_Generics_Reflection - Fatal编程技术网

C# 使用泛型类中定义的泛型参数调用非泛型方法

C# 使用泛型类中定义的泛型参数调用非泛型方法,c#,generics,reflection,C#,Generics,Reflection,这是我的问题 public class MyClass<T> { public void DoSomething(T obj) { .... } } 公共类MyClass { 公共无效剂量测定(T obj) { .... } } 我所做的是: var classType = typeof(MyClass<>); Type[] classTypeArgs = { typeof(T) }; var genericClass = classTy

这是我的问题

public class MyClass<T>
{
   public void DoSomething(T obj)
   {
      ....
   }
}
公共类MyClass
{
公共无效剂量测定(T obj)
{
....
}
}
我所做的是:

var classType = typeof(MyClass<>);
Type[] classTypeArgs = { typeof(T) };
var genericClass = classType.MakeGenericType(classTypeArgs);
var classInstance = Activator.CreateInstance(genericClass);
var method = classType.GetMethod("DoSomething", new[]{typeof(T)});
method.Invoke(classInstance, new[]{"Hello"});
var classType=typeof(MyClass);
Type[]classTypeArgs={typeof(T)};
var genericClass=classType.MakeGenericType(classTypeArgs);
var classInstance=Activator.CreateInstance(genericClass);
var method=classType.GetMethod(“DoSomething”,新[]{typeof(T)});
调用(classInstance,new[]{“Hello”});
在上述情况下,我得到的例外是:不能对ContainsGenericParameters为true的类型或方法执行后期绑定操作

如果我尝试将该方法设置为泛型,则会再次失败,出现异常: MakeGenericMethod只能在MethodBase.IsGenericMethodDefinition为true的方法上调用


如何调用该方法?

您在错误的对象上调用了
GetMethod
。用绑定的泛型类型调用它,它应该可以工作。以下是一个正常工作的完整示例:

using System;
using System.Reflection;

internal sealed class Program
{
    private static void Main(string[] args)
    {
        Type unboundGenericType = typeof(MyClass<>);
        Type boundGenericType = unboundGenericType.MakeGenericType(typeof(string));
        MethodInfo doSomethingMethod = boundGenericType.GetMethod("DoSomething");
        object instance = Activator.CreateInstance(boundGenericType);
        doSomethingMethod.Invoke(instance, new object[] { "Hello" });
    }

    private sealed class MyClass<T>
    {
        public void DoSomething(T obj)
        {
            Console.WriteLine(obj);
        }
    }
}
使用系统;
运用系统反思;
内部密封类程序
{
私有静态void Main(字符串[]args)
{
类型unboundGenericType=typeof(MyClass);
类型boundGenericType=unboundGenericType.MakeGenericType(typeof(string));
MethodInfo doSomethingMethod=boundGenericType.GetMethod(“DoSomething”);
对象实例=Activator.CreateInstance(boundGenericType);
Invoke(实例,新对象[]{“Hello”});
}
私有密封类MyClass
{
公共无效剂量测定(T obj)
{
控制台写入线(obj);
}
}
}