API Request: Allow developers to inject a XAML factory for creating objects
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
As we know, the WPF will call the `XamlTypeInvoker.CreateInstance` to create the object in the XAML engine.
https://github.com/dotnet/wpf/blob/bbd9e44d87b4edd8880f4c09c6db36a2f3e23ba8/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs#L133-L145
All the type which we define will use reflection to create. I do not think this is a high-performance behavior.
Should we add XAML object creation factory that allow developers to inject a XAML factory for creating objects?
```csharp
// Before the XAML engine starts, add initialization logic
XamlObjectCreationFactory.RegisterCreator(() => new MainWindow());
```
In the future, we can use [SourceGenerator](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/) to automate the writing of this part of the code
Contributor guide
Assessment
This issue has not been assessed yet.