在bin的所有程序集中搜索接口

ryu*_*ice 4 .net c# asp.net

如何扫描bin目录中的所有程序集并检索实现接口的所有类型?

hun*_*ter 11

您可以使用Reflection和LINQ查询轻松找到它们

var type = typeof(IRyuDice);
var types = AppDomain.CurrentDomain.GetAssemblies().ToList()
    .SelectMany(a => a.GetTypes())
    .Where(t => type.IsAssignableFrom(t));
Run Code Online (Sandbox Code Playgroud)

AppDomain.CurrentDomain.GetAssemblies返回一个System.Reflection.Assembly[]集合.然后选择该程序集中的所有类型,并检查该类型是否使用了您的接口.

http://msdn.microsoft.com/en-us/library/system.appdomain.getassemblies.aspx

  • 我认为这不会回答这个问题,因为它只会处理CurrentDomain中已加载的程序集,而不会处理bin目录中尚未使用但尚未使用的程序集.这里有一个答案可能有所帮助:http://stackoverflow.com/questions/1288288/how-to-load-all-assemblies-from-within-your-bin-directory (11认同)