winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Is there a solution when using Microsoft.WinForms.Designer.SDK and Reflection in your Project?

Open SchoenGFM opened this issue 1 year ago • 8 comments

Okay, so using Microsoft.WinForms.Designer.SDK solved it for me, but required one more change to the codebase.

And for future searchers and finders of this topic: While the Assembly containing the System.Windows.Forms.Design.ParentControlDesigner is available during runtime, the assembly that contains Microsoft.DotNet.DesignTools.Designers.ParentControlDesigner is not.

This means, that Reflection that resolves all included Type-dependencies ( like Assembly.GetTypes() ) on the assembly that contains the designer-class will now lead to a "file not found" exception during runtime.

Originally posted by @CortiWins in https://github.com/dotnet/winforms/issues/11531#issuecomment-2169282036

SchoenGFM avatar Aug 20 '24 14:08 SchoenGFM

Microsoft.DotNet.DesignTools.* are only used to facilitate the design-time experience, and are not meant to be used at runtime.

RussKie avatar Aug 21 '24 00:08 RussKie

Thank you, that makes sense! But we try to upgarde our .Net Framework Project to .Net and it uses MEF and a lot of Import/Export magic. I am not so familiar with MEF but we do something like this and get the following exception:

grafik

In this case Form1 has a dependency to a Control which uses ParentControlDesigner. Can you give me some advise / best practise how to deal with such issues?

SchoenGFM avatar Aug 21 '24 07:08 SchoenGFM

@RussKie This means i have to use different dependencies and "using" references depinding wether i am designing or running my appliation?

SchoenGFM avatar Aug 29 '24 11:08 SchoenGFM

Correct.

The design-time SDK is located here: https://github.com/microsoft/winforms-designer-extensibility/. The team has also published few blog posts (e.g., https://devblogs.microsoft.com/dotnet/state-of-the-windows-forms-designer-for-net-applications/) explaining the design-time experience.

RussKie avatar Aug 29 '24 11:08 RussKie

@SchoenGFM So, how did you solve it?

I put all designer classes using the design time assemblies into their own project, so it's no longer linked when reflection accesses the types within the assembly that contains the controls those designers work with.

I'm still not a 100% happy with it.

CortiWins avatar Oct 01 '24 21:10 CortiWins

@CortiWins We did not solve it until now. We have a quite large project using .net framework and would like to upgrade to .net. For the first try i have just used some preprocessor directives like "#IF ... using ... #ELSE ... using ....", but this is not the way to go for us.

Maybe i try to re-structure our project the way you did ... thanks for the tip and answer!

SchoenGFM avatar Oct 07 '24 09:10 SchoenGFM

@CortiWins Could you maybe provide a small example to show how you have actually structured your project which uses MEF and controls which depend on the the design-time SDK? This would help me lot when it comes to re-structure our own project.

SchoenGFM avatar Oct 08 '24 13:10 SchoenGFM

@SchoenGFM We do not use MEF ( what does that stand for? Microsoft Entity Framework, Managed Extensibility Framework? ). What we both do/did is apply reflection that leads to the problem.

We use a script that 'strongly recommends" the runtime to JIT instantly with for example System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod It is far from a perfect AOT but in the old net4 days it was helping to make our industrial application have the same timing in the first run of its execution cycle as in the following, which reduced timeouts after restarting the software.

That is done via Reflection and that Reflection tried to access Microsoft.DotNet.DesignTools.Designers when doing Reflection on the assembly that contains the designer class. Resulting in the FileNotFoundException you also know.

So we put the designer class in a new project. And we don't apply our JIT helper on it. No Reflection on that assembly, no crash. Adds one more project for like 1 class. The example would be me, putting 1 cs file in a new project. That's all.

CortiWins avatar Oct 10 '24 19:10 CortiWins