Loading

获取当前实例的字段值

其实会获取字段值,其它的也应该没问题了。^_^

using System;
using System.Reflection;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat c = new Cat();
            c.name = "mao";
            c.age = 1;
            ShowValues(c);
            Console.ReadLine();
        }

        static void ShowValues(Cat c)
        {
            Type t = c.GetType();
            foreach (FieldInfo f in t.GetFields())
            {
                Console.WriteLine(t.InvokeMember(f.Name, BindingFlags.GetField, null, c, null).ToString ());
            }
        }
    }
    public class Cat
    {
        public int age;
        public string name;
        public string CatName
        {
            get { return name; }
        }
    }
}
posted @ 2009-05-06 11:35  today4king  阅读(333)  评论(0编辑  收藏  举报