Csharp/CSharp Tutorial/Attribute/Attribute Definition
Содержание
- 1 A custom attribute based on bool value
- 2 Assembly level attributes don"t have to be in the assemblyinfo.cs file, but must be lised outside of any namespae definition.
- 3 Attribute with supplement information
- 4 Create Attribute
- 5 Positional vs. Named Parameters
- 6 Use a named attribute parameter.
- 7 Use a property as a named attribute parameter.
A custom attribute based on bool value
using System;
public class TrueFalseAttribute : Attribute
{
bool bWritten;
public bool Written()
{
return bWritten;
}
public TrueFalseAttribute(bool Written)
{
bWritten = Written;
}
}
[TrueFalseAttribute(true)]
public class Class1
{
}
[TrueFalseAttribute(false)]
public class Class2
{
}
class MainClass
{
public static void Main()
{
TrueFalseAttribute u;
Console.Write("Class1 TrueFalseAttribute attribute: ");
u = (TrueFalseAttribute) Attribute.GetCustomAttribute(typeof(Class1), typeof(TrueFalseAttribute));
Console.WriteLine(u.Written());
Console.Write("Class2 TrueFalseAttribute attribute: ");
u = (TrueFalseAttribute) Attribute.GetCustomAttribute(typeof(Class2), typeof(TrueFalseAttribute));
Console.WriteLine(u.Written());
}
}
Class1 TrueFalseAttribute attribute: True Class2 TrueFalseAttribute attribute: False
Assembly level attributes don"t have to be in the assemblyinfo.cs file, but must be lised outside of any namespae definition.
using System;
[assembly:System.CLSCompliantAttribute(true)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class MyDescriptionAttribute : System.Attribute
{
private string description;
public string Desc
{
get { return description; }
set { description = value; }
}
public MyDescriptionAttribute() {}
public MyDescriptionAttribute(string desc)
{ description = desc;}
}
[MyDescriptionAttribute("Info")]
public class MyClass
{
public MyClass()
{
}
}
public class MainClass
{
public static int Main(string[] args)
{
Type t = typeof(MyClass);
// Get all attributes in the assembly.
object[] customAtts = t.GetCustomAttributes(false);
// List all info.
Console.WriteLine("Value of MyDescriptionAttribute");
foreach(MyDescriptionAttribute v in customAtts)
Console.WriteLine("-> {0}\n", v.Desc);
return 0;
}
}
Value of MyDescriptionAttribute -> Info
Attribute with supplement information
using System;
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute {
string remark;
public string supplement;
public MyAttribute(string comment) {
remark = comment;
supplement = "None";
}
}
Create Attribute
using System;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public class AuthorAttribute : System.Attribute
{
public string Company;
public string Name;
public AuthorAttribute(string name)
{
this.Name = name;
Company = "";
}
}
[Author("Name1")]
[Author("Name2", Company = "Ltd.")]
class MainClass
{
public static void Main()
{
Type type = typeof(MainClass);
object[] attrs = type.GetCustomAttributes(typeof(AuthorAttribute), true);
foreach (AuthorAttribute a in attrs)
{
Console.WriteLine(a.Name + ", " + a.rupany);
}
}
}
Name2, Ltd. Name1,
Positional vs. Named Parameters
- A positional parameter is linked by its position.
- Positional parameters must be specified in the order in which they appear.
- Named parameters are specified by assigning values to their names.
- For an attribute, you can also create named parameters
- named parameters can be assigned initial values by using their names.
- A named parameter is supported by either a public field or property, which must not be read-only.
Here is the general form of an attribute specification that includes named parameters:
[attrib(positional-param-list, named-param1 = value, named-param2 = value, ...)]
Use a named attribute parameter.
using System;
using System.Reflection;
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute {
public string remark;
public string supplement;
public MyAttribute(string comment) {
remark = comment;
supplement = "None";
}
public string Remark {
get {
return remark;
}
}
}
[MyAttribute("This class uses an attribute.",
supplement = "This is additional info.")]
class UseAttrib {
}
class MainClass {
public static void Main() {
Type t = typeof(UseAttrib);
Console.Write("Attributes in " + t.Name + ": ");
object[] attribs = t.GetCustomAttributes(false);
foreach(object o in attribs) {
Console.WriteLine(o);
}
// Retrieve the MyAttribute.
Type tRemAtt = typeof(MyAttribute);
MyAttribute ra = (MyAttribute)
Attribute.GetCustomAttribute(t, tRemAtt);
Console.Write("Remark: ");
Console.WriteLine(ra.remark);
Console.Write("Supplement: ");
Console.WriteLine(ra.supplement);
}
}
Attributes in UseAttrib: MyAttribute Remark: This class uses an attribute. Supplement: This is additional info.
Use a property as a named attribute parameter.
using System;
using System.Reflection;
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute {
public string remark;
public int priority;
public string supplement;
public MyAttribute(string comment) {
remark = comment;
supplement = "None";
}
public string Remark {
get {
return remark;
}
}
public int Priority {
get {
return priority;
}
set {
priority = value;
}
}
}
[MyAttribute("This class uses an attribute.",
supplement = "This is additional info.",
Priority = 10)]
class UseAttrib {
}
class MainClass {
public static void Main() {
Type t = typeof(UseAttrib);
Console.Write("Attributes in " + t.Name + ": ");
object[] attribs = t.GetCustomAttributes(false);
foreach(object o in attribs) {
Console.WriteLine(o);
}
// Retrieve the MyAttribute.
Type tRemAtt = typeof(MyAttribute);
MyAttribute ra = (MyAttribute)
Attribute.GetCustomAttribute(t, tRemAtt);
Console.Write("Remark: ");
Console.WriteLine(ra.remark);
Console.Write("Supplement: ");
Console.WriteLine(ra.supplement);
Console.WriteLine("Priority: " + ra.priority);
}
}
Attributes in UseAttrib: MyAttribute Remark: This class uses an attribute. Supplement: This is additional info. Priority: 10