使用reflection在dll中获取某些基类型的所有类

我有一个dll,它包含许多都inheritance自CommandBase类的类。 我正在尝试使用C#中的reflection来获取所有这些类(CommandA,CommandB,CommandC等)的实例,以便我可以在每个类上调用特定方法。 这是我到目前为止:

//get assemblies in directory. string folder = Path.Combine(HttpContext.Current.Server.MapPath("~/"), "bin"); var files = Directory.GetFiles(folder, "*.dll"); //load each assembly. foreach (string file in files) { var assembly = Assembly.LoadFile(file); if (assembly.FullName == "MyCommandProject") { foreach (var type in assembly.GetTypes()) { if (!type.IsClass || type.IsNotPublic) continue; if(type is CommandBase) { var command = Activator.CreateInstance(type) as CommandBase; } } } } 

我有2个问题。 第一个问题是“if(type is CommandBase”)行给出以下警告:

给定的表达式永远不是提供的类型CommandBase。

第二个问题是我无法弄清楚如何获取实际对象的实例(CommandA,CommandB等…),仅将其转换为CommandBase是不够的。

这是我用于基于接口加载的方法。

 private static List GetInstances() { return (from t in Assembly.GetExecutingAssembly().GetTypes() where t.GetInterfaces().Contains(typeof (T)) && t.GetConstructor(Type.EmptyTypes) != null select (T) Activator.CreateInstance(t)).ToList(); } 

这是基于基类拉回的相同function。

 private static IList GetInstances() { return (from t in Assembly.GetExecutingAssembly().GetTypes() where t.BaseType == (typeof(T)) && t.GetConstructor(Type.EmptyTypes) != null select (T)Activator.CreateInstance(t)).ToList(); } 

当然,需要稍微修改以指向您正在加载的引用。

更改type is CommandBasetypeof(CommandBase).IsAssignableFrom(type)

你必须改变

 if(type is CommandBase) 

 if(type.IsSubclassOf(typeof(CommandBase))) 

如果IsSubclassOf与IsAssignableFrom相反。 也就是说,如果t1.IsSubclassOf(t2)为真,则t2.IsAssignableFrom(t1)也为真。

那是因为你的type变量是Type ,而不是CommandBase

你要

 if(type == typeof(CommandBase)) 

(感谢格雷格的纠正)