Reflection 如何从静态泛型类调用包含泛型类型的静态方法

Reflection 如何从静态泛型类调用包含泛型类型的静态方法,reflection,Reflection,我使用反射从第三方使用泛型计算出我的项目类,但是,当我尝试从包含泛型调用的静态泛型类调用静态方法时,我不断得到错误“不能对ContainsGenericParameters为true的类型或方法执行后期绑定操作” 第三方代码如下所示 public interface INestedGeneric<TResult> { INestedGeneric<TResult> DoSomethingElse(); } public static class GenericC

我使用反射从第三方使用泛型计算出我的项目类,但是,当我尝试从包含泛型调用的静态泛型类调用静态方法时,我不断得到错误“不能对ContainsGenericParameters为true的类型或方法执行后期绑定操作”

第三方代码如下所示

public interface INestedGeneric<TResult>
{
    INestedGeneric<TResult> DoSomethingElse();
}

public static class GenericClass<TResult> where TResult : new()
{
    public static INestedGeneric<TResult> DoSomething()
    { return new NestedClass<TResult>(); }
}

public class NestedClass<TResult> : INestedGeneric<TResult>
{
    public INestedGeneric<TResult> DoSomethingElse()
    { return new NestedClass<TResult>(); }
}
INestedGeneric的公共接口 { INestedGeneric DoSomethingElse(); } 公共静态类GenericClass,其中TResult:new() { 公共静态能量剂量测定法() {返回新的NestedClass();} } 公共类嵌套类:INestedGeneric { 公共卫生研究院能量剂量表() {返回新的NestedClass();} } 我的代码如下所示:

public class Someone
{
    private int _integerProperty;
    private string _stringProperty;

    public int IntegerProperty
    {
        get { return _integerProperty; }
        set { _integerProperty = value; }
    }

    public string StringProperty
    {
        get { return _stringProperty; }
        set { _stringProperty = value; }
    }
}

    static void Main(string[] args)
    {

        Type classType = typeof(Someone);
        Type theclass = typeof(GenericClass<>); theclass.MakeGenericType(classType);
        Type theinterface = typeof(INestedGeneric<>); theinterface.MakeGenericType(classType);

        MethodInfo dosomething = theclass.GetMethod("DoSomething", BindingFlags.Public | BindingFlags.Static);
        dosomething.Invoke(null, null);
        dosomething = null;

    }
公共类某人
{
私有整数属性;
私有字符串_stringProperty;
公共整型属性
{
获取{return\u integerProperty;}
设置{u integerProperty=value;}
}
公共字符串字符串属性
{
获取{return\u stringProperty;}
设置{u stringProperty=value;}
}
}
静态void Main(字符串[]参数)
{
类型classType=typeof(某人);
键入class=typeof(GenericClass);class.MakeGenericType(classType);
键入interface=typeof(INestedGeneric);interface.MakeGenericType(classType);
MethodInfo dosomething=class.GetMethod(“dosomething”,BindingFlags.Public | BindingFlags.Static);
调用(null,null);
dosomething=null;
}
你知道在这种情况下如何严格调用这个方法吗?我已经阅读并尝试了其他帖子的帮助,但没有成功

非常感谢你

我已经弄明白了。解决方案是使用MakeGenericType方法提供的类型

像这样:

        ...
        Type theclass = typeof(GenericClass<>).MakeGenericType(classType);
        Type theinterface = typeof(INestedGeneric<>).MakeGenericType(classType);

        MethodInfo dosomething = theclass.GetMethod("DoSomething", BindingFlags.Public | BindingFlags.Static);
        dosomething.Invoke(null, null);
        ...
。。。
Type theclass=typeof(GenericClass)。MakeGenericType(classType);
输入接口=typeof(INestedGeneric)。生成泛型(classType);
MethodInfo dosomething=class.GetMethod(“dosomething”,BindingFlags.Public | BindingFlags.Static);
调用(null,null);
...