隐式类型与显式类型的比较 - C#
我在类型比较方面遇到了一个有趣的问题。我正在尝试将隐式类型与显式类型进行比较,以测试某些内容是否是任何类型的集合
var obField = value.GetType().InvokeMember(_stCollectionField,
System.Reflection.BindingFlags.GetProperty,
null, value, null);
if (obField.GetType() != typeof(IEnumerable<object>))
{
return true;
}
。在测试过程中,我可以确保 obField
将成为对象的集合。但是,我发现它始终会在检查内运行并返回 true,而我希望它跳过该检查(因为这两种类型是相等的。)
稍微调试一下就可以得到类型obField
作为对象 {System.Collections.Generic.List
。
我怎样才能匹配该类型?
谢谢
I've got an interesting issue with type comparison. I'm attempting to compare an implied type with an explicit type, to test if something is any sort of collection
var obField = value.GetType().InvokeMember(_stCollectionField,
System.Reflection.BindingFlags.GetProperty,
null, value, null);
if (obField.GetType() != typeof(IEnumerable<object>))
{
return true;
}
During my testing, I can ensure that obField
will turn out to be a collection of objects. However, I'm finding that it will always run inside the check and return true
, where instead I wish it to skip past that (becasue the two types are equal.)
A little debugging gives me the type of obField
as object {System.Collections.Generic.List<System.DateTime>}
.
How can I go about matching that type?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Type.IsAssignableFrom,如下所示:< a href="https://stackoverflow.com/questions/26733/getting-all-types-that-implement-an-interface-with-c-3-5">获取所有实现接口
例如:
Use Type.IsAssignableFrom, as used here: Getting all types that implement an interface
For example: