【发布时间】:2017-04-25 10:02:29
【问题描述】:
我想获取程序集中的类列表,作为输出,我想要一个 List[Interface] 而不是 List[string],因为“Interface”是从中继承我的程序集类的接口。我不知道我的问题是否有意义,但如果有人有答案,我将非常感谢。 我已经尝试过这个解决方案:List of classes in an assembly,但它提供了一个包含类名的列表 [字符串],所以它没有帮助,因为我需要从我的接口继承的类的列表。 谢谢大家,祝大家有个愉快的一天:)
作为我的问题的编辑,我使用了 activator.createinstance(type t) 来创建我的类的实例,所以这里是代码:
Assembly assembly = typeof(Interface1<double, double>).Assembly;
List<Interface> Classes = new List<Interface>();
foreach (Type type in assembly.GetExportedTypes())
{
var Attributes = type.GetCustomAttributes<FilterAttribute>();
//select the classes with attribute [Filter]
if (Attributes.Any())
{
TypeOfFilters.Add(type);
}
foreach (var i in TypeOfFilters)
{
var inst = Activator.CreateInstance(i);
Classes.Add((Interface) inst);
}
}
我收到错误“System.IO.FileNotFoundException:无法加载文件或程序集”
【问题讨论】:
-
but it didn't work for me!。好的。怎么样? -
这要么是您链接到的问题的欺骗,除非您指定它是如何不起作用的。或者它是题外话,因为您没有在帖子中提供所有相关信息。考虑添加minimal reproducible example,以便清楚您的问题是什么。您可以edit您的问题添加此信息。
-
“没有工作”是什么意思?你有例外吗?出乎意料的结果?你期待什么?请在遇到困难的地方展示您尝试过的方法。
-
它给了我一个列表
,这不是我要找的。span> -
@ABIR 你在找什么?
标签: c# .net class .net-assembly