• C#通过反射给对象赋值


    class Program
        {
            static void Main(string[] args)
            {
                UserSearchRequest model = new UserSearchRequest()
                {
                    Name = "'1'=0",
                    Age = 10
                };
    
    
                Type type = model.GetType();
                //var ps = type.GetProperties();
    
                var ps = type.GetProperties().Where(u=>u.PropertyType.FullName =="System.String").ToList();
    
                foreach (var p in ps)
                {
                    Console.WriteLine("PropertyName:{0},Value:{1}",p.Name,p.GetValue(model,null).ToString());
                    p.SetValue(model, "1001",null);
                    Console.WriteLine("PropertyName:{0},Value:{1}", p.Name, p.GetValue(model, null).ToString());
                }
    
                Console.ReadLine();
            }
        }
    
        public class UserSearchRequest
        {
            public string Name { set; get; }
            public int Age { set; get; }
        }
  • 相关阅读:
    json
    封装PDO
    PDO
    jquery练习
    jquery包
    jquery
    租房子 多条件查询
    查询
    新闻修改处理页面
    新闻添加数据
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/6170502.html
Copyright © 2020-2023  润新知