如何在C#中完成以下工作?
var someType = new CustomerMessageHandler<CustomerMessage>();
var nType = someType.GetGenericArguments().First().GetType();
// except the next line of code would be a (Action<nType>)
var a = new Action<string>();Reflection.Emit、表达式或F#有一个很好的解决方案吗?
注:“以会议为基础”的方法对我来说不是一种选择。我想看看这是否有可能提升我的C#/F#技能。
发布于 2014-04-17 20:35:41
当然,MakeGenericType将能够为您做到这一点。
Type genericAction = typeof(Action<>);
var someType = new CustomerMessageHandler<CustomerMessage>();
var nType = someType.GetGenericArguments().First().GetType();
var actionType = genericAction.MakeGenericType(new[] { nType });但是请注意,Action<T>是一种实际应该做一些事情的委托.所以你需要为此写一具身体:
var actualMethod = Delegate.CreateDelegate(actionType, **your method info here**);https://stackoverflow.com/questions/23142920
复制相似问题