Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to getType from a reference dll when copylocal property is false

I need to make something like:

Type CustomType = Type.GetType("instanceName");

It always returns null. instanceName is a string which represents a type contained in a dll added to References (with copyLocal property set to false).

I also tried:

Type CustomType = Type.GetType("instanceName, dllFile.dll");

But also returns null.

Thanks a lot

Alex

like image 886
pasapepe Avatar asked Jan 16 '23 06:01

pasapepe


2 Answers

If the assembly is already loaded you could try this:

Type customType = Type.GetType("namespace.typename, assembly");
like image 80
Darin Dimitrov Avatar answered Jan 31 '23 00:01

Darin Dimitrov


If you are not deploying the assembly to the GAC and the CopyLocal setting is set to false, then where are you planning to load the assembly from?

If you are planning to deploy the assemblies to a fixed location on a drive, you can use Assembly.LoadFrom:

var assembly = Assembly.LoadFrom(@"C:\Path\To\Assembly.dll");
var type = assembly.GetType("InstanceName");

This allows you to load an absolute assembly. If you are using Type.GetType, it uses the standard fusion assembly loading rules to attempt to find a matching assemby (but if it is not GAC'd or CopyLocal = true), then it won't be deployed with your output, and GetType will return null.

Also, if you do not specify an assembly name in the type name, e.g. instanceName, assemblyName, instead of instanceName, the I believe only the currently executing assembly is checked.

like image 33
Matthew Abbott Avatar answered Jan 31 '23 01:01

Matthew Abbott