using System;
using System.ComponentModel;
using System.Linq;
[Category("nice")]
class Foo { }
static class Program
{
static void Main()
{
var ca = TypeDescriptor.GetAttributes(typeof(Foo))
.OfType<CategoryAttribute>().FirstOrDefault();
Console.WriteLine(ca.Category); // <=== nice
TypeDescriptor.AddAttributes(typeof(Foo),new CategoryAttribute("naughty"));
ca = TypeDescriptor.GetAttributes(typeof(Foo))
.OfType<CategoryAttribute>().FirstOrDefault();
Console.WriteLine(ca.Category); // <=== naughty
}
}
转载于:https://www.cnblogs.com/jacle169/archive/2013/04/20/3032113.html