.net 检查 GAC 的程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1933947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check GAC for an assembly
提问by Jessy
How to programmatically check GAC for an assembly?
如何以编程方式检查程序集的 GAC?
回答by jason
Without even trying to get complicated, you could just shell out to gacutiland capture the output. For example, gacutil /l Microsoft.Practices.Unitygives me:
甚至无需尝试变得复杂,您就可以直接gacutil捕获并捕获输出。例如,gacutil /l Microsoft.Practices.Unity给我:
Microsoft (R) .NET Global Assembly Cache Utility. Version 3.5.30729.1
Copyright (c) Microsoft Corporation. All rights reserved.
The Global Assembly Cache contains the following assemblies:
Microsoft.Practices.Unity, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31
bf3856ad364e35, processorArchitecture=MSIL
Number of items = 1
versus gacutil /l Some.Nonexistant.Assembly:
与gacutil /l Some.Nonexistant.Assembly:
Microsoft (R) .NET Global Assembly Cache Utility. Version 3.5.30729.1
Copyright (c) Microsoft Corporation. All rights reserved.
The Global Assembly Cache contains the following assemblies:
Number of items = 0
This is easy to implement and parse and isn't dependent on any third-party implementations.
这很容易实现和解析,并且不依赖于任何第三方实现。
回答by BALKANGraph
It's better to use ReflectionOnlyLoadMethod. this method loads an assembly into the reflection-only context, where it can be examined but not executed.
最好使用ReflectionOnlyLoad方法。此方法将程序集加载到仅反射上下文中,可以在其中检查但不能执行。
回答by StrayPointer
From .NET, the reflection API - Assembly.Load(...)will throw a FileNotFoundExceptionif it does not find the assembly. The API requires a fully qualified assembly name, so I assume it must be in the GAC. I am using it to test for the presence of SQL Server Compact Edition:
在 .NET 中,反射 API -如果找不到程序集,Assembly.Load(...)则会抛出 a FileNotFoundException。API 需要一个完全限定的程序集名称,所以我假设它必须在 GAC 中。我用它来测试 SQL Server Compact Edition 是否存在:
Assembly foo = Assembly.Load("System.Data.SqlServerCe, Version=3.5.1.0, " +
"Culture=neutral, PublicKeyToken=89845dcd8080cc91");
回答by David Brown
You can use the Fusion COM API. Junfeng Zhang wrote a managed wrapper. It's from 2004, though, so I don't know how well it works anymore.
您可以使用 Fusion COM API。张俊峰写了一个托管包装器。不过,它是从 2004 年开始的,所以我不知道它的效果如何了。
回答by GrayWizardx
Do you want to probe the GAC for an assembly or do you just want to know that the assembly exists on the machine?
您是想探查 GAC 中的某个程序集,还是只想知道该程序集存在于机器上?
If you dont care that the assembly is actually in the GAC, but just loadable on the machine (from the appdomain) you can just use LoadAssembly with the assemblies name (strong, common, full, etc). If the assembly can be loaded by Fusion it will be and then you will know it exists.
如果您不关心程序集实际上是否在 GAC 中,但只是可加载到机器上(从应用程序域),您可以使用带有程序集名称(强、通用、完整等)的 LoadAssembly。如果程序集可以被 Fusion 加载,那么你就会知道它存在。

