📜 ⬆️ ⬇️

Possible innovations of C #



You must be aware that recently Microsoft has been trying to communicate with the community, get as many reviews as possible and implement them. Not bypassed this mod and C # development team. Developing a new version of the language is definitely not going on behind closed doors ...

An article by Mads Torgersen entitled What's New in C # 7.0 has already been disassembled up and down. But there is something that was not mentioned in it.

I suggest you go through the review on several proposals from the Roslyn repository. Topic Title C # 7 Work List of Features . The information was updated a couple of months ago (that is, it is not very relevant), but this is something that was definitely not neglected by Mads Torgersen.
')
Even twice, the following sentences are mentioned in the headings of Strong interest and Non-language:

1. Enable code generating extensions to the compiler # 5561
2. Add supersede modifier to enable more tool generated code scenarios # 5292

In fact, this is a proposal to add some aspect-oriented programming functionality to C #. I will try to convey in abbreviated form the essence.

Adding a code generation extension to the compiler


I would like to note that this sentence is marked as being under development on github. It offers get rid of duplicate code using Code Injectors. When compiling, certain code will be added automatically by the compiler. What is important: the source code will not be changed, and the code will not be injected after being compiled into a binary file.

The description indicates that the process of writing the injector will be similar to the writing of the diagnostic analyzer. I hope that everything will be much easier (in this case, classes can be created and edited manually). But it is possible that it will be convenient to do this only with the help of tools that generate code automatically.

As an example, a class is given that in turn introduces a new field into each project class, a constant of the type string named ClassName and a value containing the name of this class.

[CodeInjector(LanguageNames.CSharp)]
public class MyInjector : CodeInjector
{
    public override void Initialize(InitializationContext context)
    {
        context.RegisterSymbolAction(InjectCodeForSymbol);
    }

    public void InjectCodeForSymbol(SymbolInjectionContext context)
    {
        if (context.Symbol.TypeKind == TypeKind.Class)
        {
            context.AddCompilationUnit($@"partial class {context.Symbol.Name} 
                      {{ public const string ClassName = ""{context.Symbol.Name}""; }}");
        }
    }
}

- , . , , , INotifyPropertyChanged.

INotifyPropertyChanged ? PostSharp Walkthrough: Automatically Implementing INotifyPropertyChanged
. , , PostSharp IL (How Does PostSharp Work). , IL .

, .

, INotifyPropertyChanged. Name, :

class Employee: INotifyPropertyChanged
{
 private string _name;
 
 public string Name
 {
    get { return _name; }
    set 
    {
       _name = value;
       RaisePropertyChanged();
    }
 }
 
 private void RaisePropertyChanged([CallerMemberName] string caller="")
 {
    if( PropertyChanged != null )
    {
       PropertyChanged(this, new PropertyChangedEventArgs(caller));
    }
 }
}

:

[INoifyPropertyChanged]
public class Employee
{
   public string Name { get; set; } 
}

, , INPC INoifyPropertyChanged. :

public class EmployeeINPC
{
   public string Name { get; set; } 
}

.

Superseding members — ,


, , supersede.

, :

//   
public partial class MyClass
{
      public void Method1()
      {
          // -   
      }
}

//   
public partial class MyClass
{
      public supersede void Method1()
      {
           Console.WriteLine("entered Method1");
           superseded();
           Consolel.WriteLine("exited Method1");
      }
}

Method1, Method1, . Method1 . , superseded. , , Method1.

, INotifyPropertyChanged :

//   
public partial class MyClass
{
     public int Property { get; set; }
}

//   
public partial class MyClass  : INotifyPropertyChanged
{
     public event PropertyChangedEventHandler PropertyChanged;

     public supersede int Property
     {
          get { return superseded; }
          set
          {
              if (superseded != value)
              {
                    superseded = value;
                    var ev = this.PropertyChanged;
                    if (ev != null) ev(this, new PropertyChangedEventArgs("Property"));
              }
          }
     } 
}

superseded , .

Main


github, . Main await. , , :

static async Task DoWork() {
    await ...
    await ...
}

static void Main() {
    DoWork().GetAwaiter().GetResult();
}

. DoWork().Wait() AggregateException.

CLR , main , async Main.

. . , .



, C# 7.0, , 8.0, .

Visual Studio 2017 Task-like return types for async methods, 7- .

GitHub .

Source: https://habr.com/ru/post/312048/


All Articles