相关疑难解决方法(0)

如何确定类型是否实现了具有C#反射的接口

是否反映C#报价的方式来确定是否给予一些System.Type款型的一些接口?

public interface IMyInterface {}

public class MyType : IMyInterface {}

// should yield 'true'
typeof(MyType)./* ????? */MODELS_INTERFACE(IMyInterface);
Run Code Online (Sandbox Code Playgroud)

c# reflection interface

527
推荐指数
11
解决办法
20万
查看次数

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

我有以下情况:

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)

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

.net c# reflection

23
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×2

reflection ×2

.net ×1

interface ×1