在List中的初始化就是这样,泛型必须一致,如果真想那样可以重写List类 public void tets() { List<NewspaperArticleInfo> x = new List<NewspaperArticleInfo>(); MyList u = new MyList(x); } public class ArticleInfo { } public class NewspaperArticleInfo : ArticleInfo { } class MyList : List<ArticleInfo > { public MyList(List<NewspaperArticleInfo> inp) { //进行转换 }
}
1. /// <summary> 2. /// 数据转换类 3. /// </summary> 4. public static class DataTransformHelper 5. { 6. #region 转换IList<T>为List<T> 7. /// <summary> 8. /// 转换IList<T>为List<T> 9. /// </summary> 10. /// <typeparam name="T">指定的集合中泛型的类型</typeparam> 11. /// <param name="gbList">需要转换的IList</param> 12. /// <returns></returns> 13. public static List<T> ConvertIListToList<T>(IList gbList) where T : class 14. { 15. List<T> list = new List<T>(); 16. if (gbList != null && gbList.Count > 0) 17. { 18. for (int i = 0; i < gbList.Count; i++) 19. { 20. T temp = gbList[i] as T; 21. if (temp != null) 22. list.Add(temp); 23. } 24. } 25. return list; 26. } 27. #endregion 转换IList<T>为List<T> 28. }
可以的。他的问题是协变性的问题了。//数组支持协变性 ArticleInfo[] s1 = new NewspaperArticleInfo[2]; //泛型在C#4.0后才支持协变性 IList<ArticleInfo> s2 = new List<NewspaperArticleInfo>();
泛型类不能直接强转的 IList<ArticleInfo> s2 = new List<ArticleInfo>();然后你s2还是能添加NewspaperArticleInfo类型的对象的
也可以在重写的类中增加add方法 class MyList : List<ArticleInfo > { public MyList(List<NewspaperArticleInfo> inp) { //进行转换 } public void Add(NewspaperArticleInfo item) { }
}
list 内部不能间接隐式转换
楼上好多都提到了, IList<ArticleInfo> s2 = new List<NewspaperArticleInfo>();你这个转换有问题。
还有 ArticleInfo[] s1 = new NewspaperArticleInfo[2];这样写的逻辑意义是什么??这和 ArticleInfo s1 =new NewspaperArticleInfo(); 区别很大这2种写法没区别 ArticleInfo[] s1 = new NewspaperArticleInfo[2]; ArticleInfo[] s1 = new ArticleInfo[2];
IList<ArticleInfo> s2 = new List<NewspaperArticleInfo>().ConvertAll<ArticleInfo>(new Converter<NewspaperArticleInfo, ArticleInfo>(delegate(NewspaperArticleInfo info) { return info as ArticleInfo;})); 这样吧。需要把集合的元素ConvertAll,全部转换成相应的类型
{
List<NewspaperArticleInfo> x = new List<NewspaperArticleInfo>();
MyList u = new MyList(x); }
public class ArticleInfo
{
}
public class NewspaperArticleInfo : ArticleInfo
{ }
class MyList : List<ArticleInfo >
{
public MyList(List<NewspaperArticleInfo> inp)
{
//进行转换
}
}
2. /// 数据转换类
3. /// </summary>
4. public static class DataTransformHelper
5. {
6. #region 转换IList<T>为List<T>
7. /// <summary>
8. /// 转换IList<T>为List<T>
9. /// </summary>
10. /// <typeparam name="T">指定的集合中泛型的类型</typeparam>
11. /// <param name="gbList">需要转换的IList</param>
12. /// <returns></returns>
13. public static List<T> ConvertIListToList<T>(IList gbList) where T : class
14. {
15. List<T> list = new List<T>();
16. if (gbList != null && gbList.Count > 0)
17. {
18. for (int i = 0; i < gbList.Count; i++)
19. {
20. T temp = gbList[i] as T;
21. if (temp != null)
22. list.Add(temp);
23. }
24. }
25. return list;
26. }
27. #endregion 转换IList<T>为List<T>
28. }
ArticleInfo[] s1 = new NewspaperArticleInfo[2];
//泛型在C#4.0后才支持协变性
IList<ArticleInfo> s2 = new List<NewspaperArticleInfo>();
IList<ArticleInfo> s2 = new List<ArticleInfo>();然后你s2还是能添加NewspaperArticleInfo类型的对象的
class MyList : List<ArticleInfo >
{
public MyList(List<NewspaperArticleInfo> inp)
{
//进行转换
}
public void Add(NewspaperArticleInfo item)
{ }
}
ArticleInfo[] s1 = new NewspaperArticleInfo[2];这样写的逻辑意义是什么??这和 ArticleInfo s1 =new NewspaperArticleInfo();
区别很大这2种写法没区别
ArticleInfo[] s1 = new NewspaperArticleInfo[2];
ArticleInfo[] s1 = new ArticleInfo[2];
如果是IEnumerable<T>则可以
IList是接口,泛型List<T>实现了该接口
objec s2 = new List<NewspaperArticleInfo>();
IList<ArticleInfo> s1=s2 as IList<ArticleInfo>;
虽然ArticleInfo和NewspaperArticleInfo 是父子关系,但是不代表List<NewspaperArticleInfo>()和List<ArticleInfo> 也是父子关系,不能直接隐身转换。要循环转换,或者用其他的办法一个一个的转换。
+1
IList<ArticleInfo> s2 = new List<NewspaperArticleInfo>().ConvertAll( nai => nai as ai);
这样吧。需要把集合的元素ConvertAll,全部转换成相应的类型