1.C# Attribute
Characteristics are a means to add additional information to the code . We usually mark the characteristics on methods, classes or attributes. When using these structures, reflection (reflection), a very advanced technology, is used to obtain They pass information from attribute tags (in compiled assemblies, as metadata) for special processing.
The .Net framework provides two types of attributes: predefined attributes and custom attributes .
1.1 Attribute
The syntax for specifying attributes is as follows:
[attribute(positional_parameters, name_parameter = value, ...)]
element
use:
The use of features is very simple, just use "[]" to expand the feature class name on the line above the structure declaration:
The system also provides us with some features , such as Serializable to mark a serializable class, and the DebuggerStepThrough setting method to skip the state during debugging.
[Serializable]
class Sunshine{...}
It actually omits (). If you use other construction methods of the feature, just display the declaration:
[Serializable("test")]
class Sunshine{...}
1.2 Predefined attributes (Attribute)
The .Net framework provides three predefined features:
- AttributeUsage
- Conditional
- Obsolete
1.2.1 AttributeUsage (custom attribute)
The predefined attribute AttributeUsage describes how to use a custom attribute class . It specifies the type of item to which the attribute can be applied.
The syntax for specifying this property is as follows:
[AttributeUsage(
validon,
AllowMultiple=allowmultiple,
Inherited=inherited
)]
in:
- The parameter validon specifies the language elements on which the attribute can be placed. It is a combination of the values of the enumerator AttributeTargets . The default value is AttributeTargets.All .
- The parameter allowmultiple (optional) provides a boolean value for the attribute's AllowMultiple property. If true, the feature is multipurpose. The default is false (single-use).
- The parameter inherited (optional) provides a boolean value for the attribute's Inherited property. If true, the attribute can be inherited by derived classes. The default value is false (not inherited).
For example:
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
use:
- Build a custom attribute [AttributeUsage()]
- Write a custom attribute class, the class name must end with Attribute and inherit from the Attribute class
- Pass the positioned parameters through the constructor (each custom attribute class must have at least one constructor)
- Property information retrieved via reflection
using System;
class MainClass
{
public static void Main()
{
Maoxiandao test = new Maoxiandao();
Type t = test.GetType();
object[] o = t.GetCustomAttributes(true);
TestAttribute attribute = (TestAttribute)o[0];
Console.WriteLine(attribute.msg);
}
}
[Test("这是测试类")] //这里的Test对应下方的TestAttribute
public class Maoxiandao { }
[AttributeUsage(AttributeTargets.All)]
public class TestAttribute : Attribute
{
public string msg;
public TestAttribute(string msg)
{
this.msg = msg;
}
}
1.2.2 Conditional
Conditional attributes can be added to a method to make the execution of this method depend on the specified preprocessing identifier
The syntax for specifying this property is as follows:
[Conditional(
conditionalSymbol
)]
Use Cases:
#define Debug //有这个就打印两个,没这个就打印一个
using System;
using System.Diagnostics;
class MainClass
{
public static void Main()
{
Fun1();
Fun2();
}
[Conditional("Debug")]
public static void Fun1()
{
Console.WriteLine("Fun1");
}
public static void Fun2()
{
Console.WriteLine("Fun2");
}
}
1.2.3 Obsolete
You can add the Obsolete feature to classes, attributes, fields, and methods to mark the target element as obsolete. Whether the
second parameter of the Obsolete feature is error or not is false by default, which does not affect the compilation and will only prompt a warning. If it is true, the compilation will not pass
The syntax for specifying this property is as follows:
[Obsolete(
message
)]
[Obsolete(
message,
iserror
)]
- The parameter message , is a string describing why the item is obsolete and what should be used instead.
- The parameter iserror , is a boolean value. If the value is true, the compiler should treat the use of this item as an error. The default is false (the compiler generates a warning).
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();
}
}
When you try to compile the program, the compiler gives an error message stating:
Don't use OldMethod, use NewMethod instead
quote
C# Attribute (Attribute) bzdww