[DefaultValue(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        public override bool Visible { get; set; }
如何通过反射的方式取得属性标签,比如:Browsable, 多谢。

解决方案 »

  1.   

    通过反射获取特性: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; }
        }
    }
      

  2.   


    System.Web.UI.DataSourceControl [DefaultValue(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [Browsable(false)]
    public override bool Visible { get; set; }
    还是有点疑问, 比如怎么取得System.Web.UI.DataSourceControl 下的Visible中的所有标签属性呢?
      

  3.   

    动动手,答案就在手边PropertyInfo 还有个重载方法: propInfo.GetCustomAttributes(bool);返回 object[] —— 全部的特性