使用泛型时如何比较类型?

发布于 2024-07-29 18:22:47 字数 1612 浏览 1 评论 0原文

我试图在运行时派生对象的类型。 具体来说,我需要知道两件事:它是实现 ICollection 还是 IDto。 目前我能找到的唯一解决方案是:

   private static bool IsACollection(PropertyDescriptor descriptor)
    {
        bool isCollection = false;

        foreach (Type type in descriptor.PropertyType.GetInterfaces())
        {
            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(ICollection<>))
                {
                    isCollection = true;
                    break;
                }
            }
            else
            {
                if (type == typeof(ICollection))
                {
                    isCollection = true;
                    break;
                }
            }
        }


        return isCollection;
    }

    private static bool IsADto(PropertyDescriptor descriptor)
    {
        bool isDto = false;

        foreach (Type type in descriptor.PropertyType.GetInterfaces())
        {
            if (type == typeof(IDto))
            {
                isDto = true;
                break;
            }
        }          
        return isDto;
    }

但是我相信一定有比这更好的方法。 我尝试以正常方式进行比较,例如:

if(descriptor.PropertyType == typeof(ICollection<>))

但是,在使用反射时会失败,但在不使用反射时它工作正常。

我不想迭代实体的每个字段的接口。 有人可以阐明另一种方法来做到这一点吗? 是的,我过早地优化了,但它看起来也很难看,所以请幽默我。

注意事项:

  1. 它可以是通用的,也可以不是通用的,例如 IList<> 。 或者只是 ArrayList 因此我正在寻找 ICollection 或 ICollection<> 。 所以我假设我应该在 if 语句中使用 IsGenericType 来知道是否使用 ICollection<> 进行测试。 或不。

提前致谢!

I am trying to derive the type of an object at runtime. Specifically I need to know two things whether it implements ICollection or IDto. Currently my only solution I have been able to find is this:

   private static bool IsACollection(PropertyDescriptor descriptor)
    {
        bool isCollection = false;

        foreach (Type type in descriptor.PropertyType.GetInterfaces())
        {
            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(ICollection<>))
                {
                    isCollection = true;
                    break;
                }
            }
            else
            {
                if (type == typeof(ICollection))
                {
                    isCollection = true;
                    break;
                }
            }
        }


        return isCollection;
    }

    private static bool IsADto(PropertyDescriptor descriptor)
    {
        bool isDto = false;

        foreach (Type type in descriptor.PropertyType.GetInterfaces())
        {
            if (type == typeof(IDto))
            {
                isDto = true;
                break;
            }
        }          
        return isDto;
    }

However I am convinced there has to be a better way than this. I have tried comparing in a normal fashion such as:

if(descriptor.PropertyType == typeof(ICollection<>))

However, this fails when using reflection yet when not using reflection it works fine.

I don't want to iterate through the interfaces for every field of my entity. Could someone shed some light on another method in which to do this? Yes, I am prematurely optimizing, but it looks ugly too so please humor me.

Caveats:

  1. It could or could not be generic, such as IList<> or just ArrayList thus why I am looking for ICollection or ICollection<>. So I assume I should use IsGenericType in an if statement to know whether to test using ICollection<> or not.

Thanks in advance!

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

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

发布评论

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

评论(2

爱她像谁 2024-08-05 18:22:47

这:

type == typeof(ICollection)

将检查属性的类型是否完全 ICollection。 也就是说,它将返回 true for:

public ICollection<int> x { get; set; }

但不会返回 true:

public List<int> x { get; set; }

如果您想检查属性的类型是否是,或派生自ICollection,最简单的方法是使用 Type.IsAssignableFrom:

typeof(ICollection).IsAssignableFrom(type)

泛型也是如此:

typeof(ICollection<>).IsAssignableFrom(type.GetGenericTypeDefinition())

This:

type == typeof(ICollection)

will check if the type of property is exactly ICollection. That is, it will return true for:

public ICollection<int> x { get; set; }

but not for:

public List<int> x { get; set; }

If you want to check if the type of property is, or is derived from, ICollection, the simplest way is to use Type.IsAssignableFrom:

typeof(ICollection).IsAssignableFrom(type)

and the same goes for generic:

typeof(ICollection<>).IsAssignableFrom(type.GetGenericTypeDefinition())
揽月 2024-08-05 18:22:47

type.IsAssignable 在这种情况下有帮助吗?

编辑:抱歉,它应该是 Type.IsAssignableFrom

Does type.IsAssignable help in this case?

EDIT: Sorry, it should have been Type.IsAssignableFrom

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