如何确定类型是否为 Action/Func 委托之一? [英] How to determine if the type is one of the Action/Func delegates?
本文介绍了如何确定类型是否为 Action/Func 委托之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除此之外,还有更好的方法来确定类型是否是 Action<> 委托之一.
Other than doing this is there a better way to determine whether type is a one of the Action<> delegates.
if(obj is MulticastDelegate && obj.GetType().FullName.StartsWith("System.Action"))
{
...
}
推荐答案
这看起来非常简单.
static bool IsAction(Type type)
{
if (type == typeof(System.Action)) return true;
Type generic = null;
if (type.IsGenericTypeDefinition) generic = type;
else if (type.IsGenericType) generic = type.GetGenericTypeDefinition();
if (generic == null) return false;
if (generic == typeof(System.Action<>)) return true;
if (generic == typeof(System.Action<,>)) return true;
... and so on ...
return false;
}
我很好奇你为什么想知道这个.如果特定类型恰好是 Action 的一个版本,您会关心什么?你打算怎么处理这些信息?
I'm curious as to why you want to know this though. What do you care if a particular type happens to be one of the versions of Action? What are you going to do with that information?
这篇关于如何确定类型是否为 Action/Func 委托之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文