检查某个类型是否是 Action 委托

发布于 2024-10-19 13:26:29 字数 545 浏览 1 评论 0原文

我正在尝试检查给定类型是否是操作委托,无论参数数量如何。

以下代码是我知道如何执行此操作的唯一方法。

    public static bool IsActionDelegate( this Type source )
    {
        return source == typeof( Action ) ||
               source.IsOfGenericType( typeof( Action<> ) ) ||
               source.IsOfGenericType( typeof( Action<,> ) ) ||
               ....
               source.IsOfGenericType( typeof( Action<,,,,,,,,,,,,,,,> ) );
    }

IsOfGenericType() 是我的另一个扩展方法,它执行它所说的操作,它检查类型是否是给定的泛型类型。

还有更好的建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

攒一口袋星星 2024-10-26 13:26:29

如果您紧随具有 void 返回类型的委托之后,您可以执行以下操作:

public static bool IsActionDelegate(Type sourceType)
{
    if(sourceType.IsSubclassOf(typeof(MulticastDelegate)) && 
       sourceType.GetMethod("Invoke").ReturnType == typeof(void))
        return true;
    return false;
}

这不会区分 ActionMethodInvoker (或与此相关的其他 void 委托)尽管。正如其他答案所建议的,您可以检查类型名称,但这有点难闻;-)
如果您能澄清您想要识别 Action 委托的原因,以了解哪种方法最有效,将会有所帮助。

If you are just after the delegates that have a void return type you could do the following:

public static bool IsActionDelegate(Type sourceType)
{
    if(sourceType.IsSubclassOf(typeof(MulticastDelegate)) && 
       sourceType.GetMethod("Invoke").ReturnType == typeof(void))
        return true;
    return false;
}

This would not distinguish between Action and MethodInvoker (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 Action delegates, to see which approach would work best.

明月松间行 2024-10-26 13:26:29
    static Type[] _actionTypes = new[]{
        typeof(Action),
        typeof(Action<>),
        typeof(Action<,>),
        typeof(Action<,,>),
        typeof(Action<,,,>),
        typeof(Action<,,,,>),
        typeof(Action<,,,,,>),
        typeof(Action<,,,,,,>),
        typeof(Action<,,,,,,,>),
        typeof(Action<,,,,,,,,>),
        typeof(Action<,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,,,,,>)
    };
    private static bool IsAction(Delegate d)
    {
        return d != null && Array.IndexOf(_actionTypes, d.GetType()) != -1;
    }
    static Type[] _actionTypes = new[]{
        typeof(Action),
        typeof(Action<>),
        typeof(Action<,>),
        typeof(Action<,,>),
        typeof(Action<,,,>),
        typeof(Action<,,,,>),
        typeof(Action<,,,,,>),
        typeof(Action<,,,,,,>),
        typeof(Action<,,,,,,,>),
        typeof(Action<,,,,,,,,>),
        typeof(Action<,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,,,,>),
        typeof(Action<,,,,,,,,,,,,,,,>)
    };
    private static bool IsAction(Delegate d)
    {
        return d != null && Array.IndexOf(_actionTypes, d.GetType()) != -1;
    }
何必那么矫情 2024-10-26 13:26:29

这似乎有效:

    private static bool IsActionDelegate(this Type source)
    {
        var type = source.Name;
        return source.Name.StartsWith("System.Action");
    }

示例:

public static class Test
{
    public static bool IsActionDelegate(this Type source)
    {
        var type = source.Name;
        return source.Name.StartsWith("Action");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Action<string> one = s => { return; };
        Action<int, string> two = (i, s) => { return; };
        Func<int, string> function = (i) => { return null; };

        var single = one.GetType().IsActionDelegate();
        var dueces = two.GetType().IsActionDelegate();
        var func = function.GetType().IsActionDelegate();
    }
}

单身和单身都是正确的。函数为假

This seems to work:

    private static bool IsActionDelegate(this Type source)
    {
        var type = source.Name;
        return source.Name.StartsWith("System.Action");
    }

Example:

public static class Test
{
    public static bool IsActionDelegate(this Type source)
    {
        var type = source.Name;
        return source.Name.StartsWith("Action");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Action<string> one = s => { return; };
        Action<int, string> two = (i, s) => { return; };
        Func<int, string> function = (i) => { return null; };

        var single = one.GetType().IsActionDelegate();
        var dueces = two.GetType().IsActionDelegate();
        var func = function.GetType().IsActionDelegate();
    }
}

Single and dueces are true. func is false

留一抹残留的笑 2024-10-26 13:26:29

这些是不同的类型,除了名称之外没有任何共同点。我能想到的唯一半合理的捷径:

public static bool IsActionDelegate( this Type source )
{
    return source.FullName.StartsWith("System.Action");
}

当然不是万无一失的,但是任何在 System 命名空间中声明自己的类型的人都应该受到一些痛苦和折磨。

These are distinct types with nothing in common but their name. The only semi-reasonable shortcut I can think of:

public static bool IsActionDelegate( this Type source )
{
    return source.FullName.StartsWith("System.Action");
}

Certainly not fail-safe, but whomever declares his own types in the System namespace deserves some pain and suffering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文