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; }



    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM