使用泛型时如何比较类型?
我试图在运行时派生对象的类型。 具体来说,我需要知道两件事:它是实现 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<>))
但是,在使用反射时会失败,但在不使用反射时它工作正常。
我不想迭代实体的每个字段的接口。 有人可以阐明另一种方法来做到这一点吗? 是的,我过早地优化了,但它看起来也很难看,所以请幽默我。
注意事项:
- 它可以是通用的,也可以不是通用的,例如 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:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这:
将检查属性的类型是否完全
ICollection
。 也就是说,它将返回 true for:但不会返回 true:
如果您想检查属性的类型是否是,或派生自,
ICollection
,最简单的方法是使用Type.IsAssignableFrom
:泛型也是如此:
This:
will check if the type of property is exactly
ICollection
. That is, it will return true for:but not for:
If you want to check if the type of property is, or is derived from,
ICollection
, the simplest way is to useType.IsAssignableFrom
:and the same goes for generic:
type.IsAssignable
在这种情况下有帮助吗?编辑:抱歉,它应该是
Type.IsAssignableFrom
Does
type.IsAssignable
help in this case?EDIT: Sorry, it should have been
Type.IsAssignableFrom