0

I got a class that currently needs to be registered like this (to work reflection-free)

ViewLocator = new StrongViewLocator()
    .Register<MainWindowViewModel>(new ViewDefinition(typeof(MainWindow), () => new MainWindow()))
    .Register<AddTextDialogViewModel>(new ViewDefinition(typeof(AddTextDialog), () => new AddTextDialog()));

I want to get the equivalent of the above code generated using this simpler syntax.

ViewLocator = new StrongViewLocator()
    .Register<MainWindowViewModel, MainWindow>()
    .Register<AddTextDialogViewModel, AddTextDialog>();

How can I achieve this with Source Generator?

Etienne Charland
  • 3,424
  • 5
  • 28
  • 58
  • 1
    I'm not sure how it is relevant to Source Generators. Source Generator generates new code during the build. Here, it seems that extension method will suffice. – Vlad DX Feb 28 '23 at 18:57
  • 1
    Why not rather make an extension method? – Fildor Feb 28 '23 at 19:12

1 Answers1

3

I believe an extension method will suffice.

Something like this:

public static StrongViewLocator Register<TViewModel, TWindow>(this StrongViewLocator viewLocator)
    where TWindow : new()
{
    // .Register<MainWindowViewModel>(new ViewDefinition(typeof(MainWindow), () => new MainWindow()))
    // .Register<MainWindowViewModel, MainWindow>()

    return viewLocator.Register<TViewModel>(new ViewDefinition(typeof(TWindow), () => new TWindow()))
}
Vlad DX
  • 4,200
  • 19
  • 28