获取对象字段的值

        static void Main(string[] args)
        {
            Person ps = new Person();
            ps.Name = "CTZ";
            ps.Age = 21;

            Console.WriteLine(ps.GetValue("Name"));
            Console.WriteLine(ps.GetValue("Age"));
            Console.Read();
        }
        public class Person
        {
            public string Name
            { get; set; }

            public int Age
            { get; set; }
            public object GetValue(string propertyName)
            {
                return this.GetType().GetProperty(propertyName).GetValue(this, null);
            }
        }

 

posted @ 2021-12-13 17:15  .NET____小白  阅读(125)  评论(0编辑  收藏  举报