您的位置:首页 >> 编程开发 >> .NET >> .NET Framework >> 正文
.NET Framework RSS
 

使用.Net Reflection 获取对象字段内容

http://www.rdxx.com 08年07月11日 00:00 我要投稿

关键词: 字段 , 内容 , 获取 , Reflection , 对象 , .NET

        以下是对某个贴子的总结:

         class Program
        {
            static void Main(string[] args)
            {
                test t = new test();//创建实例
                t.addtolist(1,"b");//集合中加如字段b
                System.Console.WriteLine(" number = " + t.l.Count + "\nhave 1: " + t.l.ContainsKey(1));
                System.Console.WriteLine(" value = " + t.l[1]);
                System.Console.In.ReadLine();
            }
        }

         public class test
        {
            public byte a=33;
            public Double b=22;
            public int c;
            public Dictionary <int, object> l = new Dictionary <int, object>();
            public void addtolist(int ID,string FieldName)
            {
                System.Reflection.FieldInfo field = this.GetType().GetField(FieldName);
                if (field == null)
                {
                    //should do something
                    System.Console.WriteLine("Cannot get the field: {0}.\n", FieldName);
                    System.Console.In.ReadLine();
                    return;
                }
                System.Console.WriteLine("Name = " + field.Name + " type = " + field.FieldType);
                System.Console.WriteLine("value = " + field.GetValue(this) );
                l.Add(ID, field.GetValue(this));
            }
        }

共2页  第1页 第2页


 
 
标签: 字段 , 内容 , 获取 , Reflection , 对象 , .NET 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站