解决一个泛型转换的问题

这个问题本来发在社区里面,整理一下备以后参考.

public class TestClass : ITestInterface
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    
    }
    public interface ITestInterface
    {
        string Name { get; set;}
    }

为什么这样写就不行呢?

            IList<ITestInterface> list = (IList<ITestInterface>)new List<TestClass>();
            Assert.IsTrue(list != null);

我想使用实现相同接口的不同类型的集合类型, 这些类型又没有实现此接口的公共基类.


己想了一个解, 请大家看看吧.思想跟Klesh Wong的类似.

List<ClassA> listA = new List<ClassA>();
for (int i = 0; i < 10; i++)
{
ClassA a = new ClassA();
listA.Add(a);
}

List<ClassB> listB = new List<ClassB>();
for (int k = 0; k < 10; k++)
{
ClassB b = new ClassB();
listB.Add(b);
}
List<IMyInterface> listInterfaceA = listA.ConvertAll(new Converter<ClassA,IMyInterface>(ConterMyGenericClassA));
Assert.IsTrue(listInterfaceA.Count > 5);

List<IMyInterface> listInterfaceB = listB.ConvertAll(new Converter<ClassB, IMyInterface>(ConterMyGenericClassB));
Assert.IsTrue(listInterfaceB.Count > 5);
}
public static IMyInterface ConterMyGenericClassA(ClassA obj)
{
return (IMyInterface)obj;
}

public static IMyInterface ConterMyGenericClassB(ClassB obj)
{
return (IMyInterface)obj;
}

为 什么会有这种需求呢, 其实是这样. 通常我们会做一些控件,在里面公开一些数据源的属性,比如 IList<ClassA>, 而这里的数据我们有时候会去视图里面取出来, 如果采用把视图映射成对象的话我们就不能直接把这个源给到控件,当然,也并非不行,比如不使用泛型,就弄个ArrayList也行,但是俺们不是追求完美 嘛~~所以这个时候自然就想到抽象一个接口出来使这个控件的数据源不依赖于某个类型,这样我们在做数据访问的时候就省事多了,特别是使用了ORM组件的时 候.

再来个泛型加匿名代理的版本,一行就搞定了;)

List<IMyInterface> listE = listB.ConvertAll<IMyInterface>(new Converter<ClassB, IMyInterface>(delegate(ClassB b) { return (IMyInterface)b; }));
Assert.IsTrue(listE.Count > 0);


应该是这样写才比较靠谱:
IList<ITestInterface> list = new List<ITestInterface>();
在类型转换的时候,接口转换只针对实现接口的类型
而(IList<ITestInterface>)右侧的List<T>仅仅
[SerializableAttribute]
public class List<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable
而这里的T是强类型判断的,此时的T在编译时就已经被指定为TestClass,因此这里不存在一个类型转换的过程。
因此顶多只能(IList<TestClass>)new List<TestClass>();
而非(IList<ITestInterface>)new List<TestClass>();


1小时前   Klesh Wong :
理论上来,
IList<ITestInterface> list = (IList<ITestInterface>)new List<TestClass>();
这种写法应该是成立的才对,但确实又不能通过编译。
楼主的需求应该也有一定的合理性,暂时只能想出使用重新构建List的方法来做转换,不知各们有没有更好的解决方法?
IList<ITestInterface> list = Cast<TestClass, ITestInterface>(new List<TestClass>());
Response.Write(list != null);
public static IList<T> Cast<S, T>(IList<S> e) where S:T
{
IList<T> list = new List<T>();
foreach (S s in e)
list.Add(s);
return list;
}


转载于:https://www.cnblogs.com/darkangle/archive/2007/12/16/996377.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值