Re[2]: Introducing C# Source Generators
От: Serginio1 СССР https://habrahabr.ru/users/serginio1/topics/
Дата: 01.05.20 14:14
Оценка:
Здравствуйте, 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

        }
и солнце б утром не вставало, когда бы не было меня
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.