gpt4 book ai didi

c# - 使用运行时类型执行的通用方法

转载 作者:太空狗 更新时间:2023-10-30 01:08:13 26 4
gpt4 key购买 nike

<分区>

奇怪的是源码

public class Processor<T> where T: class
{
...
private object WorkWithSubtype(IRequester nextRequester, Type type)
{
if (type.GetInterface("IList") != null)
{
var originalType = type.GetGenericArguments()[0];
var list = Activator.CreateInstance(type);

var method = typeof(Processor<>).GetMethod("GetObjects", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(originalType);
var resList = method.Invoke(this, new object[] { nextRequester });
typeof(List<>).GetMethod("AddRange").MakeGenericMethod(originalType).Invoke(list, new object[] { resList });
return list;
}
}

private IEnumerable<K> GetObjects<K>(IRequester requester) where K: class
{
...
//We can call method WorkWithSubtype in this method sometimes
}
}

我得到“无法对 ContainsGenericParameters 为真的类型或方法执行后期绑定(bind)操作”。异常在行 'var resList = method.Invoke(this, new object[] { nextRequester });' 处抛出。你能帮助我吗?提前致谢!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com