.NET Core CSharp intermediate characteristics label articles 2-8

.NET Core CSharp Intermediate articles 2-8

This section contains the feature tag

Brief introduction

Attribute label is a very important technology, you can use technology to optimize Attribute streamline your code. Feature tag can be used in the assemblies, modules, types (classes, structures, enumerations, interfaces, delegates), fields, methods (including configuration), methods, parameters, return value of the method, the attribute (property), Attribute in. Its usage is flexible, we make a brief presentation on the use of the label.

Attribute feature tag

Attribute specified characteristics

Feature tag in a pair of brackets, and the portion where the marker tag required, such as:

[Table("People")]
class People
{
}

It was tagged for processing any of the properties, methods, where the label will enter the class. And your tag brackets, in fact, is calling its constructor.

Feature tag is somewhat similar to our predefined macros, but it is more marked for the expansion of some limited usage code or description.

Predefined attributes Attribute

The .NET Framework provides three predefined characteristics:

  • AttributeUsage
  • Conditional
  • Obsolete

    AttributeUsage

    If you need to implement a custom Attribute, you need to create a class and inherit Attribute class, and you need to use your Attribute AttributeUsage label restrictions, which AttributeUsage three parameters can be passed, in which:
  • Predetermined characteristic parameter validon language elements may be placed. It is the combination of values ​​of the enumerator AttributeTargets. The default value is AttributeTargets.All.
  • AllowMultiple parameters (optional) provides for a Boolean attribute AllowMultiple characteristic (property). If true, then this feature is multi-use. The default value is false (single use). The label that is used multiple times within the same property, class, function, etc.
  • Inherited parameters (optional) provides a Boolean value for Inherited attributes characteristic (property). If true, then this feature can be inherited by derived classes. The default value is false (not inherited).

Obsolete

The predefined characteristics tagged entities should not be used by the program. It allows you to tell the compiler to discard a particular target element. For example, when a new method is used in a class, but you still want to keep the old methods in the class, you can display a new method should be used, rather than the old method of the message to mark it as obsolete (outdated of).

  • obsolete have a constructor two parameters, wherein
    the parameter Message, a string is why described project obsolete and what alternative use.
  • Parameters iserror, is a Boolean value. If true, the compiler should use the project as a mistake. The default value is false (compiler generates a warning).

E.g

using System;
public class MyClass
{
   [Obsolete("Don't use OldMethod, use NewMethod instead", true)]
   static void OldMethod()
   {
      Console.WriteLine("It is the old method");
   }
   static void NewMethod()
   {
      Console.WriteLine("It is the new method");
   }
   public static void Main()
   {
      OldMethod();
   }
}

As specified iserror property is true, so the above procedures will be thrown at compile time, the error message is we specify the message.

If you specify iserror is false, then the above code to compile and run, but there will be a warning reminder.

Conditional

The predefined characteristics marked a conditional method, whose execution is dependent on the pre-specified identifier.

It will cause the conditional compilation of method calls, depending on the specified value such as Debug or Trace. For example, when the value of the variable display of debugging code.

The provisions of this feature has the following syntax:

[Conditional(
   conditionalSymbol
)]
//例如:
[Conditional("DEBUG")]

requires attention:

  • Conditions must be declared by the method or structure declaration. If the Conditional attribute is specified in the interface declaration method, compile-time error will occur.
  • The method must have a return type conditions.
  • Flag condition can not override modifier method. However, the method using the flag condition modifier virtual. Such methods overridden method is a method of conditional implied, but not explicitly marked with a Conditional attribute.
  • Condition can not be implemented method interface method. Otherwise, compile-time error will occur.
  • If the conditions of the process used in the "delegate creation expression", a compilation error will occur

Creating a Custom Attribute

Finished some of our basic content, we explain some of our application, there will be a slight reflection comes to our next lecture, but not very deep.

Construct a TestAttribute

// 描述如何使用一个自定义特性 
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]    
//自定义特性
public class TestAttribute : Attribute    {
    private string name; // 名字
    private string date; // 日期
    public string Name {
        get { return name; }
        set { name = value; }
    }
    public string Date    {
        get { return date; }
        set { date = value; }
    }
    public TestAttribute(string name)    {
        this.name = name;
        this.name = name;
    }
}

Instantiation

[Test("Amy", Date = "2018-06-18")]
[Test("Jack", Date = "2018-06-18")]
class Test{}

Extracting the tag value in tag

Type t = typeof(Test);
var value = t.GetCustomAttributes(typeof(TestAttribute),true);
foreach(TestAttribute each in value)
{
    Console.WriteLine("Name:{0}", each.Name);
    Console.WriteLine("Data:{0}",each.Data);
}

If my article help you, please have your github .NETCoreGuide project helped me a star, a concern and recommendation in the garden midpoint blog.

Github

BiliBili Home

WarrenRyan'sBlog

Blog Park

Guess you like

Origin www.cnblogs.com/WarrenRyan/p/11484963.html