C # commonly used Attribute collection (beginning ...)

Source: Internet
Author: User

ObsoleteAttribute

Namespace: System

Functions are no longer recommended.

 

FlagsAttribute

Namespace: System

[FlagsAttribute]enum class MultiHue : short{   Black = 0,   Red = 1,   Green = 2,   Blue = 4};

All possible combinations of values of an Enum with flagsattriations:

0-Black 1-Red 2-Green 3-Red, Green 4-Blue 5-Red, Blue 6-Green, Blue 7-Red, Green, Blue 8-8

 

SerializableAttribute, NonSerializedAttribute

Namespace: System

Mark whether a field is serialized

 

DllImportAttribute

Win32 API functions

   1:  [DllImport("User32.dll", EntryPoint="MessageBox")]
   2:  static extern int MessageDialog(int hWnd, string msg, string caption, int msgType);

 

WebMethod

Namespace: System. Web. Services

The WebService method has six attributes: BufferResponse, CacheDuration, Description, EnableSession, MessageName, and TransactionOption.

   1:  [WebMethod(TransactionOption=TransactionOption.RequiresNew) ]
   2:  public void Transfer(long Amount, long AcctNumberTo, long AcctNumberFrom) 
   3:  {
   4:  }

DataObjectAttribute, DataObjectFieldAttribute, DataObjectMethodAttribute

Namespace: System. ComponentModel

[DataObjectAttribute]public class NorthwindData{    public NorthwindData() {}  private int _employeeID;  [DataObjectFieldAttribute(true, true, false)]  public int EmployeeID  {    get { return _employeeID; }    set { _employeeID = value; }  }  [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]  public static IEnumerable GetAllEmployees()  {    AccessDataSource ads = new AccessDataSource();    ads.DataSourceMode = SqlDataSourceMode.DataReader;    ads.DataFile = "~//App_Data//Northwind.mdb";    ads.SelectCommand = "SELECT EmployeeID,FirstName,LastName FROM Employees";    return ads.Select(DataSourceSelectArguments.Empty);  }  // Delete the Employee by ID.  [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]  public void DeleteEmployeeByID(int employeeID)  {    throw new Exception("The value passed to the delete method is "                         + employeeID.ToString());  }}

 

AmbientValueAttribute

Namespace: System. ComponentModel

Indicates whether a property is obtained from the surrounding environment. For example, the background color of a space is automatically the background color of its parent space.

 

BindableAttribute

Namespace: System. ComponentModel

Mark whether an attribute can be bound

[Bindable(true)] public int MyProperty {    get {       // Insert code here.       return 0;    }    set {       // Insert code here.    } }

 

BrowsableAttribute, CategoryAttribute, DisplayNameAttribute, DescriptionAttribute

DefaultEventAttribute, defaproperpropertyattribute, defavaluvalueattribute,

DesignerAttribute, DesignerCategoryAttribute, DesignTimeVisibleAttribute, and DesignOnlyAttribute

Namespace: System. ComponentModel

When designing a GUI component, you can mark attributes, events, and how the method is displayed in the designer.

Refer to: Developing Custom Data-Bound Web Server Controls for ASP. NET 2.0.

 

ConditionalAttribute

Namespace: System. Diagnostics

class Test{    static void Main()    {                       Console.WriteLine("Calling Method1");        Method1(3);        Console.WriteLine("Calling Method2");        Method2();        Console.WriteLine("Using the Debug class");        Debug.Listeners.Add(new ConsoleTraceListener());        Debug.WriteLine("DEBUG is defined");    }    [Conditional("CONDITION1")]    public static void Method1(int x)    {        Console.WriteLine("CONDITION1 is defined");    }    [Conditional("CONDITION1"), Conditional("Condition2")]      public static void Method2()    {        Console.WriteLine("CONDITION1 or Condition2 is defined");    }}

/*
When compiled as shown, the application (named ConsoleApp)
Produces the following output.

Csc ConsoleApp. cs
Calling Method1
Calling Method2
Using the Debug class

Csc/define: CONDITION1 ConsoleApp. cs
Calling Method1
CONDITION1 is defined
Calling Method2
CONDITION1 or Condition2 is defined
Using the Debug class

Csc/define: Condition2 ConsoleApp. cs
Calling Method1
Calling Method2
CONDITION1 or Condition2 is defined
Using the Debug class

Csc/define: DEBUG ConsoleApp. cs
Calling Method1
Calling Method2
Using the Debug class
DEBUG is defined
*/

 

DebuggerBrowserableAttribute, DebuggerDisplayAttribute, DebuggerHiddenAttribute, MonitoringDescriptionAttribute

DebuggerNonUserCodeAttribute

DebuggerStepperBoundaryAttribute

DebuggerStepThroughAttribute

DebuggerTypeProxyAttribute

SwitchAttribute

SwitchLevelAttribute

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.