2 回答

TA貢獻(xiàn)1873條經(jīng)驗 獲得超9個贊
一樓介紹了方法,我來具體化一下代碼:
你需要得到被一個特定的程序集所引用的所有程序集。這個信息可以告訴你這個程序集是否在引用一個或多個你所創(chuàng)建的程序集,或者你的程序集是否在引用其他特定的程序集。
解決方法:
使用Assembly.GetReferencedAssemblies方法去得到一個程序集所引用的程序集。例如:
public static string[] BuildDependentAssemblyList(string path, List<string> assemblies) { // 維護(hù)一個本程序集需要的程序集列表 if (assemblies == null) assemblies = new List<string>();
// 是否已經(jīng)包含這個路徑的程序了
if (assemblies.Contains(path) == true)
return (new string[0]);
Assembly asm = null;
// 檢查這個路徑
// 看是一個程序名還是一個路徑
if ((path.IndexOf(Path.DirectorySeparatorChar, 0, path.Length) != -1) || (path.IndexOf(Path.AltDirectorySeparatorChar, 0, path.Length) != -1)) {
// 從這個路徑加載程序集
asm = Assembly.ReflectionOnlyLoadFrom(path);
}
else
{
// 是一個程序集名稱
asm = Assembly.ReflectionOnlyLoad(path);
}
// 把程序集添加到列表中
if (asm != null)
{
assemblies.Add(path);
}
// 獲取所引用的程序集
AssemblyName[] imports = asm.GetReferencedAssemblies();
// 遍歷所有的引用,并進(jìn)行遞歸
foreach (AssemblyName asmName in imports) {
BuildDependentAssemblyList(asmName.FullName, assemblies);
}
string[] temp = new string[assemblies.Count];
assemblies.CopyTo(temp, 0);
return (temp);
}
這段代碼返回一個包含有原程序集、所有其引用的程序集和這些程序集依賴的其他程序集的string數(shù)組。

TA貢獻(xiàn)1906條經(jīng)驗 獲得超3個贊
MSDN
ms-help://MS.MSDNQTR.v90.chs/fxref_mscorlib/html/2fcfa8fc-9a2b-af3a-8224-cee181149029.htm
Assembly.GetReferencedAssemblies 方法
- 2 回答
- 0 關(guān)注
- 974 瀏覽
添加回答
舉報