• List<T> 实现IList接口


    1 知识点:
      1.1 List<T> 实现IList接口
      1.2 System.Reflection 反射

    2 举例及实现(调试OK)

     

    using System;
    using System.Text;
    using System.Reflection;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace ConsoleApplication1
    {
        public class Node
        {
            private int _id;
            private int _pid;
            private string _name;
    
            public Node() { }
    
            public Node(int id, int pid, string name)
            {
                _id = id;
                _pid = pid;
                _name = name;
            }
    
            public int ID
            {
                get { return _id; }
                set { _id = value; }
            }
    
            public int PID
            {
                get { return _pid; }
                set { _pid = value; }
            }
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            public override string ToString()
            {
                return "ID:" + _id + "\t" + "PID:" + _pid + "\t" + "Name:" + _name;
            }
        }
    
        class B
        {
            private object _dataSource;
            public object DataSource
            {
                get { return _dataSource; }
                set { _dataSource = value; }
            }
    
            //怎么遍历DataSource,取出ID,PID,Name属性值?
            public void RecursionList()
            {
                if (this._dataSource == null) return;
                if (!(this._dataSource is IList)) return;
    
                IList nodeList = (IList)this._dataSource;//知识点1
                PropertyInfo[] props = nodeList[0].GetType().GetProperties();//知识点2
                foreach (object node in nodeList)
                {
                    foreach (System.Reflection.PropertyInfo prop in props)
                    {
                        object propValue = prop.GetValue(node, null);//知识点2
                        Console.WriteLine(string.Format("{0} = {1}", prop.Name, propValue));
                    }
                }
            }
        }
    
        //应用举例
        class Program
        {
            static void Main(string[] args)
            {
                List<Node> listTree = new List<Node>();
                listTree.Add(new Node(0, 1, "北京"));
                listTree.Add(new Node(1, -1, "中国"));
                listTree.Add(new Node(2, 6, "莫斯科"));
                listTree.Add(new Node(3, 0, "海淀区"));
                listTree.Add(new Node(4, 0, "朝阳区"));
                listTree.Add(new Node(5, 3, "上地"));
                listTree.Add(new Node(6, -1, "俄罗斯"));
                listTree.Add(new Node(8, -1, "美国"));
    
                B newB = new B();
                newB.DataSource = listTree;
                newB.RecursionList();
    
                Console.Read();
            }
        }
    }
    


    再更进一步:
    当然,this._dataSource 可能不一定可迭待的
    所以如下修改更完善

    //怎么遍历DataSource,取出ID,PID,Name属性值?
            public void RecursionList()
            {
                if (this._dataSource == null) return;
                if (this._dataSource is IEnumerable)
                {
                   IEnumerable nodeList = (IEnumerable)this._dataSource;
                   foreach (object node in nodeList)
                   {
                     PropertyInfo[] props = node.GetType().GetProperties();
                     foreach (System.Reflection.PropertyInfo prop in props)
                     {
                        object propValue = prop.GetValue(node, null);
                        Console.WriteLine(string.Format("{0} = {1}", prop.Name, propValue));
                     }
                   }
                } 
                else if (this._dataSource is IEnumerable)//其他类型呢?
                {
                  ... 此时,你想到什么呢?什么要做可使扩展性更好呢?
                }
            }
    
    

  • 相关阅读:
    聊一聊正则表达式,最全最常用总结
    11个优秀的Android开发开源项目
    一招教你打造一个滑动置顶的视觉特效
    这些优质的电子书 开发必备【干货精选】
    用Kotlin破解Android版微信小游戏跳一跳
    2017上半年技术文章集合【Android】—184篇文章分类汇总
    经常用到的字符串函数
    EhLib使用说明
    Delphi中本年、本月、本周的第一天和最后天
    Delphi中关于listview的一些使用
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1889780.html
Copyright © 2020-2023  润新知