Is there a solution when using Microsoft.WinForms.Designer.SDK and Reflection in your Project?
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
Microsoft.DotNet.DesignTools.* are only used to facilitate the design-time experience, and are not meant to be used at runtime.
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:
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?
@RussKie This means i have to use different dependencies and "using" references depinding wether i am designing or running my appliation?
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.
@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 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!
@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 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.