使用反射来找到实现的接口 [英] Using reflection to find interfaces implemented
问题描述
我有以下情况:
公共接口IPerson {..}
公共类人:IPerson { ..}
公共类用户:{人..}
现在;如果我有一个用户的对象 - 我该如何检查,如果这个实现使用反射IPerson?为了更精确我有一个对象可能有一个属性SomeUser的,这应该是某种类型的实现接口IPerson的。在我来说,我其实有一个用户,但是这是我想通过反射来检查什么。我无法弄清楚如何检查属性类型,因为它是一个用户,但我想检查它是否实现IPerson ...:
无功控制= _container.Resolve(OBJTYPE); //这里是OBJTYPE用户
VAR道具= viewType.GetProperty(指someUser);
如果((丙= NULL)及!及(prop.PropertyType是IPerson))
{..}
的(注意,这是我的实际案例的简化,但有一点应该是相同的......)的
< DIV CLASS =h2_lin>解决方案
检查的 Type.IsAssignableFrom
方法。
I have the following case:
public interface IPerson { .. }
public class Person : IPerson { .. }
public class User : Person { .. }
Now; if I have a "User" object - how can I check if this implements IPerson using reflection? To be more precise I have an object that might have a property SomeUser, which should be of some type implementing the interface "IPerson". In my case I actually have a User, but this is what I want to check through reflection. I can't figure out how to check the property type since it is a "User", but I want to check if it implements IPerson...:
var control = _container.Resolve(objType); // objType is User here
var prop = viewType.GetProperty("SomeUser");
if ((prop != null) && (prop.PropertyType is IPerson))
{ .. }
(Note that this is a simplification of my actual case, but the point should be the same...)
Check the Type.IsAssignableFrom
method.
这篇关于使用反射来找到实现的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!