I’m trying to check whether a given type is an action delegate, regardless of the amount of parameters.
The following code is the only way I know how to do this.
public static bool IsActionDelegate( this Type source )
{
return source == typeof( Action ) ||
source.IsOfGenericType( typeof( Action<> ) ) ||
source.IsOfGenericType( typeof( Action<,> ) ) ||
....
source.IsOfGenericType( typeof( Action<,,,,,,,,,,,,,,,> ) );
}
IsOfGenericType() is another extension method of mine, which does what it says, it checks whether the type is of the given generic type.
Any better suggestions?
If you are just after the delegates that have a void return type you could do the following:
This would not distinguish between
ActionandMethodInvoker(or other void delegates for that matter) though. As other answers suggest you could examine the type name, but that kinda smells 😉It would help if you could clarify for what reason you want to identify
Actiondelegates, to see which approach would work best.