转换功能< DerivedType,Object>到Func< BaseType,Object> [英] Convert Func<DerivedType, Object> to Func<BaseType, Object>
本文介绍了转换功能< DerivedType,Object>到Func< BaseType,Object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从派生类型的第一个参数转换为接收基类型的代理的最佳方式是什么?
我的意思是:
What is the best way to convert from a delegate that takes a first parameter of a derived type into one that receives a base type? What I mean is:
Func<DerivedType, Object> original = ...;
Func<BaseType, Object> converted = Something(original);
Casts当然不起作用,因为这些实际上是两种不同的类型。
Casts, of course, do not work, since these are actually two different types.
推荐答案
由于您希望将基类型传递给采用派生类型的方法,因此需要添加一个转换。如果您知道所有调用已转换的调用
将会传递 DerivedType
,那么可以使用简单的包装器: / p>
Since you want to pass a base type to a method that takes a derived type, you need to add a cast. If you know that all calls to converted
would be passing DerivedType
, you could make a straightforward wrapper, like this:
Func<BaseType,Object> converted = b => original((DerivedType)b);
这篇关于转换功能< DerivedType,Object>到Func< BaseType,Object>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文