我想通过调用具有不同类型的泛型方法在这样的循环中执行一些类似的过程。
AAA
,BBB
都是类。CreateProcessor
是类别中的通用方法MyProcessor
。
new List<Type> {typeof (AAA), typeof (BBB)}.ForEach(x =>
{
var processor = MyProcessor.CreateProcessor<x>(x.Name);
processor.process();
});
这没有编译,我得到了错误提示Cannnot resolve symbol x
。
从技术上讲,如何实现呢?(我知道策略模式更好...)
抱歉,我更新了我的问题。我打算实际上调用通用方法。
var method = typeof(MyProcessor).GetMethod("CreateProcessor", new Type[] { typeof(string) });
new List<Type> { typeof(AAA), typeof(BBB) }.ForEach(x =>
{
dynamic processor = method.MakeGenericMethod(x).Invoke(null, new[] { x.Name });
processor.process();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句