C#遍历类的属性 PropertyInfo.Attributes

 

PropertyInfo.Attributes 属性

此属性表示与成员关联的特性。 所有成员都具有相对于特定成员类型定义的特性集。 属性特性使用户能够知道此属性是否是默认属性、SpecialName 属性等等。

若要获取 Attributes 属性,请先获取类类型。 从 Type 获取 PropertyInfo PropertyInfo 获取特性。

官方示例:获取类的特性

using System;
using System.Reflection;

public class Myproperty
{
    private string caption = "Default caption";
    public string Caption
    {
        get{return caption;}
        set {if(caption!=value) {caption = value;}
        }
    }
}

class Mypropertyinfo
{
    public static int Main(string[] args)
    {
        Console.WriteLine("\nReflection.PropertyInfo");

        // Define a property.
        Myproperty Myproperty = new Myproperty();
        Console.Write("\nMyproperty.Caption = " + Myproperty.Caption);

        // Get the type and PropertyInfo.
        Type MyType = Type.GetType("Myproperty");
        PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption");

        // Get and display the attributes property.
        PropertyAttributes Myattributes = Mypropertyinfo.Attributes;

        Console.Write("\nPropertyAttributes - " + Myattributes.ToString());

        return 0;
    }
}


 

官方参考:http://msdn.microsoft.com/zh-cn/library/system.reflection.propertyinfo.attributes

 ****************************************************************************************************

一个例子:  注意:貌似对字段无效

先建一个类User

namespace ClassLibrary1
{
    public class User    
    {       
        private int userid = 1;
        public int Userid
        { 
            get { return userid; }
            set { userid = value; }
        }  
        private string userName = "jghg";       
        public string UserName{
            get { return userName; } 
            set { userName = value; }
        }
        private string address = "ghjghj";
        public string Address{ 
            get { return address; } 
            set { address = value; }
        }        
        private string email = "jhgjhg";
        public string Email{
            get { return email; }
            set { email = value; }
        }
        private string phone = "ghjgjg";
        public string Phone
        { 
            get { return phone; }
            set { phone = value; }
        }
    }
}


接着在主程序中获取类的属性,看代码

namespace ConsoleApplication2 { 
    class Program { 
        static void Main(string[] args) 
        { 
            Type type = typeof(ClassLibrary1.User); 
            object obj = Activator.CreateInstance(type); 
            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); 
            foreach (PropertyInfo p in props) { 
                Console.WriteLine(p.Name); 
            } 
            Console.ReadLine(); 
        }
    } 
}


需要引入命名空间:

using System.Reflection;

转自:http://www.cnblogs.com/JinvidLiang/archive/2010/11/08/1871733.html

 

*****************************************************************************************************************

让你省写大量重复代码的方法 使用PropertyInfo类 反射获取类 的类型

http://blog.csdn.net/vip__888/article/details/5500588

合理利用PropertyInfo.SetValue()

http://archive.cnblogs.com/a/2013446/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值