C# 객체에 반사하여 값을 지정합니다.

2998 단어
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; }
    }

 
전재 대상:https://www.cnblogs.com/binlyzhuo/p/6170502.html

좋은 웹페이지 즐겨찾기