Attribute can add some additional information to a class or method, we can look at MSDN's description of attribute:
The common language runtime allows you to add a descriptive declaration like a keyword, called attributes, that labels elements in your program, such as types, fields, methods, and properties. Attributes and Microsoft. NET Framework file metadata are saved together to describe your code to the runtime, or to affect the behavior of your application as it runs.
In. NET, attribute is used to handle a variety of problems, such as serialization, the security features of the program, preventing the immediate compiler from optimizing program code, code debugging, and so on.
Built-in attribute
C # gives us something to deal with specific issues, and we'll look at a few simple features:
Conditional
The specified method is invoked only if the specified condition is met.
1 usingSystem;2 usingSystem.Diagnostics;3 4 namespaceStudy5 {6 class Program7 {8 Static voidMain (string[] args)9 {Ten Func (); One A Console.read (); - } - the[Conditional ("DEBUG")] - Private Static voidFunc () - { -Console.WriteLine ("I am a DEBUG function!"); + } - } +}
Running in debug mode can see the string output, while in release mode Func is not called.
Obsolete
You can specify whether a class or method has been deprecated, the first argument is a description string, and the second parameter specifies whether a class or method is used directly to throw a mistake.
1 usingSystem;2 usingSystem.Diagnostics;3 4 namespaceStudy5 {6 class Program7 {8 Static voidMain (string[] args)9 {Ten Func (); One A Console.read (); - } - the[Obsolete ("Don ' t use this method!",true)] - Private Static voidFunc () - { -Console.WriteLine ("I am a DEBUG function!"); + } - } +}
Oh, you run to see the effect of it.
Serializable
Use this feature to implement serialization and deserialization of a class.
1 usingSystem;2 usingSystem.IO;3 usingSystem.Runtime.Serialization;4 usingSystem.Runtime.Serialization.Formatters.Binary;5 6 namespaceStudy7 {8 class Program9 {Ten Static voidMain (string[] args) One { ATest test =NewTest {name ="Lilei", age = -}; - - byte[] bytes =objecttobytes (test); theTest test2 = bytestoobject (bytes) asTest; - -Console.WriteLine ("Name:"+ Test2.name +", Age:"+test2.age); - + Console.read (); - } + A Public Static byte[] Objecttobytes (Objectobj) at { - using(MemoryStream ms =NewMemoryStream ()) - { -IFormatter formatter =NewBinaryFormatter (); - Formatter. Serialize (MS, obj); - returnMs. GetBuffer (); in } - } to + Public Static ObjectBytestoobject (byte[] Bytes) - { the using(MemoryStream ms =NewMemoryStream (Bytes)) * { $IFormatter formatter =NewBinaryFormatter ();Panax Notoginseng returnFormatter. Deserialize (MS); - } the } + } A the [Serializable] + Public classTest - { $ Public stringname; $ Public intAge ; - } -}
Note An object that can use two static serialization methods must be an object that uses the Serializable attribute.
Custom attribute
In addition to using the official features, we can customize a feature according to our own needs, using the following methods:
- A new class inherits from System.Attribute, and the name needs to end with Attribute, such as Versionattribute, and we can add the attributes we need to the class.
- Use "[Versionattribute]" or "[Version]" to define a attribute, and you can set properties.
- It is generally used in the form of reflection to take the information we need to be removed and processed.
Give a few Good articles:
Http://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html
Http://www.cnblogs.com/luckdv/articles/1682488.html
C # Learning Note (16): Attribute