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/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Centos7安装redis
    Cookie和Session的区别
    JavaWeb中的域对象
    ServletContext使用介绍
    Java Web核心组件之Servlet的使用介绍
    关于反射的杂谈
    leetcode117search-in-rotated-sorted-array
    23longest-consecutive-sequence
    leetcode24:word-ladder-ii
    leetcode25word-ladder
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1450524.html
Copyright © 2011-2022 走看看