C#高级:通过一个遍历实体的小案例去理解反射(基础版)

发布于:2024-08-11 ⋅ 阅读:(128) ⋅ 点赞:(0)

 一、任务一二:遍历、获取、设置字段的值

任务一:获取实体的所有字段,输出字段名、字段类型、字段值

任务二:获取实体的指定字段,输出该字段值、赋值该字段值

(entity和field都是属性,stu是带有数据的实体)

【不封装实现】

using ConsoleApp1;
using SqlSugar;
using System.Reflection;

class Program
{
    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }

        public int age { get; set; }
    }
    static void Main()
    {
        Student stu = new Student { id = 1, name = "小苏", age = 18 };

        //任务一:遍历打印这个实体的字段名,字段类型,字段的值
        foreach (var entity in typeof(Student).GetProperties())
        {
            Console.Write($"entity Name: {entity.Name}, ");

            Console.Write($"Type: {entity.PropertyType.Name}, ");

            Console.WriteLine($"Value: {entity.GetValue(stu)}");
        }

        //任务二:获取stu的age字段,将age的字段值设置为20

        //获取到Age属性
        var field = typeof(Student).GetProperties().First(x=>x.Name=="age");

        //通过属性反射给实体赋值
        field.SetValue(stu, 20);

        //查看赋值后的结果
        var resultage = field.GetValue(stu);
        Console.WriteLine(resultage);
        ;
    }
}

任务一:获取实体的所有字段,输出字段名、字段类型、字段值

任务二:获取实体的指定字段,输出该字段值、赋值该字段值

(entity和field都是属性,stu是带有数据的实体)

【小封装实现-更好理解】

using ConsoleApp1;
using SqlSugar;
using System.Reflection;

class Program
{
    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }

        public int age { get; set; }
    }
    /// <summary>
    /// 获取实体的所有属性
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    static PropertyInfo[] GetEntity<T>() where T : class
    {
        return typeof(T).GetProperties();

    }
    /// <summary>
    /// 获取实体的某个属性
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="Fieldname"></param>
    /// <returns></returns>
    static PropertyInfo GetField<T>(string Fieldname) where T : class
    {
        return typeof(T).GetProperties().First(x => x.Name == Fieldname);
    }


    static void Main()
    {
        Student stu = new Student { id = 1, name = "小苏", age = 18 };

        //任务一:遍历打印这个实体的字段名,字段类型,字段的值
        foreach (var entity in GetEntity<Student>())
        {
            Console.Write($"entity Name: {entity.Name}, ");

            Console.Write($"Type: {entity.PropertyType.Name}, ");

            Console.WriteLine($"Value: {entity.GetValue(stu)}");
        }

        //任务二:获取stu的age字段,将age的字段值设置为20

        //获取到Age属性
        var field = GetField<Student>("age");

        //通过属性反射给实体赋值
        field.SetValue(stu, 20);

        //查看赋值后的结果
        var resultage = field.GetValue(stu);
        Console.WriteLine(resultage);//20
        
    }
}

(封装代码)

    /// <summary>
    /// 获取实体的所有属性(属性列表)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    static PropertyInfo[] GetEntity<T>() where T : class
    {
        return typeof(T).GetProperties();

    }

    /// <summary>
    /// 获取实体的某个属性(单个属性)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="Fieldname"></param>
    /// <returns></returns>
    static PropertyInfo GetField<T>(string Fieldname) where T : class
    {
        return typeof(T).GetProperties().First(x => x.Name == Fieldname);
    }

二、任务三:获取指定类型的字段名称

【封装的方法】

using ConsoleApp1;
using SqlSugar;
using System.Reflection;

class Program
{
    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }

        public int age { get; set; }
    }

    /// <summary>
    /// 获取实体的所有属性
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    static PropertyInfo[] GetEntity<T>() where T : class
    {
        return typeof(T).GetProperties();

    }


    static List<string> GetintField<T>(T entity) where T : class
    {
        List<string> result = new List<string>();
        foreach (var item in GetEntity<T>())
        {
            if (item.PropertyType==typeof(int))
            {
                result.Add(item.Name);
            }
        }
        return result;
    }

    static List<string> GetintField2<T>() where T : class
    {
        List<string> result = new List<string>();
        foreach (var item in GetEntity<T>())
        {
            if (item.PropertyType == typeof(int))
            {
                result.Add(item.Name);
            }
        }
        return result;
    }

    static void Main()
    {
        Student stu = new Student { id = 1, name = "小苏", age = 18 };

        //任务三:获取指定类型(例如int类型)的字段名称

        //以下两种方法都可以,只是入参不一样
        var result = GetintField(stu);

        var result2 = GetintField2<Student>();

        //输出id,age

    }
}


网站公告

今日签到

点亮在社区的每一天
去签到