Jesse Carter
Jesse Carter

Reputation: 21167

Is there a way I can safely check to see if an assembly CAN be loaded before I actually do so?

I am working on some software that will dynamically build menu items for certain dlls so that we can load components in dynamically based on what dlls are available on the users machine. Any dlls that I want to load have been flagged with an Assembly Attribute in the AssemblyInfo.cs file and how I determine whether or not I want to build a menu item for that dll. Here is my method so far:

private void GetReportModules() {
        foreach (string fileName in Directory.GetFiles(Directory.GetCurrentDirectory())) {
            if (Path.GetExtension(fileName) == ".dll" || Path.GetExtension(fileName) == ".exe") {
                System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(fileName);
                object[] attributes = assembly.GetCustomAttributes(typeof(ReportNameAttribute), false);
                if (attributes.Count() > 0) {
                    ReportNameAttribute reportNameAttribute = attributes[0] as ReportNameAttribute;
                    Type type = assembly.GetType(reportNameAttribute.BaseType);
                    MenuItem customReportsMenuItem = new MenuItem();
                    customReportsMenuItem.Header = reportNameAttribute.ReportName;
                    ReportsMenuItem.Items.Add(customReportsMenuItem);
                    customReportsMenuItem.Click += (s, ev) => {
                        var obj = Activator.CreateInstance(type);
                        type.InvokeMember("Show", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, obj, null);
                    };
                }
            }
        }
    }

For the most part its working fine, I am getting the dlls that I am expecting back out and am creating my menu items fine. The problem is that in order to check for the attribute I first need to load the assembly using Reflection. Some of the other local dlls are throwing errors when I try to load them about missing dependencies or he module was expected to contain an assembly manifest. Is there a way I can safely check to see if an assembly CAN be loaded before I actually do so? (sounds stupid as I write it out). Any thoughts on the problem I'm running into or better suggestions for how to accomplish what I'm trying here? Feeling a little bit in over my head.

Upvotes: 1

Views: 983

Answers (3)

fsimonazzi
fsimonazzi

Reputation: 3013

You can try the Unmanaged Metadata API (http://msdn.microsoft.com/en-us/library/ms404384.aspx) or the Common Compiler Infrastructure Metadata API (http://ccimetadata.codeplex.com/) as alternatives to plain reflection.

Upvotes: 1

Maarten
Maarten

Reputation: 22955

You can create a separate AppDomain, try to load the assemblies there, send the results back, and unload the AppDomain. This way you do not change your current AppDomain with 'garbage' of any loaded assemblies.

Upvotes: 2

Science_Fiction
Science_Fiction

Reputation: 3433

One way would be to make use of a try catch block. If it throw's an exception, you're not interested...

EDIT:

MSDN explains clearly the type of exceptions LoadFrom can throw. FileLoadException looks likely in your case.

I'm sure there is code out there that carried on after a catch. For example a logging framework. I would not want my framework to catch an exception and make my executable stop etc, i'd want it to smother the exception. My application should not fail just because a line of log miss fired.

Upvotes: 2

Related Questions