有沒有辦法在我自己的應用程序中使用visual studio的「添加程序集引用對話框」(或類似的東西)?我需要它來進行動態代碼生成和編譯。添加程序集引用對話框
這不僅僅是一個OpenFileDialog,因爲它另外考慮了GAC等,所以我認爲這樣做會非常複雜。
如果這不可行,我如何從GAC獲取所有程序集的列表?
有沒有辦法在我自己的應用程序中使用visual studio的「添加程序集引用對話框」(或類似的東西)?我需要它來進行動態代碼生成和編譯。添加程序集引用對話框
這不僅僅是一個OpenFileDialog,因爲它另外考慮了GAC等,所以我認爲這樣做會非常複雜。
如果這不可行,我如何從GAC獲取所有程序集的列表?
你不想你的應用程序是是慢,你:P
有一個undocumented API它允許您枚舉來自GAC的程序集。
從海關總署獲得的所有組件的最好方法是使用融合
第一計算策略:下面的代碼片段展示瞭如何實現自己的目標:
internal class GacApi
{
[DllImport("fusion.dll")]
internal static extern IntPtr CreateAssemblyCache(
out IAssemblyCache ppAsmCache,
int reserved);
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
internal interface IAssemblyCache
{
int Dummy1();
[PreserveSig()]
IntPtr QueryAssemblyInfo(int flags, [MarshalAs(UnmanagedType.LPWStr)] String assemblyName, ref ASSEMBLY_INFO assemblyInfo); int Dummy2(); int Dummy3(); int Dummy4();
}
[StructLayout(LayoutKind.Sequential)]
internal struct ASSEMBLY_INFO
{
public int cbAssemblyInfo;
public int assemblyFlags;
public long assemblySizeInKB;
[MarshalAs(UnmanagedType.LPWStr)]
public String currentAssemblyPath;
public int cchBuf;
}
class Program
{
static void Main()
{
try
{
Console.WriteLine(QueryAssemblyInfo("System"));
}
catch(System.IO.FileNotFoundException e)
{
Console.WriteLine(e.Message);
}
}
public static String QueryAssemblyInfo(String assemblyName)
{
ASSEMBLY_INFO assembyInfo = new ASSEMBLY_INFO();
assembyInfo.cchBuf = 512;
assembyInfo.currentAssemblyPath = new String('\0', assembyInfo.cchBuf) ;
IAssemblyCache assemblyCache = null;
IntPtr hr = GacApi.CreateAssemblyCache(out assemblyCache, 0);
if (hr == IntPtr.Zero)
{
hr = assemblyCache.QueryAssemblyInfo(1, assemblyName, ref assembyInfo);
if(hr != IntPtr.Zero)
Marshal.ThrowExceptionForHR(hr.ToInt32());
}
else
Marshal.ThrowExceptionForHR(hr.ToInt32());
return assembyInfo.currentAssemblyPath;
}
}
第二種方法: GAC目錄(用於默認安裝的%systemroot%\ assembly)是一個標準目錄,就像任何其他目錄一樣,您應該可以遍歷該目錄,加載程序集並檢索程序集中所有類型的類型信息。
編輯:爲最簡單的方法代碼:
List<string> dirs = new List<string>() {
"GAC", "GAC_32", "GAC_64", "GAC_MSIL",
"NativeImages_v2.0.50727_32",
"NativeImages_v2.0.50727_64",
"NativeImages_v4.0.50727_32",
"NativeImages_v4.0.50727_64"
};
string baseDir = @"c:\windows\assembly";
int i = 0;
foreach (string dir in dirs)
if (Directory.Exists(Path.Combine(baseDir, dir)))
foreach (string assemblyDir in Directory.GetFiles(Path.Combine(baseDir, dir), "*.dll", SearchOption.AllDirectories))
Console.WriteLine(assemblyDir);
有關Fusion.dll的更多信息,可以發現在:
http://support.microsoft.com/kb/317540
讓我知道,如果你有其他疑問
s
像什麼慢?如果用戶打開添加引用對話框,則可以等待幾秒鐘。 – codymanix 2009-11-25 21:42:07
這是一個普遍接受的事情,我從幾百個來源聽到,如http://weblogs.asp.net/scottgu/archive/2009/10/29/add-reference-dialog-improvements-vs-2010-and-淨4-0-series.aspx。但笑臉旨在承認你想要類似的功能,這是一個完全合理的目標。 – 2009-11-26 08:46:53