简体   繁体   中英

How to check if a certain assembly exists?

I'm using the Activator to instantiate a new class based on the short name of an assembly (ea 'CustomModule'). It throws a FileNotFoundException , because the assembly isn't there. Is there a way to check if a certain assembly name is present?

I'm using the following code:

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

The main objective is to rather test for the presence of the assembly than to wait for the exception to occur.

If you'll notice my comment to your question it will be evident that I'm not rightly sure exactly how you want or need to go about this, but until we have a more elaborate description I can only offer you this in the hope it fits well to your situation (the key is in 'searching' the 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);             
}

.NET 2.0 Compatible 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 whether or not the file exists before trying to load it (a good practice) then, given you have its name and know the desired location, simply try to find the file when the assembly is being resolved:

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

I think it is better not to try to avoid the exception. The reason is that 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 pre-emptive multitasking OS that the assembly might be added/removed between the check and the actual creation. Yes, I realize this risk is minimal, but I still think the exception variant looks better because it is atomic.

缺少程序集肯定构成异常,尝试/捕获FileNotFoundException并根据您的逻辑处理该情况。

I hope it will help for someone in the future:

On every external .dll you are using, create its own uniqe key, like so:

string key = "fjrj3288skckfktk4owoxkvkfk4o29dic";

And then, when you load your form, for every single external .dll that you have got, simply check if the key is exists like so:

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

else // .dll does not exists or broken.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM