这个问题与以下问题非常相似,但我的问题更精确一些,而且两者都没有被接受的答案,而且所提供的答案似乎都不完整或最优:
最初我认为以下是最好的方法,但是除非您指定程序集的全名,否则它不起作用,并且由于 try/catch 有点骇人听闻,但它很简单并且适用于许多情况:
public static class GacUtil
{
public static bool IsAssemblyInGAC(string assemblyFullName)
{
try
{
return Assembly.ReflectionOnlyLoad(assemblyFullName)
.GlobalAssemblyCache;
}
catch
{
return false;
}
}
public static bool IsAssemblyInGAC(Assembly assembly)
{
return assembly.GlobalAssemblyCache;
}
}
这是一种更好的方法,通过使用Fusion API无需 try/catch 即可工作。这是一堆更多的代码,但它适用于部分程序集名称:
public static class GacUtil
{
[DllImport("fusion.dll")]
private static extern IntPtr CreateAssemblyCache(
out IAssemblyCache ppAsmCache,
int reserved);
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
private interface IAssemblyCache
{
int Dummy1();
[PreserveSig()]
IntPtr QueryAssemblyInfo(
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string assemblyName,
ref AssemblyInfo assemblyInfo);
int Dummy2();
int Dummy3();
int Dummy4();
}
[StructLayout(LayoutKind.Sequential)]
private struct AssemblyInfo
{
public int cbAssemblyInfo;
public int assemblyFlags;
public long assemblySizeInKB;
[MarshalAs(UnmanagedType.LPWStr)]
public string currentAssemblyPath;
public int cchBuf;
}
public static bool IsAssemblyInGAC(string assemblyName)
{
var assembyInfo = new AssemblyInfo { cchBuf = 512 };
assembyInfo.currentAssemblyPath = new string('\0', assembyInfo.cchBuf);
IAssemblyCache assemblyCache;
var hr = CreateAssemblyCache(out assemblyCache, 0);
if (hr == IntPtr.Zero)
{
hr = assemblyCache.QueryAssemblyInfo(
1,
assemblyName,
ref assembyInfo);
if (hr != IntPtr.Zero)
{
return false;
}
return true;
}
Marshal.ThrowExceptionForHR(hr.ToInt32());
return false;
}
}