Load types from DLL in run time

0

I need to execute a method in an assembly loaded during runtime. The assemblies that I'm going to load are plugins that contain interface implementations.

This is the loading class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace Stub.Logic {
    public class DllReader {
        private static List<Type> connectionTypes = new List<Type>();

        public static void LoadConnectionTypes(string path) {
            DirectoryInfo dllDirectory = new DirectoryInfo(path);
            FileInfo[] dlls = dllDirectory.GetFiles("*.dll");
            foreach (FileInfo dllFileInfo in dlls) {
                Assembly assembly = Assembly.Load(dllFileInfo.FullName);
                connectionTypes.AddRange(assembly.GetTypes());
            }
        }

        //public static Connection GetConnection(string connectionTypeName) {
        //    return new Connection();
        //}
    }
}

I'm getting this error:

Could not load file or assembly '..\Plugins\MqConnection.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

This is the loaded assembly:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using Stub.Logic;

namespace MqConnection {

    public class MqConnection : Connection {
        // Stuff here...
    }
}

What am I doing wrong?

c#
reflection
asked on Stack Overflow Mar 24, 2013 by Yoav • edited Mar 24, 2013 by Maroun

1 Answer

3

Assembly.Load takes an Assembly name, not a file path. Use Assebmly.LoadFrom instead

answered on Stack Overflow Mar 24, 2013 by Andrei Schneider

User contributions licensed under CC BY-SA 3.0