Здравствуйте, Sinclair, Вы писали:
S>Здравствуйте, Serginio1, Вы писали:
S>>https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/
S>>Как то новость прошла без энтузиазма
S>Крутая штука. Надо покрутить.
S>> Очень интересен пример AutoNotifyGenerator https://github.com/dotnet/roslyn-sdk/blob/master/samples/CSharp/SourceGenerators/SourceGeneratorSamples/AutoNotifyGenerator.cs
S>Cходу не понял, нафига они инжектят код атрибута. Такие-то штуки можно просто через Dependency принести, обычным атрибутом как у всех.
S>Или они хотели избежать необходимости давать ссылку на второй пакет?
https://github.com/dotnet/roslyn-sdk/blob/master/samples/CSharp/SourceGenerators/GeneratedDemo/UseAutoNotifyGenerator.cs
Ну они генерят отдельный файл во время компиляции, при этом можно использовать внутри кода
public partial class ExampleViewModel
{
[AutoNotify]
private string _text = "private field text";
[AutoNotify(PropertyName = "Count")]
private int _amount = 5;
}
public static class UseAutoNotifyGenerator
{
public static void Run()
{
ExampleViewModel vm = new ExampleViewModel();
// we didn't explicitly create the 'Text' property, it was generated for us
string text = vm.Text;
Console.WriteLine($"Text = {text}");
// Properties can have differnt names generated based on the PropertyName argument of the attribute
int count = vm.Count;
Console.WriteLine($"Count = {count}");
// the viewmodel will automatically implement INotifyPropertyChanged
vm.PropertyChanged += (o, e) => Console.WriteLine($"Property {e.PropertyName} was changed");
vm.Text = "abc";
vm.Count = 123;
// Try adding fields to the ExampleViewModel class above and tagging them with the [AutoNotify] attribute
// You'll see the matching generated properties visibile in IntelliSense in realtime
}