c#控制台应用程序能否在Windows控件上获取反射信息?

问题描述 投票:-1回答:1

我正在编写一个由Visual Studio运行的控制台应用程序,作为预构建步骤。此应用需要能够获取控件(例如System.Windows.Controls.Button)的Type信息,这通常是不可能的,因为控制台应用中未包含System.Windows.Controls。

我的控制台应用程序如何加载必要的DLL并提取类型信息?

c# controls reflect
1个回答
0
投票

为了加载PresentationFramework.dll并访问其中的Type信息,我必须首先加载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". 

这很有意义。

© www.soinside.com 2019 - 2024. All rights reserved.