如何测试反射字段的类型是否是指定类型或继承自指定类型
我正在迭代类的 FieldInfo
。我希望能够测试给定字段是否属于某种类型。
具体问题是我想知道从 SortedList
派生的所有字段。所以它们并不完全是 SortedList
,但每一个都是一个 SortedList
。
给定字段的 FieldInfo
,我如何测试它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
IsAssignableFrom
方法来执行此测试,如下所示:You can use
IsAssignableFrom
method to perform this test, like this:除了查找 SortedList 作为类型之外,您还可以查找 IDictionary、ICollection 等接口,其中 SortedList 源自。我在我的博客上提供了一篇有趣的读物,其中给出了接口反射的示例:
Reflect Interface from Unknown Assembly in C#< /a>
HTH(来自 HR 南边;-))
Instead of looking for the SortedList as the type, you can also look for interfaces such as IDictionary, ICollection which SortedList derives from. I provide an interesting read on my blog which gives an example of reflecting for an interface:
Reflect Interface from Unknown Assembly in C#
HTH (From HR down south of you ;-) )
这段代码未经测试,但大致是我之前使用过的。
This code is untested but is roughly what I have used before.