Cast Object to Generic List

后端 未结 8 1281
野趣味
野趣味 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:09

    A general solution like this (to instantiate a type with a generic parameter based on a System.Type object) is not possible. If you're really just dealing with these three types, though, then you're in luck because it's pretty easy:

    Type t = typeof(obj);
    
    if (t == typeof(List)) {
        var contactList = (List)obj;
        // do stuff with contactList
    
    } else if (t == typeof(List
    )) { var addressList = (List
    )obj; // do stuff with addressList } else if (t == typeof(List)) { var documentList = (List)obj; // do stuff with documentList }

提交回复
热议问题