zoukankan      html  css  js  c++  java
  • 获取当前实例的字段值

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

    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; }
            }
        }
    }
    作者:KKcat
        
    个人博客:http://jinzhao.me/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    c++ new 堆 栈
    c++ int 负数 补码 隐式类型转换
    python json 序列化
    %pylab ipython 中文
    matplotlib中什么是后端
    IPython 4.0发布:Jupyter和IPython分离后的首个版本
    ipython
    python 类
    python 高级特性
    windows网络模型
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1450524.html
Copyright © 2011-2022 走看看