Some common attributes in C #

Source: Internet
Author: User
C # For some default predefined attributes, see the following table:

Predefined attributes Effective Target Description
AttributeUsage Class Specify the valid usage of another attribute class
CLSCompliant All Indicates whether the program element is compatible with CLS.
Conditional Method Indicates that if no associated string is defined, the compiler can ignore any call to this method.
DllImport Method Specifies the DLL location that contains the implementation of external Methods
STAThread Method (Main) Indicates that the default thread model of the program is STA.
MTAThread Method (Main) Specifies that the default program model is multi-thread (MTA)
Obsolete Besides Assembly, Module, Parameter, and Return Mark an element as unavailable, notifying users that this element will be used by future products
ParamArray Parameter Allows a single parameter to be treated implicitly as a params (array) parameter
Serializable Class, Struct, enum, delegate All public and private fields of this type can be serialized.
NonSerialized Field Applies to Fields marked as serializable classes, indicating that these fields cannot be serialized
StructLayout Class, struct Specifies the nature of the data layout of a class or structure, such as Auto, Explicit, or sequential
ThreadStatic Field (static) Implement local thread storage (TLS ). A given static field cannot be shared across multiple threads. Each thread has a copy of this static field.

The following describes several common attributes. [STAThread] and [MTAThread] Attribute class Class1 {[STAThread] Static void Main (string [] args) {}} use the STAThread attribute to specify the default thread model of the program as a single-thread model. Note that the thread model only affects applications that use COM interop. Applying this attribute to programs that do not use COM interop will not produce any effect. 2. The AttributeUsage attribute can be used to label custom attributes of the General C # type. You can also use the AttributeUsage attribute to define how you use these attributes. The AttributeUsage attribute of a file record is called as follows: public enum AttributeTargets {Assembly = 0x0001, Module = 0x0002, Class = 0x0004, Struct = 0x0008, Enum = 0x0010, Constructor = 0x0020, method = 0x0040, Property = 0x0080, Field = 0x0100, Event = 0x200, Interface = 0x400, Parameter = 0x800, Delegate = 0x1000, All = Assembly | Module | Class | Struct | Enum | Constructor | Method | Property | Filed | Event | Interface | Parameter | Deleagte, classMembers = | Class | Struct | Enum | Constructor | Method | Property | Field | Event | Delegate | Interface} AllowMultiple determines how many times a certain attribute can be used on a single Field, by default, all attributes are used at a time. Example: [AttributeUsage (AttributeTargets. all, AllowMultiple = true)] public class SomethingAttribute: Attribute {public SomethingAttribute (string str) {}} // If AllowMultiple = false, [Something ("abc")] [Something ("def")] the class Myclass {} Inherited parameter indicates whether the property can be Inherited. The default value is false.

Inherited AllowMultiple Result
True False The derived attribute overwrites the base attribute.
True False The derived attributes and the base attributes coexist.
Sample Code: using System; using System. reflection; namespace AttribInheritance {[AttributeUsage (AttributeTargets. all, AllowMultiple = true, // AllowMultiple = false, Inherited = true)] public class SomethingAttribute: Attribute {private string name; public string Name {get {return name ;} set {name = value ;}} public SomethingAttribute (string str) {this. name = str ;}} [Something ("abc")] class MyClass {} [Something ("def")] class Another: MyClass {} class Test {[STAThread] static void Main (string [] args) {Type type Type = Type. getType ("AttribInheritance. another "); foreach (Attribute attr in type. getCustomAttributes (true) // type. getCustomAttributes (false) {SomethingAttribute sa = attr as SomethingAttribute; if (null! = Sa) {Console. writeLine ("Custom Attribute: {0}", sa. name) ;}}}}when AllowMultiple is set to false, the result is: Custom Attribute: def. When AllowMultiple is set to true, the result is: Custom Attribute: defCustom Attribute: abc Note: if false is passed to GetCustomAttributes, it will not search for the inheritance tree, so you can only get the derived class attributes. 3. You can attach the Conditional attribute to the method. In this way, when the compiler calls this method, if the corresponding string value is not defined, the compiler ignores this call. For example, whether the following method is compiled depends on whether the string "DEGUG" is defined: [Condition ("DEBUG")] public void SomeDebugFunc () {Console. writeLine ("SomeDebugFunc");} using System; using System. diagnostics; namespace CondAttrib {class Thing {private string name; public Thing (string name) {this. name = name; # if DEBUG SomeDebugFunc (); # else SomeFunc (); # endif} public void SomeFunc () {Console. writeLine ("SomeFunc");} [Conditional ("DEBUG")] [Condit Ional ("ANDREW")] public void SomeDebugFunc () {Console. writeLine ("SomeDebugFunc") ;}} public class Class1 {[STAThread] static void Main (string [] args) {Thing t = new Thing ("T1 ");}}} 4. with the continuous development of the code, you may not need to use some methods. You can delete them all, but sometimes adding appropriate labels to them is more appropriate than deleting them, for example: using System; namespace ObsAttrib {class SomeClass {[Obsolete ("Don't use OldFunc, use NewFunc instead", true)] public void OldFunc () {Console. writeLine ("Oops");} public void NewFunc () {Console. writeLine ("Cool") ;}} class Class1 {[STAThread] static void Main (string [] args) {SomeClass SC = new SomeClass (); SC. newFunc (); // SC. oldFunc (); // compiler error }}} We set the second parameter of the Obsolete attribute to true. The compiler will generate an error when calling the function. E: \ InsideC # \ Code \ Chap06 \ ObsAttrib \ Class1.cs (20): 'obsattrib. someClass. oldFunc () 'expired: 'don' t use OldFunc, use NewFunc instead' 5. the DllImport and StructLayout attributes of DllImport allow C # code to call functions in the local code. C # code calls them through the runtime function of platform invoke. If you want the runtime environment to correctly organize the structure from the managed code into unmanaged code (or vice versa), you need to add attributes to the schema declaration. In order for structure parameters to be correctly grouped, The StructLayout attribute must be used to declare them, indicating that the data should be arranged strictly according to the format listed in the Declaration. If this is not done, data cannot be correctly grouped, and applications may encounter errors. Using System; using System. runtime. interopServices; // for DllImport namespace nativeDLL {public class Test {// [DllImport ("user32.dll")] // all the defaults are OK [DllImport ("user32 ", entryPoint = "MessageBoxA", SetLastError = true, CharSet = CharSet. ansi, ExactSpelling = true, CallingConvention = CallingConvention. stdCall)] public static extern int MessageBoxA (int h, string m, string c, int type); [Stru CtLayout (LayoutKind. sequential)] public class SystemTime {public ushort wYear; public ushort wMonth; public ushort hour; public ushort wDay; public ushort wHour; public ushort wMinute; public ushort wSecond; public ushort hour ;} [DllImport ("kernel32.dll")] public static extern void GetLocalTime (SystemTime st); [STAThread] public static void Main (string [] args) {MessageBoxA (0, "H Ello World "," nativeDLL ", 0); SystemTime st = new SystemTime (); GetLocalTime (st); string s = String. format ("date: {0}-{1}-{2}", st. wMonth, st. wDay, st. wYear); string t = String. format ("time: {0 }:{ 1 }:{ 2}", st. wHour, st. wMinute, st. wSecond); string u = s + "," + t; MessageBoxA (0, u, "Now", 0) ;}} 6. when using accessory attributes. NET will automatically generate an AssemblyInfo. cs source code file and application source code file. AssemblyInfo. cs contains the code in the accessory. Some of the information is purely information, while other information enables the runtime environment to ensure the unique name and version number for the Customer Code to reuse your accessories. 7. The context property. NET Cabinet also provides another property: context property. Context properties provide an intercept mechanism that can be processed before and after class instantiation and method calling. This function is used for remote object calls. It is used from the COM + component Services and Microsoft Transaction Services (MTS) used by the COM-based system ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.