使用反射来查找实现的接口

sti*_*k81 23 .net c# reflection

我有以下情况:

public interface IPerson { .. }    
public class Person : IPerson { .. }    
public class User : Person { .. }
Run Code Online (Sandbox Code Playgroud)

现在; 如果我有一个"用户"对象 - 我如何检查这是否使用反射实现IPerson?更确切地说,我有一个可能具有属性SomeUser的对象,它应该是某种类型,实现接口"IPerson".在我的情况下,我实际上有一个用户,但这是我想通过反思检查.我无法弄清楚如何检查属性类型,因为它是一个"用户",但我想检查它是否实现了IPerson ...:

var control = _container.Resolve(objType); // objType is User here
var prop = viewType.GetProperty("SomeUser");
if ((prop != null) && (prop.PropertyType is IPerson)) 
{ .. }
Run Code Online (Sandbox Code Playgroud)

(请注意,这是我实际情况的简化,但重点应该相同......)

Ste*_*ger 14

var control = _container.Resolve(objType); 
var prop = viewType.GetProperty("SomeUser");
if ((prop != null) && (prop.PropertyType.GetInterfaces().Contains(typeof(IPerson))) 
{ .. }
Run Code Online (Sandbox Code Playgroud)