gpt4 book ai didi

c# - 替换 Net Core 中的 AppDomain

转载 作者:行者123 更新时间:2023-11-30 15:19:06 25 4
gpt4 key购买 nike

所以,我是 C# 和 Net Core 的新手,有人将代码传递给了评估命令。但是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)));

最佳答案

.net core 中没有 AppDoamain 你可以使用 Microsoft.Extensions.DependencyModel 库来实现你需要的。

下面是来自 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 包才能使用上述代码。

关于c# - 替换 Net Core 中的 AppDomain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42596138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com