我如何从GAC加载程序集?

时间:2021-01-09 02:46:25

I'm trying to use Assembly.Load() to load an assembly that is in the GAC. For example, say I want to list out all of the types that exist in PresentationCore.dll, how would I go about loading PresentationCore.dll?

我正在尝试使用Assembly.Load()来加载GAC中的程序集。例如,假设我要列出PresentationCore.dll中存在的所有类型,我将如何加载PresentationCore.dll?

When I try this:

当我尝试这个:

Assembly a = Assembly.Load("PresentationCore.dll");

I get a FileNotFoundException. Another answer on SO suggested I used Assembly.LoadFrom() to accomplish this - I'm hesitant to do that because Assembly.LoadFrom() is deprecated, according to Visual Studio 2008 - plus, it doesn't seem to actually work.

我得到一个FileNotFoundException。关于SO的另一个答案建议我使用Assembly.LoadFrom()来完成这个 - 我很犹豫,因为根据Visual Studio 2008,不推荐使用Assembly.LoadFrom() - 而且它似乎并没有真正起作用。

Any ideas?

5 个解决方案

#1


If the assembly is in the GAC you must load it via its fully qualified name.

如果程序集在GAC中,则必须通过其完全限定名称加载它。

For example, if I wanted to load mscorlib.dll I would do something like this:

例如,如果我想加载mscorlib.dll,我会这样做:

Assembly a = Assembly.Load
    ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

The easiest way to determine an assembly's fully qualified name is to load the assembly in Reflector and use the Name field from the lower display pane like this:

确定程序集的完全限定名称的最简单方法是在Reflector中加载程序集,并使用下部显示窗格中的Name字段,如下所示:

http://i42.tinypic.com/2m30ocn.png

#2


You need to pass the name of the assembly to Assembly.Load(), not the name of the DLL. If you open the DLL in Reflector, the name should be at the bottom of the window. In the case of PresentationCore.dll, the name should be something like PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

您需要将程序集的名称传递给Assembly.Load(),而不是DLL的名称。如果在Reflector中打开DLL,则名称应位于窗口的底部。在PresentationCore.dll的情况下,名称应该类似于PresentationCore,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35。

#3


I recommend you take a look at How the Runtime Locates Assemblies in the MSDN library to get an idea of what happens when the CLR attempts to load an assembly.

我建议您查看运行时如何在MSDN库中查找程序集,以了解CLR尝试加载程序集时会发生什么。

For diagnosing specific issues, the Fusion Log Viewer tool is fantastic. Suzanne Cook, one of the Fusion developers has a guide on her blog which has helped me out in the past.

对于诊断特定问题,Fusion Log Viewer工具非常棒。 Fusion开发人员之一Suzanne Cook在她的博客上有一个指南,过去曾帮助过我。

#4


The easiest way to get the string is to first add a reference to your project then do this:

获取字符串的最简单方法是首先添加对项目的引用,然后执行以下操作:

string regStringMath = typeof(System.Math).Assembly;
Assembly assMath = Assembly.Load("System.Math", regStringMath);

string regStringPres = typeof(PresentationCore).Assembly;
Assembly assPres = Assembly.Load("PresentationCore", regStringPres);

This will ensure you get the right version that you added as a reference to your project.

这将确保您获得作为项目参考添加的正确版本。

#5


Use GetAssemblyName (http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.getassemblyname.aspx) to get the fully qualified assembly name of the dll, and pass this to Assembly.Load().

使用GetAssemblyName(http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.getassemblyname.aspx)获取dll的完全限定程序集名称,并将其传递给Assembly.Load()。

#1


If the assembly is in the GAC you must load it via its fully qualified name.

如果程序集在GAC中,则必须通过其完全限定名称加载它。

For example, if I wanted to load mscorlib.dll I would do something like this:

例如,如果我想加载mscorlib.dll,我会这样做:

Assembly a = Assembly.Load
    ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

The easiest way to determine an assembly's fully qualified name is to load the assembly in Reflector and use the Name field from the lower display pane like this:

确定程序集的完全限定名称的最简单方法是在Reflector中加载程序集,并使用下部显示窗格中的Name字段,如下所示:

http://i42.tinypic.com/2m30ocn.png

#2


You need to pass the name of the assembly to Assembly.Load(), not the name of the DLL. If you open the DLL in Reflector, the name should be at the bottom of the window. In the case of PresentationCore.dll, the name should be something like PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

您需要将程序集的名称传递给Assembly.Load(),而不是DLL的名称。如果在Reflector中打开DLL,则名称应位于窗口的底部。在PresentationCore.dll的情况下,名称应该类似于PresentationCore,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35。

#3


I recommend you take a look at How the Runtime Locates Assemblies in the MSDN library to get an idea of what happens when the CLR attempts to load an assembly.

我建议您查看运行时如何在MSDN库中查找程序集,以了解CLR尝试加载程序集时会发生什么。

For diagnosing specific issues, the Fusion Log Viewer tool is fantastic. Suzanne Cook, one of the Fusion developers has a guide on her blog which has helped me out in the past.

对于诊断特定问题,Fusion Log Viewer工具非常棒。 Fusion开发人员之一Suzanne Cook在她的博客上有一个指南,过去曾帮助过我。

#4


The easiest way to get the string is to first add a reference to your project then do this:

获取字符串的最简单方法是首先添加对项目的引用,然后执行以下操作:

string regStringMath = typeof(System.Math).Assembly;
Assembly assMath = Assembly.Load("System.Math", regStringMath);

string regStringPres = typeof(PresentationCore).Assembly;
Assembly assPres = Assembly.Load("PresentationCore", regStringPres);

This will ensure you get the right version that you added as a reference to your project.

这将确保您获得作为项目参考添加的正确版本。

#5


Use GetAssemblyName (http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.getassemblyname.aspx) to get the fully qualified assembly name of the dll, and pass this to Assembly.Load().

使用GetAssemblyName(http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.getassemblyname.aspx)获取dll的完全限定程序集名称,并将其传递给Assembly.Load()。