How to check if a specific assembly exists?

I use Activator to create a new class based on the assembly short name (for example, "CustomModule"). It throws a FileNotFoundException because the assembly is not there. Is there a way to check if a specific assembly name is present?

I am using the following code:

 System.Runtime.Remoting.ObjectHandle obj = System.Activator.CreateInstance(assemblyName, className); 

The main goal is to check for an assembly rather than wait for an exception.

+8
reflection c # activator assemblies
source share
4 answers

If you notice my comment on your question, it will be obvious that I do not know exactly how you want or need to do this, but until we have a more detailed description, I can only offer you this in the hope that it fits well for your situation (the key is in the "search" of assemblies):

 var className = "System.Boolean"; var assemblyName = "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var assembly = (from a in assemblies where a.FullName == assemblyName select a).SingleOrDefault(); if (assembly != null) { System.Runtime.Remoting.ObjectHandle obj = System.Activator.CreateInstance(assemblyName, className); } 

Compatible .NET 2.0 Code

 Assembly assembly = null; var className = "System.Boolean"; var assemblyName = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) { if (a.FullName == assemblyName) { assembly = a; break; } } if (assembly != null) { System.Runtime.Remoting.ObjectHandle obj = System.Activator.CreateInstance(assemblyName, className); } 

If you want to determine if a file exists before downloading (good practice), then, given that you have its name and you know the desired location, just try to find the file with the assembly permission:

 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); var className = "StackOverflowLib.Class1"; var assemblyName = "StackOverflowLib.dll"; var currentAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var obj = Activator.CreateInstance(Path.Combine(currentAssemblyPath, assemblyName), className); static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { var currentAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (File.Exists(Path.Combine(currentAssemblyPath, args.Name))) { return Assembly.LoadFile(Path.Combine(currentAssemblyPath, args.Name)); } return null; } 
+8
source share

I think it’s better not to try to avoid an exception. The reason is because if you have code like

 if (DoesAssemblyExist(asmName)) { object = Activator.CreateInstance(typeName); } else { MessageBox.Show("Assembly does not exist"); } 

There is always a risk in a proactive multi-tasking OS that an assembly may be added / removed between validation and actual creation. Yes, I understand that this risk is minimal, but I still think that the option of exclusion looks better, because it is atomic.

+2
source share

The missing assembly is definitely an exception, try / catch FileNotFoundException and handles the situation according to your logic.

+1
source share

I hope this helps someone in the future:

On each external .dll that you use, create your own uniqe key, for example:

string key = "fjrj3288skckfktk4owoxkvkfk4o29dic";

And then, when you upload your form, for each separate external DLL you received, just check if such a key exists:

If (isMyLib.Variables.key == key) // continue

yet //.dll does not exist or does not work.

-one
source share

All Articles