简体   繁体   English

在 c# 中,确定由 object 在运行时实现的接口

[英]In c#, determine interfaces implemented by an object at runtime

I know there are ways to determine at runtime if an object implements a particular interface.我知道有一些方法可以在运行时确定 object 是否实现了特定接口。 What I'm wondering is if there's a way to determine at runtime all of the interfaces an object implements without looking for any one interface in particular.我想知道是否有一种方法可以在运行时确定 object 实现的所有接口,而无需特别查找任何一个接口。

I'd really rather not have to check for each interface one by one - even using asynchronous processing that seems clunky and inelegant to me.我真的宁愿不必逐个检查每个接口——即使使用对我来说似乎笨拙和不雅的异步处理。

I'm currently looking into reflection, but if there is a less resource-intensive, in-language way to do it I'd prefer that.我目前正在考虑反思,但如果有一种资源密集度较低的语言方式来做到这一点,我更愿意这样做。

Any ideas?有任何想法吗?

Type.GetInterfaces will return you the types of the interfaces implemented by the type of an object. Type.GetInterfaces将返回由 object 类型实现的接口类型。 For example:例如:

var interfaces = someObject.GetType().GetInterfaces();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM