C#根据属性名称获取字段的值

示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FanSheTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            dog.Age = 2;
            dog.Name = "XiaoHei";

            Console.WriteLine(dog.GetType().GetProperty("Name").GetValue(dog));
            Console.WriteLine(dog.GetType().GetProperty("Age").GetValue(dog));

            Console.ReadKey();
        }
    }

    public class Dog
    {
        public string Name { get; set; }

        public int Age { get; set; }



    }
}

 

C#根据属性名称获取字段的值

上一篇:Win10 C盘空间越来越小怎么办?


下一篇:读《大话设计模式》——应用工厂模式的"商场收银系统"(WinForm)