c# - Using reflection to get all classes of certain base type in dll -


"itemprop =" text "> after

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:

// 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; }}}}

I have 2 issues I

The type of expression given is never provided by CommandBase

<: 1 issue is that the line "(if type commandbase") gives following warning. p> 2 issue is that I do not understand how to get an example of how the real object (Commanda, CommandB, etc ...), is not enough just to convert it to CommandBase.

This is a method I use to load on an interface basis.

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 (); }

And there is only one function that pulls back on base class

  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 () .; }  

Definitely you will need to modify the context to be loaded slightly.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -