2017-03-04 138 views
3

因此,我是C#和Net Core中的新成員,並且有人將我的代碼傳遞給評估命令。 但是,它不是在Net Core中製作的,所以AppDomain在Net Core中不存在,我該怎麼辦?替換Net Core中的AppDomain

ScriptOptions so = ScriptOptions.Default; 

      so = so.WithImports(new string[] { "System.Math", "System.Diagnostics", "System.Runtime.InteropServices", 
      "Discord.WebSocket", "Discord.Commands", "Discord", "System.Threading.Tasks", "System.Linq", "System", "System.Collections.Generic", "System.Convert", "System.Reflection"}); 
      so = so.WithReferences(AppDomain.CurrentDomain.GetAssemblies().Where(xa => !xa.IsDynamic && !string.IsNullOrWhiteSpace(xa.Location))); 

回答

4

有一個在.NET的核心,你可以使用Microsoft.Extensions.DependencyModel庫來實現你所需要的任何AppDoamain

以下是article中的代碼。你可以在那裏看到完整的細節。

return GetReferencingLibraries("Specify") 
.SelectMany(assembly => assembly.ExportedTypes); 

public static IEnumerable<Assembly> GetReferencingAssemblies(string assemblyName) 
{ 
    var assemblies = new List<Assembly>(); 
    var dependencies = DependencyContext.Default.RuntimeLibraries; 
    foreach (var library in dependencies) 
    { 
     if (IsCandidateLibrary(library, assemblyName)) 
     { 
      var assembly = Assembly.Load(new AssemblyName(library.Name)); 
      assemblies.Add(assembly); 
     } 
    } 
    return assemblies; 
} 

private static bool IsCandidateLibrary(RuntimeLibrary library, assemblyName) 
{ 
    return library.Name == (assemblyName) 
     || library.Dependencies.Any(d => d.Name.StartsWith(assemblyName)); 
} 

您將需要包括Microsoft.Extensions.DependencyModel軟件包以便使用上面的代碼。

+0

只是爲了澄清AppDomain並沒有完全消失,因爲[它是.Net Standard 2.0的一部分](https://stackoverflow.com/a/45600956/465053)。 – RBT