是否反映在C#
报价的方式来确定是否给予一些System.Type
款型的一些接口?
public interface IMyInterface {}
public class MyType : IMyInterface {}
// should yield 'true'
typeof(MyType)./* ????? */MODELS_INTERFACE(IMyInterface);
Run Code Online (Sandbox Code Playgroud) 我有以下情况:
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)
(请注意,这是我实际情况的简化,但重点应该相同......)