-1

I am writing a console app that is run by Visual Studio as a pre-build step. This app needs to be able to get Type information on controls (like System.Windows.Controls.Button) which normally isn't possible since System.Windows.Controls isn't included in a console app.

How can my console app load the necessary DLL and extract the Type info?

Betty Crokker
  • 3,001
  • 6
  • 34
  • 68
  • [Assembly.LoadFrom()](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.loadfrom?view=netframework-4.8), [Assembly.GetTypes()](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.gettypes?view=netframework-4.8) – Robert Harvey Feb 06 '20 at 19:15
  • You can add System.Windows.Controls in your reference and have it available in your console app. – the_lotus Feb 06 '20 at 19:16
  • @the_lotus: You could, but then you'd be tightly-bound to the assembly. If you need type information on arbitrary assemblies by name, this is probably intractable. You'd have to recompile the app every time you wanted to support a new assembly. – Robert Harvey Feb 06 '20 at 19:18
  • Since it's a console app, System.Windows.Controls isn't included automatically, so it won't be part of the assembly, so Assembly.GetExecutingAssembly().GetTypes() won't help. – Betty Crokker Feb 06 '20 at 19:19
  • I can't easily add a reference to System.Windows.Controls - when I right-click on the project and select Add and then Reference, there's nothing under Projects or Shared Projects, and the list under "COM" doesn't include System.Windows.Controls. I could Browse but don't know which DLL to point to. – Betty Crokker Feb 06 '20 at 19:21
  • Yes, @RobertHarvey, I realize my app is now tightly bound to System.Windows.Controls. I can't go into the proprietary details but this is an acceptable limitation. – Betty Crokker Feb 06 '20 at 19:22
  • @RobertHarvey Assembly.LoadFrom() seems like a good plan - but what file am I loading? – Betty Crokker Feb 06 '20 at 19:23
  • The [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.control?view=netframework-4.8) tells you in which assembly the class is in. Right on the top. – the_lotus Feb 06 '20 at 19:25
  • Getting closer! If I LoadFrom(@"C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\3.1.1\PresentationFramework.dll") it succeeds, but when I try to look at any Type information it says "Could not load type" – Betty Crokker Feb 06 '20 at 19:42
  • Show us your code. – Robert Harvey Feb 06 '20 at 19:43

1 Answers1

0

In order to load the PresentationFramework.dll and access the Type information therein, I had to first load WindowsBase.dll. The final code looks something like this:

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");

If I try to add a reference to PresentationFramework.dll it gets upset saying

There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "PresentationFramework", "AMD64". 

which makes sense.

Betty Crokker
  • 3,001
  • 6
  • 34
  • 68