mrfangzheng

Hope and fear are useless. Be confident, and always be prepared for the worst.
  首页  :: 新随笔  :: 联系 :: 管理

C#中常用的Attribute搜集(刚开始...)

Posted on 2008-06-02 09:32  mrfangzheng  阅读(904)  评论(0编辑  收藏  举报

ObsoleteAttribute

名字空间: System

函数已经不推荐使用

 

FlagsAttribute

名字空间: System

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

All possible combinations of values of an Enum with FlagsAttribute:

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

名字空间: System

标记一个字段是否被序列化

 

DllImportAttribute

Win32 API 函数

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

 

WebMethod

名字空间: System.Web.Services

WebService方法, 有6个属性, 分别是 BufferResponse, CacheDuration, Description, EnableSession, MessageName, TransactionOption

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

DataObjectAttribute, DataObjectFieldAttribute, DataObjectMethodAttribute

名字空间: 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

名字空间: System.ComponentModel

标记一个属性是否从周围环境获得值, 比如: 一个空间的背景色自动为其父空间的背景色

 

BindableAttribute

名字空间: System.ComponentModel

标记一个属性是否可以被绑定

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

 

BrowsableAttribute, CategoryAttribute, DisplayNameAttribute,DescriptionAttribute

DefaultEventAttribute, DefaultPropertyAttribute, DefaultValueAttribute,

DesignerAttribute, DesignerCategoryAttribute, DesignTimeVisibleAttribute, DesignOnlyAttribute

名字空间: System.ComponentModel

设计一个GUI组件时, 标记属性, 事件, 方法在设计器如何显示.

参见: Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0

 

ConditionalAttribute

名字空间: 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