ilist 与list 之间的相互转换

1>  将list<T>转化为ilist<T>
 

#region 转化list为ilist集合
    private IList<T> ConvertToIList(System.Collections.Generic.List<T> listObjects)
    {
        IList<T> convertedList = new List<T>();

        foreach (T listObject in listObjects)
        {
            convertedList.Add(listObject);
        }

        return convertedList;
    }

    #endregion


 

#region 转化ilist为list集合
        private IList<adminListInfo> ConvertToIList(System.Collections.Generic.List<adminListInfo> listObjects)
        {
            IList<adminListInfo> convertedList=new List<adminListInfo>();

            foreach (adminListInfo listObject in listObjects)
            {
                convertedList.Add(listObject);
            }

            return convertedList;
        }

        #endregion


2> 将ilist<T> 转化为list<T>

 

private List<T> Convert(System.Collections.IList listObjects)
{
    List<T> convertedList = new List<T>();

    foreach (object listObject in listObjects)
    {
        convertedList.Add((T)listObject);
    }

    return convertedList;
}


/**//// <summary>

    /// 转换IList<T>为List<T>

    /// </summary>

    /// <typeparam name="T">指定的集合中泛型的类型</typeparam>

    /// <param name="gbList">需要转换的IList</param>

    /// <returns></returns>

    public static List<T> ConvertIListToList<T>(IList gbList) where T : class
    {
        if (gbList != null && gbList.Count > 1)
        {
            List<T> list = new List<T>();
            for (int i = 0; i < gbList.Count; i++)
            {
                T temp = gbList[i] as T;
                if (temp != null)
                    list.Add(temp);
            }
            return list;
        }
        return null;
    }

 

来自:http://blogold.chinaunix.net/u2/69195/showart_2281892.html

你可能感兴趣的:(list)