Cast Object to Generic List

后端 未结 8 1272
野趣味
野趣味 2020-12-03 03:37

I have 3 generict type list.

List = new List();
List
= new List
(); List = new Lis
8条回答
  •  醉梦人生
    2020-12-03 04:17

    What a sticky problem. Try this:

    List c = null;
    List
    a = null; List d = null; object o = GetObject(); c = o as List; a = o as List
    ; d = o as List;

    Between c, a, and d, there's 2 nulls and 1 non-null, or 3 nulls.


    Take 2:

    object o = GetObject();
    IEnumerable e = o as IEnumerable;
    IEnumerable c = e.OfType();
    IEnumerable
    a = e.OfType
    (); IEnumerable d = e.OfType();

提交回复
热议问题