Shenzhen Ka Wah Road _04 _ architect advanced properties

First, what is characteristic

Characteristics (the Attribute) is a program for transferring various elements (such as classes, methods, structures, enumerations, assembly, etc.) at runtime behavior declarative tag information. You can add declarative information to the program by using the feature. A label statement by placing it in front of the application element in square brackets ([]) is described.

Characteristics (the Attribute) for adding metadata and compiler directives as comments, descriptions, methods, additional information and the like. .Net framework offers two types of features: predefined properties and custom properties.

The syntax of the following characteristics:

[attribute(positional_parameters, name_parameter = value, ...)]
element

Characteristics (the Attribute) names and values ​​are defined within the square brackets, the element is placed before it is applied. positional_parameters regulations necessary information, name_parameter provisions optional information.

Second, the predefined characteristics

1, Obsolete characteristics

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).

The syntax is as follows:

[Obsolete(
   message
)]
[Obsolete(
   message,
   iserror
)]

among them:

  • Parameters  the Message , is a string, the reason why the described project obsolete and what the 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).

Consider the following example of a small:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAttribute
{
    [Obsolete("请不要使用该类了,该类已经过时了,请使用什么代替")]
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public string Accont { get; set; }

        public long QQ { get; set; }

        public string Answer([Custom]string name) 
        {
            return $"This is {name}";
        }
    }
}

In the example above, the above uses the class Student Obsolete attribute to mark the class obsolete.

 Third, custom properties

.Net framework allows the creation of custom features for declarative information storage, and may be retrieved at run-time. This information is based on design criteria and applications need to be associated with any objective element.

Create and use custom features include four steps:

  • Statement Custom Properties
  • Build custom properties
  • Apply custom properties on the target program element
  • By accessing reflection characteristics

1, the statement custom properties

In the above example, is defined using the F12 view Obsolete:

Build custom properties

Each property must have at least one constructor. Required location (Positional) should be passed by the constructor parameter. The following code demonstrates CustomAttribute categories:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAttribute
{
    /// <summary>
    /// 自定义Custom特性
    /// </summary>
    public class CustomAttribute :Attribute
    {
        /// <summary>
        /// 无参构造函数
        /// </summary>
         public CustomAttribute()
        {

        }

        /// <summary>
        /// 有参构造函数
        /// </summary>
        /// <param name="id"></param>
        public CustomAttribute(string description)
        {
            this.Description = description;
        }

        /// <summary>
        /// 属性
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 字段
        /// </summary>
        public string Remark = null;

        public void Show()
        {
            Console.WriteLine("This Is CustomAttribute");
        }
    }
}

Apply custom properties on the target program element

By immediately placed in its characteristic target (classes, methods, properties, fields, etc.) above, to apply the features:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAttribute
{
    [Obsolete("请不要使用该类了,该类已经过时了")]
    [Custom("这是Custom自定义特性")]
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public string Accont { get; set; }

        public long QQ { get; set; }

        public string Answer([Custom]string name) 
        {
            return $"This is {name}";
        }
    }
}

 

Published 37 original articles · won praise 3 · Views 6316

Guess you like

Origin blog.csdn.net/huan13479195089/article/details/104726830