use Reflection, for example:using System; using System.Reflection; using System.ComponentModel;public class test { private int _a; [CategoryAttribute("背景")] public int a {get{return _a;} set{_a=value;} } static void Main() { Type t = typeof(test); PropertyInfo pi = t.GetProperty("a"); if (pi != null) { object[] atts = pi.GetCustomAttributes(true); if (atts.Length > 0) { foreach (object at in atts) { if (at is CategoryAttribute) Console.WriteLine(((CategoryAttribute)at).Category); } } } } }
using System.Reflection;
using System.ComponentModel;public class test
{
private int _a;
[CategoryAttribute("背景")]
public int a
{get{return _a;}
set{_a=value;}
} static void Main()
{
Type t = typeof(test);
PropertyInfo pi = t.GetProperty("a");
if (pi != null)
{
object[] atts = pi.GetCustomAttributes(true);
if (atts.Length > 0)
{
foreach (object at in atts)
{
if (at is CategoryAttribute)
Console.WriteLine(((CategoryAttribute)at).Category);
}
}
}
}
}