2016-07-29 25 views
2

アセンブリをプリロードするか、新しい実行可能アセンブリを起動するためのデーモンがあります。実行時にdotnetコアでアセンブリをロードする方法

Assembly assembly = Assembly.LoadFile(dllPath); 

をDLLを起動するには:アセンブリをプリロードする

AppDomain.CurrentDomain.ExecuteAssembly(executablePath, args); 

が、私はnetcoreapp1.0 APIでLoadFileがまたはのAppDomainクラスを見つけることができません。

dotnetコアに移植する方法は?


私はLoadFromAssemblyPathとAssemblyLoadContextを使用しようとしていました。 ですが、実行時に例外が発生します。ここ

はコードです:

using System; 
using System.Runtime.Loader; 
using System.Reflection; 
using System.IO; 

namespace Tizen.DotnetLauncher 
{ 
    class Shield 
    { 
    public static void Main(string[] args) 
    { 

     if (args.Length < 1) return; 

     var asl = new AssemblyLoader(); 
     string fpath = Path.GetFullPath(args[0]); 
     Console.WriteLine(fpath); 
     var asm = asl.LoadFromAssemblyPath(fpath); 

     Type[] types = null; 

     try 
     { 
      types = asm.GetTypes(); 
     } 
     catch (ReflectionTypeLoadException e) 
     { 
      foreach (Exception ex in e.LoaderExceptions) 
      { 
       Console.WriteLine(ex); 
      } 
     } 

     foreach (Type info in types) 
     { 
      Console.WriteLine(info.GetTypeInfo()); 
     } 
    } 

    class AssemblyLoader : AssemblyLoadContext 
    { 
     protected override Assembly Load(AssemblyName assemblyName) 
     { 
      return null; 
     } 
    } 
    } 
} 

LoaderExceptions出力:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. 

File name: 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileNotFoundException: Could not load the specified file. 
File name: 'System.Runtime' 
    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(AssemblyName assemblyName) 
    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName) 

例外

+1

[ネットコアコンソールアプリケーションのフォルダにあるアセンブリをロードする方法](http://stackoverflow.com/questions/37895278/how-to-load-assemblies-located-in-a-folder) -in-net-core-console-app) – Mat

答えて

1

この記事を見てみます

How to load assemblies located in a folder in .net core console app

.NETコアでは、AppDomainの概念はもう存在しません。

+1

リンク内にDependencyContextクラスが見つかりません。 LoadFromAssemblyPath()からAssemblyオブジェクトでGetTypes()を呼び出すと、 "System.IO.FileNotFoundException:ファイルまたはアセンブリをロードできませんでした。 'System.Runtime、Version = 4.1.1.0、Culture = neutral、PublicKeyToken = b03f5f7f11d50a3a '指定されたファイルが見つかりません。 –

+0

@piuslee Microsoft.Extensions.DependencyModelのnugetライブラリにあります。つまり、System.AppDomainのnugetパッケージからAppDomainを取得できます – Sinaesthetic

関連する問題