4

私が使用する場合

Assembly assembly = Assembly.LoadFrom(file);

その後、ファイルを使用しようとすると、ファイルが使用中であることを示す例外が発生します。

新しいappdomainにロードする必要があります。

私が見つけたように見えるのは、アセンブリ内でインスタンスを作成する方法の例です 。アセンブリ全体をロードする方法はありますか。

私が必要なのは:

 (1) load the assembly into a new AppDomain from a file . 
 (2) extract an embedded  resource (xml file) from the Dll .
 (3) extract a type of class which implements an interface (which i know the interface type) .
 (4) unload the entire appdomain in order to free the file .  

2-4は問題ありません

アセンブリを新しいAppDominにロードする方法が見つからないようです。インスタンスの作成の例だけで、DLL内のwithからクラスのインスタンスを取得できます。

全部必要です。

この質問のように:インスタンスの作成の別の例。

DLLを別のAppDomainにロードする

4

1 に答える 1

2

最も基本的なマルチドメインシナリオは

static void Main()
{
    AppDomain newDomain = AppDomain.CreateDomain("New Domain");
    newDomain.ExecuteAssembly("file.exe");
    AppDomain.Unload(newDomain);
}

個別のドメインを呼び出すExecuteAssemblyことは便利ですが、ドメイン自体と対話する機能は提供しません。また、ターゲットアセンブリが実行可能ファイルである必要があり、呼び出し元を単一のエントリポイントに強制します。柔軟性を組み込むために、文字列または引数を.exeに渡すこともできます。

これがお役に立てば幸いです。

拡張機能:次のようなものを試してください

AppDomainSetup setup = new AppDomainSetup();
setup.AppDomainInitializer = new AppDomainInitializer(ConfigureAppDomain);
setup.AppDomainInitializerArguments = new string[] { unknownAppPath };
AppDomain testDomain = AppDomain.CreateDomain("test", AppDomain.CurrentDomain.Evidence, setup);
AppDomain.Unload(testDomain);
File.Delete(unknownAppPath);

ここで、は次のAppDomainように初期化できます

public static void ConfigureAppDomain(string[] args)
{
    string unknownAppPath = args[0];
    AppDomain.CurrentDomain.DoCallBack(delegate()
    {
        //check that the new assembly is signed with the same public key
        Assembly unknownAsm = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(unknownAppPath));

        //get the new assembly public key
        byte[] unknownKeyBytes = unknownAsm.GetName().GetPublicKey();
        string unknownKeyStr = BitConverter.ToString(unknownKeyBytes);

        //get the current public key
        Assembly asm = Assembly.GetExecutingAssembly();
        AssemblyName aname = asm.GetName();
        byte[] pubKey = aname.GetPublicKey();
        string hexKeyStr = BitConverter.ToString(pubKey);
        if (hexKeyStr == unknownKeyStr)
        {
            //keys match so execute a method
            Type classType = unknownAsm.GetType("namespace.classname");
            classType.InvokeMember("MethodNameToInvoke", BindingFlags.InvokeMethod, null, null, null);
        }
    });
}
于 2012-04-25T13:00:52.033 に答える