首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

架构设计中关于类型转换(object<>List),会有什么损失。该如何处理

2013-10-27 
架构设计中关于类型转换(object---List),会有什么损失。本帖最后由 toploveall 于 2013-10-19 18:24:11

架构设计中关于类型转换(object<--->List),会有什么损失。
本帖最后由 toploveall 于 2013-10-19 18:24:11 编辑 直接上代码


     //父类
    public class Base
    {
        public virtual object GetList()
        {
            return null;
        }
    }
    
    //子类A
    public class A : Base
    {
        public override object GetList()
        {
            //返回List<A>
            List<A> aList = new List<A>();
            return aList;//会不会自动转成Object类型
        }
    }
    //子类B
    public class B : Base
    {
        public override object GetList()
        {
            List<B> bList = new List<B>();
            return bList;//会不会转成Object类型
        }
    }

    //上下文
    public class Context
    {
        Base b;
        public Context(Base b)
        {
            this.b = b;
        }
        public object GetList()
        {
            return b.GetList();
        }
    }

   //客户端
    public class Client
    {
        public void Test()
        {
            Context ct = new Context(new A());
            List<A> aList = ct.GetList() as List<A>;//会不会有性能损失,数据会不会转出错了
            List<B> bList = new Context(new B()).GetList() as List<B>;
        }
    }



我的意图大家通过代码应该很清楚。
以上的代码,把object和List之间转来转去,会不会影响性能,数据会不会转丢掉。
如果哪位大侠有更好的建议,请指点。 架构设计 性能
[解决办法]

    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();
        }
    }


[解决办法]
子类父类和继续承关系的,转换后不会有丢失,放心就是了
[解决办法]
引用:
子类父类和继续承关系的,转换后不会有丢失,放心就是了

正解
[解决办法]
性能损失肯定是有的,因为需要做类型判定。可以考虑使用泛型
        //父类
        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();
            }


        }


[解决办法]
更改引用类型不会对对象本身造成任何改变。

好比你将你的笔记本电脑“视作”电脑,或者视作一个物体,它本身不会因此而有改变一样。
[解决办法]
 那我可能需要怀疑1个问题。 如果是在remoting,我们清楚List<T> 无法通过remoting传递,因为它不可序列化,但是如果转型为IEnumerable<T> 则能够发送,因此我认为,在必要的情况下,.NET仍会对其进行必要的转换与舍弃

热点排行