C # Some default predefined attributes

Source: Internet
Author: User

C # For some default predefined attributes, see the following table:

Predefined attribute Effective Target description
AttributeUsage Class specifies the effective usage of another attribute Class
CLSCompliant indicates whether the program element is compatible with CLS.
Conditional Method indicates that if no correlated 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) indicates that the default program model is multi-thread (MTA)
In addition to Assembly, Module, Parameter, and Return, Obsolete marks an element as unavailable, notifying users that this element will be used by future products.
ParamArray Parameter allows a single Parameter to be implicitly treated as a params (array) Parameter
Serializable Class, Struct, enum, and delegate specify that all public and private fields of this type can be serialized.
NonSerialized Field is applied to Fields marked as serializable classes, indicating that these fields will not be serializable
StructLayout Class, struct specifies the nature of the data layout of a Class or structure, such as Auto, Explicit, or sequential
ThreadStatic Field (static) implements 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.
1. [STAThread] and [MTAThread] attributes
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. AttributeUsage attributes
You can also use the AttributeUsage attribute to define how you use these attributes in addition to the custom attributes of the General C # type. The AttributeUsage attribute of a file record is called as follows:
[AttributeUsage (validon, AllowMutiple = allowmutiple, Inherited = inherited)]
The Validon parameter is of the AttributeTargets type. The enumerated value is defined 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 an 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, an error is returned.
[Something ("abc")]
[Something ("def")]
Class Myclass
{
}
The Inherited parameter indicates whether the property can be Inherited. The default value is false.
Inherited AllowMultiple result
True false: the property derived from the parameter overwrites the base property.
True false: The derived attributes coexist with the basic attributes.
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. 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: def
Custom 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. Conditional attributes
You can attach this attribute to a 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

Related Article

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.