当前位置:网站首页>C 11 more practical NAMEOF

C 11 more practical NAMEOF

2022-06-13 09:44:00 Dotnet cross platform

C# 11 More practical nameof

Intro

from C# 6.0 Start , We can start using nameof To reference the parameter name , Where possible nameof The place of , It is generally recommended to use for automatic update during refactoring , Prior to C# In the version , If you want to add a method parameter or a method attribute Pass through nameof To reference a method will compile an error , from C# 11 Start , Expanded nameof Scope of action , We can use it , Here are a few examples

Samples

Method parameter reference

stay .NET 6 A new caller information is introduced in Attribute CallerArgumentExpression , Using it, we can simplify many logs or metrics Record and so on , Automatically get the information of the call , But there is a fly in the ointment , We need to use strings to refer to method parameters , As shown below :

public static ICollection<T> NotEmpty<T>([NotNull] ICollection<T> collection, [CallerArgumentExpression("collection")] string? paramName = null)
{
    if (collection.Count == 0)
    {
        throw new ArgumentException("The collection could not be empty", paramName);
    }
    return collection;
}

Examples like this , We quote collection Parameters can only use strings , This is for us collection When refactoring parameter names , Will still need to be manually modified CallerArgumentExpression Parameter name in , In the new version , We will be able to use directly nameof(collection) Instead of a string , as follows :

public static ICollection<T> NotEmpty<T>([NotNull] ICollection<T> collection, [CallerArgumentExpression(nameof(collection))] string? paramName = null)
{
    NotNull(collection, paramName);
    if (collection.Count == 0)
    {
        throw new ArgumentException("The collection could not be empty", paramName);
    }
    return collection;
}

In this way, it is not easy to make a manual error and input the parameter name incorrectly , and nameof Can provide a better refactoring experience

Attribute reference

In addition to the method parameters referenced in the method parameters , We can also use the Attribute Reference method parameters in , Examples are as follows :

[Description(nameof(nameExpression))]
public bool M(string name, [CallerArgumentExpression(nameof(name))]string nameExpression = null) 
{
    return string.IsNullOrEmpty(name);
}

Thanks to the C# Constant interpolation string property in , We can also be in Attribute Using constant interpolation strings in , as follows :

[Description($"{nameof(nameExpression)} IsNullOrEmpty")]
public bool M(string name, [CallerArgumentExpression(nameof(name))]string nameExpression = null) 
{
    return string.IsNullOrEmpty(name);
}

271d2cea26ed835411f2aef73aa2e057.png

More

At present, the support of this feature has been completed and merged into the main branch , Expect the next version of SDK We can use it after publishing ~~

References

  • https://github.com/dotnet/csharplang/blob/main/proposals/extended-nameof-scope.md

  • https://sharplab.io/#v2:EYLgtghglgdgNAExAagD4AEBMBGAsAKHQAYACdbAOgGEB7MABxpgFMYAXAWRoWYBsBuAsTKUASgFd2UMM2p16UXswBOAZRUA3KAGNmAZ0H4hAZjKYSVEgG8CJOyQDaAEX3blUemyhMAFABIAIisYCBkaADMfEJkAUQAPemV9PW8YAEoAXxIAST0AOXFeXgB5ZRiGNgBPALSAXVt7dFNgGhpeEg4fclJo5jhHKggilQBBZQBzcRl2eMTk1KjQ5gjFmTS67pJe2aS9FKYSAF4twt40kga7G3x7W7IAdhEiClyCotLyz0rV5jTDW4yBAyQA

原网站

版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130936444157.html