C#之反射(PropertyInfo类)

1、引入命名空间:System.Reflection;程序集:mscorlib(在mscorlib.dll中)

2、示例代码(主要是getType()、setValue()、getValue()方法):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 反射
{
    class Program
    {
        /// <summary>
        /// 反射描述了程序集、模块、类型的对象(Type类型)。可以使用反射动态创建类型的实例,将实例绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性
        /// 如果代码中使用了特性,可以利用反射来访问它们
        /// </summary>
        static void Main(string[] args)
        {

            //1、GetType()的解释及示例
            //
            // 摘要:
            //     获取当前实例的 System.Type。
            //
            // 返回结果:
            //     System.Type 实例,表示当前实例的确切运行时类型。

            //下面使用静态方法GetType(从Object基类派生的所有类型都继承该方法)获取其类型的简单反射示例
            Student stu1 = new Student() { id = 1, name = "张三", length = 175, datetime = DateTime.Now };//
            Student stu2 = new Student() { id = 2, name = "李四", length = 180, datetime = DateTime.Now };//name = "李四", datetime = DateTime.Now

            var list = new List<Student>();
            list.Add(stu1);
            list.Add(stu2);

            Type stuType = stu1.GetType();
            Type listType = list.GetType();

            Console.WriteLine("---------------getType()输出类型----------------");
            Console.WriteLine(stuType);//输出Student的类型
            Console.WriteLine(stu1.id.GetType());//输出Student的id的类型
            Console.WriteLine(stu1.name.GetType());//输出Student的name的类型
            Console.WriteLine(stu1.datetime.GetType());//输出Student的datetime的类型

            Console.WriteLine(listType);//输出List的类型


            //2、GetProperties()的解释及示例
            //
            // 摘要:
            //     返回当前 System.Type 的所有公共属性。
            //
            // 返回结果:
            //     表示当前 System.Type 的所有公共属性的 System.Reflection.PropertyInfo 对象数组。- 或 -如果当前 System.Type
            //     没有公共属性,则为 System.Reflection.PropertyInfo 类型的空数组。

            PropertyInfo[] stuPropertyS = stuType.GetProperties();
            PropertyInfo[] listPropertyS = listType.GetProperties();

            //Console.WriteLine(stuPropertyS.Count());
            //Console.WriteLine(stuPropertyS.Length);

            //3、SetValue()的解释及示例
            //
            // 摘要:
            //     用索引属性的可选索引值设置该属性的值。
            //
            // 参数:
            //   obj:
            //     将设置其属性值的对象。
            //
            //   value:
            //     此属性的新值。
            //
            //   index:
            //     索引化属性的可选索引值。对于非索引化属性,此值应为 null。
            //
            // 异常:
            //   System.ArgumentException:
            //     index 数组不包含所需类型的参数。- 或 -未找到该属性的 set 访问器。
            //
            //   System.Reflection.TargetException:
            //     该对象与目标类型不匹配,或者某属性是实例属性但 obj 为 null。
            //
            //   System.Reflection.TargetParameterCountException:
            //     index 中参数的数目与已编制索引的属性所采用的参数的数目不相符。
            //
            //   System.MethodAccessException:
            //     尝试非法访问某类中私有或受保护的方法。
            //
            //   System.Reflection.TargetInvocationException:
            //     设置属性值时出错。例如,为索引属性指定的索引值超出范围。System.Exception.InnerException 属性指示错误的原因。
            Console.WriteLine("---------------setValue()给某属性赋值----------------");
            PropertyInfo stuPro = stuPropertyS.FirstOrDefault(x => x.Name == "name");//这里是找name的属性名
            stuPro.SetValue(stu1, "王五", null);
            Console.WriteLine(stu1.name);


            //4、GetType()方法的解释和示例
            //
            // 摘要:
            //     用索引化属性的可选索引值返回指定对象的该属性值。
            //
            // 参数:
            //   obj:
            //     将返回其属性值的对象。
            //
            //   index:
            //     索引化属性的可选索引值。 对于非索引化属性,该值应为 null。
            //
            // 返回结果:
            //     指定对象的属性值。
            //
            // 异常:
            //   System.ArgumentException:
            //     index 数组不包含所需类型的参数。 - 或 - 未找到该属性的 get 访问器。
            //
            //   System.Reflection.TargetException:
            //     该对象与目标类型不匹配,或者某属性是实例属性但 obj 为 null。
            //
            //   System.Reflection.TargetParameterCountException:
            //     index 中参数的数目与已编制索引的属性所采用的参数的数目不相符。
            //
            //   System.MethodAccessException:
            //     尝试非法访问某类中私有或受保护的方法。
            //
            //   System.Reflection.TargetInvocationException:
            //     检索属性值时出错。 例如,为索引属性指定的索引值超出范围。 System.Exception.InnerException 属性指示错误的原因。
            Console.WriteLine("---------------getValue()遍历属性值----------------");
            foreach (var v in stuPropertyS)//遍历属性
            {
                Console.WriteLine(v.GetValue(stu1, null));
            }


            Console.ReadLine();
        }
    }

    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }
        public decimal length { get; set; }
        public DateTime datetime { get; set; }
    }
}

3、运行结果:

在这里插入图片描述

4、下面是PropertyInfo 类型主要公开的成员,可供参考(摘自:http://msdn.microsoft.com/zh-cn/library/system.reflection.propertyinfo(v=vs.110).aspx)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
转自https://www.cnblogs.com/cocoon/p/4997030.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值