using
System;
using System.Reflection;
class Program
... {
static void Main()
...{
System.Attribute[] attrs = Attribute.GetCustomAttributes(typeof(xxx));
foreach (System.Attribute attr in attrs)
...{
if (attr is aaa)
...{
aaa a = (aaa)attr;
Console.WriteLine("name={0},version={1}",a.Name,a.Version);
}
}
}
}
[aaa( " nid " ,Version = 1 )]
public class xxx
... {
//...
}
public class aaa : Attribute
... {
public aaa(string name)
...{
this.name = name;
}
private string name;
public string Name
...{
get ...{ return name; }
set ...{ name = value; }
}
private int version;
public int Version
...{
get ...{ return version; }
set ...{ version = value; }
}
}
using System.Reflection;
class Program
... {
static void Main()
...{
System.Attribute[] attrs = Attribute.GetCustomAttributes(typeof(xxx));
foreach (System.Attribute attr in attrs)
...{
if (attr is aaa)
...{
aaa a = (aaa)attr;
Console.WriteLine("name={0},version={1}",a.Name,a.Version);
}
}
}
}
[aaa( " nid " ,Version = 1 )]
public class xxx
... {
//...
}
public class aaa : Attribute
... {
public aaa(string name)
...{
this.name = name;
}
private string name;
public string Name
...{
get ...{ return name; }
set ...{ name = value; }
}
private int version;
public int Version
...{
get ...{ return version; }
set ...{ version = value; }
}
}
跑跑感觉一下
自定义属性可以用来标记某个类或方法是否具有某种属性
自定义属性可以用来标记某个类或方法是否具有某种属性