java获取类实现的所有接口_获取实现接口的所有类型

使用反射,如何使用最少的代码获得使用C#3.0 / .NET 3.5实现接口的所有类型,并最大限度地减少迭代?

这就是我想要重写的内容:

foreach (Type t in this.GetType().Assembly.GetTypes())

if (t is IMyInterface)

; //do stuff

#1楼

这里的其他答案使用IsAssignableFrom 。 您还可以使用FindInterfaces从System命名空间,如所描述这里 。

下面是一个示例,它检查当前正在执行的程序集文件夹中的所有程序集,查找实现某个接口的类(为清楚起见,避免使用LINQ)。

static void Main() {

const string qualifiedInterfaceName = "Interfaces.IMyInterface";

var interfaceFilter = new TypeFilter(InterfaceFilter);

var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var di = new DirectoryInfo(path);

foreach (var file in di.GetFiles("*.dll")) {

try {

var nextAssembly = Assembly.ReflectionOnlyLoadFrom(file.FullName);

foreach (var type in nextAssembly.GetTypes()) {

var myInterfaces = type.FindInterfaces(interfaceFilter, qualifiedInterfaceName);

if (myInterfaces.Length > 0) {

// This class implements the interface

}

}

} catch (BadImageFormatException) {

// Not a .net assembly - ignore

}

}

}

public static bool InterfaceFilter(Type typeObj, Object criteriaObj) {

return typeObj.ToString() == criteriaObj.ToString();

}

如果要匹配多个接口,可以设置接口列表。

#2楼

这对我有用。 它循环遍历类并检查它们是否来自myInterface

foreach (Type mytype in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()

.Where(mytype => mytype .GetInterfaces().Contains(typeof(myInterface)))) {

//do stuff

}

#3楼

循环遍历所有加载的程序集,遍历所有类型,并检查它们是否实现了接口。

就像是:

Type ti = typeof(IYourInterface);

foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) {

foreach (Type t in asm.GetTypes()) {

if (ti.IsAssignableFrom(t)) {

// here's your type in t

}

}

}

#4楼

我的将是这个在c#3.0 :)

var type = typeof(IMyInterface);

var types = AppDomain.CurrentDomain.GetAssemblies()

.SelectMany(s => s.GetTypes())

.Where(p => type.IsAssignableFrom(p));

基本上,迭代次数最少的是:

loop assemblies

loop types

see if implemented.

#5楼

编辑:我刚刚看到编辑,以澄清原始问题是减少迭代/代码,这一切都很好,作为一个练习,但在现实世界的情况下,你将需要最快的实现,无论如何底层LINQ看起来有多酷。

这是我的Utils方法,用于迭代加载的类型。 它处理常规类和接口,如果您在自己的/第三方代码库中寻找实现,则excludeSystemTypes选项可以大大加快速度。

public static List GetSubclassesOf(this Type type, bool excludeSystemTypes) {

List list = new List();

IEnumerator enumerator = Thread.GetDomain().GetAssemblies().GetEnumerator();

while (enumerator.MoveNext()) {

try {

Type[] types = ((Assembly) enumerator.Current).GetTypes();

if (!excludeSystemTypes || (excludeSystemTypes && !((Assembly) enumerator.Current).FullName.StartsWith("System."))) {

IEnumerator enumerator2 = types.GetEnumerator();

while (enumerator2.MoveNext()) {

Type current = (Type) enumerator2.Current;

if (type.IsInterface) {

if (current.GetInterface(type.FullName) != null) {

list.Add(current);

}

} else if (current.IsSubclassOf(type)) {

list.Add(current);

}

}

}

} catch {

}

}

return list;

}

我承认,它不漂亮。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值