指的是你所定义的属性可以指派的对象:也就是你所列的:
AttributeTarget.Field //即这儿是这个属性可以指派到字段(field)上
AttributeTarget.Assembly
AttributeTarget.Class
AttributeTarget.Constructor
AttributeTarget.Delegete// Example 18-01: Working with custom attributesnamespace Programming_CSharp
{
using System;
using System.Reflection; // create custom attribute to be assigned to class members
//指出定义的属性信息可以用于:Class,Constructor.... [AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
public class BugFixAttribute : System.Attribute
{
// attribute constructor for
// positional parameters
public BugFixAttribute
(int bugID,
string programmer,
string date)
{
this.bugID = bugID;
this.programmer = programmer;
this.date = date;
} // accessor
public int BugID
{
get
{
return bugID;
}
} // property for named parameter
public string Comment
{
get
{
return comment;
}
set
{
comment = value;
}
} // accessor
public string Date
{
get
{
return date;
}
} // accessor
public string Programmer
{
get
{
return programmer;
}
}
// private member data
private int bugID;
private string comment;
private string date;
private string programmer;
}
// ********* assign the attributes to the class ********
//这儿是属性信息用于class
[BugFixAttribute(121,"Jesse Liberty","01/03/05")]
[BugFixAttribute(107,"Jesse Liberty","01/04/05",
Comment="Fixed off by one errors")]
public class MyMath
{
public double DoFunc1(double param1)
{
return param1 + DoFunc2(param1);
} public double DoFunc2(double param1)
{
return param1 / 3;
}
} public class Tester
{
public static void Main()
{
MyMath mm = new MyMath();
Console.WriteLine("Calling DoFunc(7). Result: {0}",
mm.DoFunc1(7));
}
}
}
可能会出错,或是不会产生期望中的属性信息
——我也没有用过
你可以试一下