一、使命一二:遍历、获取、设置字段的值
使命一:获取实体的全部字段,输出字段名、字段范例、字段值
使命二:获取实体的指定字段,输出该字段值、赋值该字段值
(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
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |