2

I am trying to do something like this in my WPF application:

    ToolboxControl ctrl = new ToolboxControl();
    Assembly assembly = Assembly.LoadFile(file);
    var category = new ToolboxCategory(assembly.GetName().Name);
    foreach (Type t in assembly.GetTypes())
    {
        var wrapper = new ToolboxItemWrapper(t, t.Name);
        category.Add(wrapper);
    }
    ctrl.Categories.Add(category);

i.e. adding ToolboxItemWrappers for each type found in an assembly. However the last line throws the following exception (see image)

http://img41.imageshack.us/img41/2261/7xvqv.png http://img41.imageshack.us/img41/2261/7xvqv.png

All dependencies of the external assembly are also referenced in the main (WPF) application. So what's wrong here and how to fix it?

Louis Rhys
  • 34,517
  • 56
  • 153
  • 221

3 Answers3

2

You are using Assembly.LoadFile to load the assembly through reflection. However this method does not automatically find dependencies in the same directory. You should use Assembly.LoadFrom.

Also take in consideration that LoadFrom goes through Fusion allowing the load request to be redirected to another assembly while LoadFile loads exactly what you requested.

João Angelo
  • 56,552
  • 12
  • 145
  • 147
  • Thanks for you answer! What is fusion? – Louis Rhys Jan 04 '11 at 16:16
  • I tried changing `LoadFile` to `LoadFrom` and it still does not work. – Louis Rhys Jan 05 '11 at 02:20
  • Fusion is the assembly loader component of the CLR. Assuming the exception you get when CustomLibrary is requested goes through Fusion you can use [Fusion log viewer](http://msdn.microsoft.com/en-us/library/e74a18c4.aspx) to verify why the assembly fails to load. – João Angelo Jan 05 '11 at 12:05
1

Is the CustomLibrary assembly in the file? If not, hook to this event AppDomain.CurrentDomain.AssemblyResolve in you app, and load any other assemblies that the assembly at the filePath references to. It is required if CustomLibrary or other dlls are not in GAC.

Vijay Sirigiri
  • 4,653
  • 29
  • 31
  • I suggest you hook to the AssemblyResolve event and put a breakpoint. You would know what all Types are being referred to and which of the dependencies CLR is not able to find. – Vijay Sirigiri Jan 05 '11 at 14:57
  • You're right. See also [this answer](http://stackoverflow.com/questions/4600688/how-to-make-an-external-assembly-available-at-runtime) – Louis Rhys Jan 07 '11 at 09:51
0

Make sure "CustomLisbrary" is installed in GAC. Additionally, You may want to create design.dll and VisualStudio.design.dll.

Prince Ashitaka
  • 8,623
  • 12
  • 48
  • 71