3

別のドメインに .dll(プラグイン) をロードする必要があります。メインアプリでは、プラグインのタイプについては何も知りませんが、いくつかのメソッドで共通インターフェイス ICommonInterface を実装しているだけです。インターフェイス型のインスタンスを作成できないため、このコードは役に立ちません。

AppDomain domain = AppDomain.CreateDomain("New domain name");
//Do other things to the domain like set the security policy

string pathToDll = @"C:\myDll.dll"; //Full path to dll you want to load
Type t = typeof(TypeIWantToLoad);
TypeIWantToLoad myObject = (TypeIWantToLoad)domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName);

私の質問は、作成したいタイプを実装するインターフェイス名しか知らない場合、新しいドメインにアセンブリをロードしてインスタンスを取得する方法です。

更新: これが私のコードです: MainLib.dll

namespace MainLib
{
public interface ICommonInterface
{
    void ShowDllName();
}
}

PluginWithOutException.dll

namespace PluginWithOutException
{
public class WithOutException : MarshalByRefObject, ICommonInterface
{
    public void ShowDllName()
    {
        Console.WriteLine("PluginWithOutException");
    }
}
}

PluginWithException.dll

namespace PluginWithException
{
public class WithException : MarshalByRefObject, ICommonInterface
{
    public void ShowDllName()
    {
        Console.WriteLine("WithException");
        throw new NotImplementedException();
    }
}
}

そして主なアプリケーション:

        static void Main(string[] args)
    {
        string path = @"E:\Plugins\";
        string[] assemblies = Directory.GetFiles(path);

        List<string> plugins = SearchPlugins(assemblies);

        foreach (string item in plugins)
        {
            CreateDomainAndLoadAssebly(item);
        }

        Console.ReadKey();
    }

    public static List<string> SearchPlugins(string[] names)
    {
        AppDomain domain = AppDomain.CreateDomain("tmpDomain");
        domain.Load(Assembly.LoadFrom(@"E:\Plugins\MainLib.dll").FullName);
        List<string> plugins = new List<string>();

        foreach (string asm in names)
        {
            Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);

            var theClassTypes = from t in loadedAssembly.GetTypes()
                                where t.IsClass &&
                                      (t.GetInterface("ICommonInterface") != null)
                                select t;
            if (theClassTypes.Count() > 0)
            {
                plugins.Add(asm);
            }
        }
        AppDomain.Unload(domain);
        return plugins;
    }

プラグインとメイン アプリには、MainLib.dll への参照があります。主な目的は、アセンブリを既定のドメインにロードするのではなく、別のドメインにロードすることです。したがって、アセンブリが必要ない場合は、Unload() ドメインを使用してアプリケーションからすべてのプラグインをアンロードします。

今のところ、例外はFileNotFoundException, Could not load file or assembly 'PluginWithException, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.)文字列にありますAssembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);(PluginWithException という名前のプラグインを読み込もうとしています)。システムを除くプラグインのすべての依存関係を削除しました。このドメインに System.dll をロードしました (正しくロードされ、ドメイン内にあります)。それでもプラグインをドメインにロードできません。また、PluginWithException には 2 つの依存関係 (mscorlib と MainLib) があり、それらすべてがこのドメインに読み込まれていることも確認しました。

更新:ここで、この質問に詳細を尋ねました。

4

2 に答える 2

3

それがあなたにとって必要なものかどうかわかりませんが、私はあなたを助けようとします. これは、プラグイン アセンブリをロードする方法です。ヘルパー クラスを使用して、新しい AppDomain とそのアセンブリのクラスのインスタンスを管理します。これはヘルパー クラスです。

[Serializable, ClassInterface(ClassInterfaceType.AutoDual)]
class helperDomain<T>: MarshalByRefObject where T: class
{
    #region private
    private AppDomain _app_domain;
    private AppDomainSetup _app_domain_info;

    private string _assembly_class_name;
    private string _assembly_file;
    private string _assembly_file_name;
    private T _inner_class;
    private bool _load_ok;
    private string _loading_errors;
    private string _path;
    #endregion

    #region .ctor
    public helperDomain(string AssemblyFile, 
       string configFile = null, string domainName)
    {
        this._load_ok = false;
        try
        {
            this._assembly_file = AssemblyFile; //full path to assembly
            this._assembly_file_name = System.IO.Path.GetFileName(this._assembly_file); //assmbly file name
            this._path = System.IO.Path.GetDirectoryName(this._assembly_file); //get root directory from assembly path 
            this._assembly_class_name = typeof(T).ToString(); //the class name to instantiate in the domain from the assembly
            //start to configure domain
            this._app_domain_info = new AppDomainSetup();
            this._app_domain_info.ApplicationBase = this._path;
            this._app_domain_info.PrivateBinPath = this._path;
            this._app_domain_info.PrivateBinPathProbe = this._path;
            if (!string.IsNullOrEmpty(configFile))
            {
                this._app_domain_info.ConfigurationFile = configFile;
            }
            //lets create the domain
            this._app_domain = AppDomain.CreateDomain(domainName, null, this._app_domain_info);
            //instantiate the class
            this._inner_class = (T) this._app_domain.CreateInstanceFromAndUnwrap(this._assembly_file, this._assembly_class_name);
            this._load_ok = true;
        }
        catch (Exception exception)
        {
            //There was a problema setting up the new appDomain
            this._load_ok = false;
            this._loading_errors = exception.ToString();
        }
    }
    #endregion


    #region public properties
    public string AssemblyFile
    {
        get
        {
            return _assembly_file;
        }
    }

    public string AssemblyFileName
    {
        get
        {
            return _assembly_file_name;
        }
    }

    public AppDomain AtomicAppDomain
    {
        get
        {
            return _app_domain;
        }
    }

    public T InstancedObject
    {
        get
        {
            return _inner_class;
        }
    }

    public string LoadingErrors
    {
        get
        {
            return _loading_errors;
        }
    }

    public bool LoadOK
    {
        get
        {
            return _load_ok;
        }
    }

    public string Path
    {
        get
        {
            return _path;
        }
    }
    #endregion
}

次に、プラグインをロードします(それぞれ異なるフォルダーにあります)。

foreach(string pluginassemblypath in pluginspaths)
{ 
    //Each pluginassemblypath (as it says..) is the full path to the assembly
    helperDomain<IPluginClass> isoDomain = 
        helperDomain<IPluginClass>(pluginassemblypath, 
             pluginassemblypath + ".config", 
             System.IO.Path.GetFileName(pluginassemblypath) + ".domain");
    if (isoDomain.LoadOK)
    {
       //We can access instance of the class (.InstancedObject)
       Console.WriteLine("Plugin loaded..." + isoDomain.InstancedObject.GetType().Name);
    }
    else
    {
       //Something happened...
       Console.WriteLine("There was en error loading plugin " + 
            pluginassemblypath + " - " + helperDomain.LoadingErrors);
    }
}

それがあなたを助けることを願っています...

于 2013-06-11T11:40:53.610 に答える
1

この質問は、あなたがやりたいことに関連しているようです。

すべての参照を再帰的に使用してアセンブリをAppDomainにロードする方法は?

アセンブリを読み込んだ後、Assembly.GetTypes()と を繰り返し使用して、インターフェイスを実装する型を見つけることができます。

于 2013-05-03T15:49:04.990 に答える