vendredi 10 mai 2019

How to load Type from assembly that is in referenced but using in file is not used

  • I am creating a middleware service to run jobs.
  • I will have a DB full of jobs that run.
  • I want to use reflection to get the concrete classes that will do the jobs so that I don't have to hard code each job into a massive switch statement and instantiate them.
  • The class that does the job is in a different project from the class that is trying to call it.
  • I build up the FulllyQuailifiedAssemblyName with bits of information to get the type.
  • The project that tries to call the job reference to the project the job is in.
  • All projects are in .Net Framework 4.7.2 and are just normal libraries.

I know everything is okay Reference wise and versions of .Net etc. As if I do

var assemblyname = typeof(Jobclass).AssemblyQualifiedName;

And then plug that into.

Type.GetType(assemblyname);

The type that comes out is correct.

It seems that only if I reference the class directly in the code, then the using is then being used and then the type is loaded.

but if I just try to do it using the string i get null out of the type.

I believe this is the correct behaviour as unused using are just ignored while compiling etc.

Here is my code to try and get the type of from the AssemblyQualifiedName

Using statement

using Middleware.Services.Thing;

Trying to get Type from class in different assembly / project

 var typeName = GenerateTypeName(job.Service);

 var type = Type.GetType(typeName);

 if (type == null)
 {
     throw new InvalidOperationException("File can not be found for job");
 }

Is there any way to explicitly load the using so I can get the type??





Aucun commentaire:

Enregistrer un commentaire