C#:在 GAC 中查找 PresentationCore.dll(和其他程序集)的路径

C#: finding path to PresentationCore.dll (and other assemblies) in the GAC

我需要通过调用 ReferencedAssemblies.Add.

来引用 CSharpCodeProvider 中的各种程序集

对于某些程序集,只需传递程序集名称就足够了 - 例如 ReferencedAssemblies.Add("System.dll").

但是,PresentationCore.dll 是其中一个需要程序集完整路径的地方 - 我可能不一定知道目标机器上的路径。

问题:

How can I locate the definitive path to the (latest?) respective assembly - say, in the GAC?

Ideally, the solution would not only work for PresentationCore.dll, but also other assemblies such as PresentationFramework.dll, System.Xaml.dll or System.Windows.Forms.

未产生预期结果的来源:

您可以在应该进行解析的代码中引用程序集。然后运行时会告诉你程序集位置:

typeof([TypeKnownToBeInTheDesiredAssembly]).Assembly.CodeBase

例如,要找到System.Xml.dll:

string codeBase = typeof(System.Xml.XmlDocument).Assembly.CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
Console.WriteLine(path);

[ref]

在我的系统上:

C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll