利用反射获取类的所有字段

以下是利用反射获取类RefPoint中所有的字段。

 

代码一,RefPoint类

        public class RefPoint
        {
            public int x;
            public int y;
            public Class02 c02;
            private int z;
            public RefPoint(int x)
            {
                this.x = x;
            }
        }


        public class  Class02
        {
             
        }

 

代码二,打印出所有类RefPoint中的所有字段

            RefPoint refPoint01 = new RefPoint(3);
            Type typeObj = refPoint01.GetType();
            FieldInfo[] filedInfos = typeObj.GetFields();
            foreach (FieldInfo filedInfo in filedInfos)
            {
                Console.WriteLine(filedInfo.ToString());
            }

 

 

打印结果如下:(注意只能打印出public类型的字段)

 

posted @ 2015-04-02 13:49  Freshcoder  阅读(789)  评论(0编辑  收藏  举报