c# - Using reflection to get all classes of certain base type in dll -
I have a dll that contains a number of sections that all inherit from a CommandBase class. I am trying to use the reflection in all these sections (command, commandb, command c, etc.) in C # so that I can call a specific method on each one. Even I have so far: I have 2 issues I The type of expression given is never provided by CommandBase This is a method I use to load on an interface basis. And there is only one function that pulls back on base class Definitely you will need to modify the context to be loaded slightly. // Get the assemblies in the directory string folder = path.combin (http consort. Current server.mappath ("~ /") , "Bin"); Var file = directory. Gatefile (folder, "* .dll"); // Each assembly load foreach (string file in file) {var assembly = assembly.loadfile (file); If (if type.IsClass || type.IsNotPublic!) Is continued (assembly.FullName == "MyCommandProject") {foreach (type in the assembly.GetTypes), if (type is the commandbase) {var command = Activator Crete Instance (Type) commandbase; }}}}
Private Static List & lt; T & gt; GetInstances & LT; T & gt; () GetTypes () from Return (assembly) to Assembly.GetExecutingAssembly () where t.GetInterfaces () are included (typeof (t)) & amp; .. & Amp;! T.GetConstructor (Type.EmptyTypes) = Select Null (T) catalyst. Create intine (t)). ToList (); }
Private static IList & Lt; T & gt; GetInstances & lt; T & gt; () T In the assembly from return GetExecutingAssembly () GetTypes () where t.BaseType == (typeof (T)) & amp; Amp; T. GETConstructor (Type.EmptyTypes)! = Null select (t) Activator.CreateInstance (T)) ToList () .; }
Comments
Post a Comment