本帖最后由 toploveall 于 2013-10-19 18:24:11 编辑

解决方案 »

  1.   

    Base中的GetList方法类型能不能动态的反映出子类的类型,而不是用object。
    之前我用List<Base>,在子类重写时返回的是List<A>或List<B>
    结果编译不过,所以只能用object了。
      

  2.   


        public abstract class Base
        {
            public abstract IEnumerable<Base> GetList();
        }
        public class DerivedA : Base
        {
            public override IEnumerable<Base> GetList()
            {
                return new List<DerivedA>();
            }
        }
        public class DerivedB : Base
        {
            public override IEnumerable<Base> GetList()
            {
                return new List<DerivedB>();
            }
        }
        public class Context
        {
            Base b;
            public Context(Base b)
            {
                this.b = b;
            }
            public object GetList()
            {
                return b.GetList();
            }
        }
        public class Client
        {
            public void Test()
            {
                var aList = new Context(new DerivedA()).GetList();
                var bList = new Context(new DerivedB()).GetList();
            }
        }
      

  3.   

    性能损失肯定是有的,因为需要做类型判定。可以考虑使用泛型        //父类
            public class Base<T>
            {
                public virtual List<T> GetList()
                {
                    return null;
                }
            }        //子类A
            public class A : Base<A>
            {
                public override List<A> GetList()
                {
                    //返回List<A>
                    List<A> aList = new List<A>();
                    return aList;//会不会自动转成Object类型
                }
            }
            //子类B
            public class B : Base<B>
            {
                public override List<B> GetList()
                {
                    List<B> bList = new List<B>();
                    return bList;//会不会转成Object类型
                }
            }        //上下文
            public class Context<T>
            {
                Base<T> b;
                public Context(Base<T> b)
                {
                    this.b = b;
                }
                public List<T> GetList()
                {
                    return b.GetList();
                }
            }        //客户端
            public class Client
            {
                public void Test()
                {
                    Context<A> ct = new Context<A>(new A());
                    List<A> aList = ct.GetList();
                    List<B> bList = new Context<B>(new B()).GetList();
                }
            }
      

  4.   


     那我可能需要怀疑1个问题。 如果是在remoting,我们清楚List<T> 无法通过remoting传递,因为它不可序列化,但是如果转型为IEnumerable<T> 则能够发送,因此我认为,在必要的情况下,.NET仍会对其进行必要的转换与舍弃
      

  5.   


    求回复#7我给你的不是 IEnumberable<T> 的版本吗?这不就不需要考虑 Remoting 的问题了吗?