代码之家  ›  专栏  ›  技术社区  ›  Amit Sharma

将对象转换为泛型列表

  •  6
  • Amit Sharma  · 技术社区  · 16 年前

    a type T or List<T> 
    

    namespace Generic_Collection_Code
    {
        class Program
        {
            public static string DumpObj(object obj)
            {
                string sTemp = String.Empty;
    
                List<int> ints = obj as List<int>;
                if (ints != null)
                {
                    foreach (int i in ints)
                        sTemp += i.ToString() + ",";
                    sTemp.Trim(',');
                }
                else 
                {
                    List<string> strings = obj as List<string>;
                    if (strings != null)
                    {
                        foreach (string s in strings)
                            sTemp += s + ",";
                        sTemp.Trim(',');
                    }
                    else
                    {
                        sTemp += obj.ToString();
                    }
                }
                return sTemp;
            }
            static void Main(string[] args)
            {
                List<int> listInts = new List<int>();
                listInts.Add(1);
                listInts.Add(2);
                listInts.Add(3);
    
                Console.WriteLine("Object1: {0}", DumpObj(listInts));
                int i = 90;
    
                Console.WriteLine("Object2 {0}", DumpObj(i));
    
    
                List<string> listStrings = new List<string>();
                listStrings.Add("1");
                listStrings.Add("2");
                listStrings.Add("3");
    
                Console.WriteLine("Object3: {0}", DumpObj(listStrings));
                Console.ReadKey();
            }
        }
    }
    

        public static string DumpObj<T>(object obj)
        {
            string sTemp = String.Empty;
    
            List<T> list = obj as List<T>;
            if (list != null)
            {
                foreach (T i in list)
                    sTemp += i.ToString() + ",";
                sTemp.Trim(',');
            }
            return sTemp;
        }
    

    5 回复  |  直到 13 年前
        1
  •  11
  •   Charlie Flowers    16 年前

    List<T> genericList = object as List<T>;
    
    if(genericList != null)
    {
       // Do the loop
    }
    

    “as”关键字验证“object”实际上是一个“List<T>如果是这样,您将得到一个列表<T>如果没有,则返回null。

        2
  •  9
  •   Dave    16 年前

    您遇到的编译错误是什么?如果T在上下文中被声明为泛型类型参数,那么我在该语句中看到的唯一编译时问题就是关键字的使用 object

    IEnumerable enumerable = obj as IEnumerable;
    
    if (enumerable != null)
    {
        foreach (object item in enumerable)
        {
            sTemp += item.ToString();
        }
    }
    

    StringBuilder

        3
  •  4
  •   Brijesh Mishra    16 年前

    你不能这么做

    List<T> genericList = (List<T>)object
    

    List<T> genericList = (List<T>)obj
    

    其中obj是对象

        4
  •  0
  •   MarcE    16 年前

    把“as”和“is”结合起来怎么样?

    if (object is List<T>)
    { 
      List<T> genericlist = object as List<T>;
     // loop list
    }
    else if (object is T)
    {
     // do something else
    }
    
        5
  •  0
  •   Khanh Hua    13 年前

    private static void DataSourcePropertyChanged(DependencyObject sender,     
            DependencyPropertyChangedEventArgs args) {
            BarChart _ = sender as BarChart;
    
            if (args.Property.Equals(BarChart.DataSourceProperty)) {
                System.Collections.IList data = (System.Collections.IList)args.NewValue;
                if (data == null) return;
    
                foreach (object __ in data) {
                    IChartDataItem item = __ as IChartDataItem;
                    BarChartItem bar = new BarChartItem() {
                        Label = item.Label,
                        Value = item.Value
                    };
                    _._visualCollection.Add(bar);
    
                    if (_.MaxData < item.Value)
                        _.MaxData = item.Value;
                }
    
                if (_.Orientation == Orientation.Horizontal)
                    _.Ratio = _.Width / _.MaxData;
            }
        }
    
    推荐文章