通过反射获取特性:using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ReflectorAttributes { class Program { static void Main(string[] args) { Test test = new Test(); var propInfo = test.GetType().GetProperty("Name"); var myAttribute = (MyAttribute)propInfo.GetCustomAttributes(typeof(MyAttribute), true).FirstOrDefault(); Console.WriteLine(myAttribute.Name); Console.Read(); } } public class Test { [MyAttribute(Name="test")] public string Name { get; set; }
} [AttributeUsage(AttributeTargets.Property)] public class MyAttribute : Attribute { public string Name { get; set; } } }
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ReflectorAttributes
{
class Program
{
static void Main(string[] args)
{
Test test = new Test();
var propInfo = test.GetType().GetProperty("Name");
var myAttribute = (MyAttribute)propInfo.GetCustomAttributes(typeof(MyAttribute), true).FirstOrDefault();
Console.WriteLine(myAttribute.Name);
Console.Read();
}
} public class Test
{
[MyAttribute(Name="test")]
public string Name { get; set; }
} [AttributeUsage(AttributeTargets.Property)]
public class MyAttribute : Attribute
{
public string Name { get; set; }
}
}
System.Web.UI.DataSourceControl [DefaultValue(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public override bool Visible { get; set; }
还是有点疑问, 比如怎么取得System.Web.UI.DataSourceControl 下的Visible中的所有标签属性呢?