确定从泛型类型派生的类型

发布于 2024-10-07 02:02:35 字数 1276 浏览 10 评论 0原文

我有以下实用程序例程,用于确定类型是否派生自特定类型:

private static bool DerivesFrom(Type rType, Type rDerivedType)
{
    while ((rType != null) && ((rType != rDerivedType)))
        rType = rType.BaseType;
    return (rType == rDerivedType);
}

(实际上我不知道是否有更方便的方法来测试派生...)

问题是我想要确定类型是否派生自泛型类型,但不指定泛型参数。

例如我可以写:

DerivesFrom(typeof(ClassA), typeof(MyGenericClass<ClassB>))

但我需要的是以下内容

DerivesFrom(typeof(ClassA), typeof(MyGenericClass))

我怎样才能实现它?


这是一个示例应用程序,基于 Darin Miritrov 的示例:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace ConsoleApplication1
{
    public class MyGenericClass<T> { }
    public class ClassB {}
    public class ClassA : MyGenericClass<ClassB> { }

    class Program
    {
        static void Main()
        {
            bool result = DerivesFrom(typeof(ClassA), typeof(MyGenericClass<>));
            Console.WriteLine(result); // prints **false**
        }

        private static bool DerivesFrom(Type rType, Type rDerivedType)
        {
            return rType.IsSubclassOf(rDerivedType);
        }
    }
}

I have the following utility routine which determine whether a type derives from a specific type:

private static bool DerivesFrom(Type rType, Type rDerivedType)
{
    while ((rType != null) && ((rType != rDerivedType)))
        rType = rType.BaseType;
    return (rType == rDerivedType);
}

(actually I don't know whether there is a more convenient way to test the derivation...)

The problem is I want to determine whether a type derives from a generic type, but without specify the generic arguments.

For example I can write:

DerivesFrom(typeof(ClassA), typeof(MyGenericClass<ClassB>))

but what I need is the following

DerivesFrom(typeof(ClassA), typeof(MyGenericClass))

How can I achieve it?


Based on the example of Darin Miritrov, this is a sample application:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace ConsoleApplication1
{
    public class MyGenericClass<T> { }
    public class ClassB {}
    public class ClassA : MyGenericClass<ClassB> { }

    class Program
    {
        static void Main()
        {
            bool result = DerivesFrom(typeof(ClassA), typeof(MyGenericClass<>));
            Console.WriteLine(result); // prints **false**
        }

        private static bool DerivesFrom(Type rType, Type rDerivedType)
        {
            return rType.IsSubclassOf(rDerivedType);
        }
    }
}

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

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

发布评论

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

评论(1

谎言月老 2024-10-14 02:02:35

您可以将通用参数保持打开状态:

DerivesFrom(typeof(ClassA), typeof(MyGenericClass<>));

应该可以工作。示例:

public class ClassA { }
public class MyGenericClass<T>: ClassA { }

class Program
{
    static void Main()
    {
        var result = DerivesFrom(typeof(MyGenericClass<>), typeof(ClassA));
        Console.WriteLine(result); // prints True
    }

    private static bool DerivesFrom(Type rType, Type rDerivedType)
    {
        return rType.IsSubclassOf(rDerivedType);
    }
}

另请注意 IsSubClassOf 方法的用法,该方法应该简化您的 DerivesFrom 方法并有点违背其目的。您还可以查看 IsAssignableFrom 方法。

You could leave the generic parameter open:

DerivesFrom(typeof(ClassA), typeof(MyGenericClass<>));

should work. Example:

public class ClassA { }
public class MyGenericClass<T>: ClassA { }

class Program
{
    static void Main()
    {
        var result = DerivesFrom(typeof(MyGenericClass<>), typeof(ClassA));
        Console.WriteLine(result); // prints True
    }

    private static bool DerivesFrom(Type rType, Type rDerivedType)
    {
        return rType.IsSubclassOf(rDerivedType);
    }
}

Also notice the usage of IsSubClassOf method which should simplify your DerivesFrom method and kind of defeat its purpose. There's also the IsAssignableFrom method you may take a look at.

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