我正在编写一个由 Visual Studio 作为预构建步骤运行的控制台应用程序。此应用程序需要能够获取有关控件(如 System.Windows.Controls.Button)的类型信息,这通常是不可能的,因为 System.Windows.Controls 不包含在控制台应用程序中。
我的控制台应用程序如何加载必要的 DLL 并提取类型信息?
为了加载 PresentationFramework.dll 并访问其中的类型信息,我必须首先加载 WindowsBase.dll。最终代码如下所示:
Assembly assem1 = Assembly.LoadFrom(@"C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\3.1.1\WindowsBase.dll");
Assembly assem2 = Assembly.LoadFrom(@"C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\3.1.1\PresentationFramework.dll");
Type t2 = assem2.GetType("System.Windows.Controls.Button");
PropertyInfo contentProperty = t2.GetProperty("Content");
如果我尝试添加对 PresentationFramework.dll 的引用,它会不高兴地说
There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "PresentationFramework", "AMD64".
这是有道理的。