![](/img/trans.png)
[英]How to programmatically determine if .NET assembly is installed in GAC?
[英]how to query gac for assembly files
我試圖學習有關反射的更多信息,並采用了一些已構建並添加到其中的代碼。 現在,我試圖在GAC中查詢其他程序集和構建類型實例等。我修改了在這里找到的代碼,但myAssemblyList為空。 你能告訴我我做錯了什么嗎? 我進行了調試,並在“ var currentAssembly = value.GetAssembly(f);”處停了下來。 它返回null。 我看到的所有代碼都從當前AppDomain填充程序集,但是我看到了類似LoadFrom()的方法,這些方法應與目錄路徑一起使用。 我也看到了這篇文章,並進行了編譯。
class Program
{
static void Main(string[] args)
{
AppDomainSetup domaininfo = new AppDomainSetup();
domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
AppDomain domain = AppDomain.CreateDomain("MyDomain", adevidence, domaininfo);
Type type = typeof(Proxy);
var value = (Proxy)domain.CreateInstanceAndUnwrap(
type.Assembly.FullName,
type.FullName);
//String myDir = "C:\\Windows\\Microsoft.NET\\assembly\\GAC_64\\";
String myDir = "C:\\Windows\\Microsoft.NET\\assembly\\";
List<Assembly> myAssemblyList = new List<Assembly>();
foreach (String f in Directory.GetFiles(myDir, "*.dll", SearchOption.AllDirectories))
{
//Console.WriteLine($"Here is f: {f}");
var currentAssembly = value.GetAssembly(f);
if (currentAssembly != null)
{
myAssemblyList.Add(currentAssembly);
Console.WriteLine(currentAssembly.FullName);
//Console.ReadLine();
}
Console.WriteLine($"Total Assemblies found: {myAssemblyList.Count}");
}
Console.WriteLine($"Total Assemblies found: {myAssemblyList.Count}");
Console.ReadLine();
}
}
public class Proxy : MarshalByRefObject
{
public Assembly GetAssembly(string assemblyPath)
{
try
{
return Assembly.LoadFile(assemblyPath);
}
catch (Exception)
{
return null;
// throw new InvalidOperationException(ex);
}
}
}
我跳回到一個目錄,並嘗試從GAC_ *收集,即32、64和MSIL。 我為currentAssembly添加了一個null測試,以解決GetAssembly()的問題。 但是仍然有些包含dll和非dll文件的目錄會導致異常。
修改此行:
foreach (String f in Directory.GetFiles(myDir))
同
foreach (String f in Directory.GetFiles(myDir, "*.dll", SearchOption.AllDirectories)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.