gpt4 book ai didi

c# - 确定哪个 .net 程序集加载了另一个

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:29 27 4
gpt4 key购买 nike

我正在订阅 AppDomain.AssemblyLoaded 事件,并希望知道请求的(当前正在加载的)程序集是从哪个程序集加载的。

如果程序集 A 包含对 Assembly.Load(AssemblyB) 的调用,我想在运行时确定是程序集 A 导致加载程序集 B。

最佳答案

这将为您提供引用程序集,这有点接近。我不确定是否可以获得更多详细信息。

static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(MyAssemblyLoadEventHandler);
System.Data.DataColumn d = new System.Data.DataColumn();
Console.ReadLine();
}
static void MyAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args)
{
Console.WriteLine("ASSEMBLY LOADED: " + args.LoadedAssembly.FullName);
string loadedAssemblyFullName = args.LoadedAssembly.FullName;
foreach (System.Reflection.Assembly parent in AppDomain.CurrentDomain.GetAssemblies())
{
System.Reflection.AssemblyName[] referencedAssemblies = parent.GetReferencedAssemblies();
string[] referencedFullNames = (from r in referencedAssemblies select r.FullName).ToArray();
if (referencedFullNames.Contains(loadedAssemblyFullName))
{
Console.WriteLine(System.IO.Path.GetFileName(args.LoadedAssembly.CodeBase) +
" was referenced by " +
System.IO.Path.GetFileName(parent.CodeBase));
}
}
}

输出:

ASSEMBLY LOADED: System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

System.Numerics.dll was referenced by System.Core.dll

System.Numerics.dll was referenced by System.Data.dll

代码来自:https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getreferencedassemblies(v=vs.110).aspx

(编辑删除了之前无用的答案)

关于c# - 确定哪个 .net 程序集加载了另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37198739/

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