[英]After register new assembly in GAC, Visual Studio doesn't find any dll
[英]Register a dll into the GAC - but then it doesn't show up in the assembly window
我有這個代碼將dll注冊到我的gac中
Assembly asm = Assembly.LoadFrom(argument);
RegistrationServices regAsm = new RegistrationServices();
bool bResult = regAsm.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase);
並且工作正常,我對bResult忠誠,但是當我打開GAC窗口時,我希望看到那里的dll,但事實並非如此。 誰能解釋我為什么?
當我將dll放入GAC窗口時,我在那里看到它。
這很奇怪,它不是由.NET框架包裝的。 必要的融合聲明隨時可用。 這段代碼效果很好:
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
static class GacUtil {
public static void InstallAssembly(string path, bool forceRefresh) {
IAssemblyCache iac = null;
CreateAssemblyCache(out iac, 0);
try {
uint flags = forceRefresh ? 2u : 1u;
int hr = iac.InstallAssembly(flags, path, IntPtr.Zero);
if (hr < 0) Marshal.ThrowExceptionForHR(hr);
}
finally {
Marshal.FinalReleaseComObject(iac);
}
}
public static void UninstallAssembly(string displayName) {
IAssemblyCache iac = null;
CreateAssemblyCache(out iac, 0);
try {
uint whatHappened;
int hr = iac.UninstallAssembly(0, displayName, IntPtr.Zero, out whatHappened);
if (hr < 0) Marshal.ThrowExceptionForHR(hr);
switch (whatHappened) {
case 2: throw new InvalidOperationException("Assembly still in use");
case 5: throw new InvalidOperationException("Assembly still has install references");
case 6: throw new System.IO.FileNotFoundException(); // Not actually raised
}
}
finally {
Marshal.FinalReleaseComObject(iac);
}
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
internal interface IAssemblyCache {
[PreserveSig]
int UninstallAssembly(uint flags, [MarshalAs(UnmanagedType.LPWStr)] string assemblyName, IntPtr pvReserved, out uint pulDisposition);
[PreserveSig]
int QueryAssemblyInfo(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssemblyName, IntPtr pAsmInfo);
[PreserveSig]
int CreateAssemblyCacheItem(/* arguments omitted */);
[PreserveSig]
int CreateAssemblyScavenger(out object ppAsmScavenger);
[PreserveSig]
int InstallAssembly(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszManifestFilePath, IntPtr pvReserved);
}
[DllImport("mscorwks.dll", PreserveSig = false)] // NOTE: use "clr.dll" in .NET 4+
internal static extern void CreateAssemblyCache(out IAssemblyCache ppAsmCache, int reserved);
}
如果要使用此代碼添加和刪除程序集,請不要忘記在資源管理器窗口中按F5刷新視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.