I am trying to dynamically load an encryption assembly but my GetType
is returning null, even though I am using the correct class name. Here's the code:
//Load encryption assembly.
Assembly encryptionAssembly = Assembly.LoadFrom("Encryption.dll");
foreach(Type t in encryptionAssembly.GetTypes())
{
MessageBox.Show(t.Name.ToString());
// This shows "Encryption"
}
Type encryptionClass = encryptionAssembly.GetType("Encryption");
// But this returns null
I've got a bit of a headache with this one. The class is public and I've definitely spelled it correctly.
Thanks in advance.
Here
MessageBox.Show(t.FullName.ToString()); //FULLNAME
print out the FullName
of the type and after use that FullName
to get the type from the assembly.
I didn't know the the fully qualified name of my assembly at runtime, but I was able to get the type using only the class name from the assembly's ExportedTypes. For instance, to get an export type of "ClassName":
var assembly = Assembly.LoadFile(dllPath);
var types = assembly.ExportedTypes;
if(types != null)
{
var type = types.Where(x => x.Name == "ClassName").FirstOrDefault();
}
If there were multiple types with the identical name of "ClassName" this would only get the first one, but this is a safe solution for the dlls I'm loading.
You should specify a full namespace of a type, for example:
encryptionAssembly.GetType("My.Namespace.Encryption")
You can know it using t.FullName
try specifying the full name of the Encryption type (namespace.classname)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With