C# Casting a List as List

前端 未结 11 1522
小鲜肉
小鲜肉 2020-12-09 15:35

Why can I not cast a List as List? Why does the following not work:

internal class ObjBase
   {
   }

int         


        
相关标签:
11条回答
  • 2020-12-09 15:56

    This is a major pain in C# - this is how generics were designed. List doesn't extend List, its just a completely different type. You can't cast or assign them to each other in any way, your only option is to copy one list to the other one.

    0 讨论(0)
  • 2020-12-09 16:03

    Covariance my friend. Look at http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-contravariance

    0 讨论(0)
  • 2020-12-09 16:03

    I think you are misunderstanding the cast you are trying to do. You are thinking that you are changing the type of the object that is stored in the list, where you are actually trying to change the type of the list itself. It rather makes sense that you can't change the list itself as you have already populated it.

    You might look at it as a list of a base class and then cast it when you are processing the list items, that would be my approach.

    What is the purpose of this attempted cast?

    0 讨论(0)
  • 2020-12-09 16:05

    Here is how I fixed the Conversion from a

    list<SomeOtherObject>
    

    to a

    object
    

    and then to a

    List<object>
    

    https://stackoverflow.com/a/16147909/2307326

    0 讨论(0)
  • 2020-12-09 16:06

    I can only describe the "problem" from a Java view, but from what little I know this aspect is the same in both C# and Java:

    A List<ObjBase> is not a List<Obj>, because it could contain an ObjBase object which is not a Obj object.

    The other way around a List<Obj> can not be cast to a List<ObjBase> because the former guarantees to accept an Add() call with a ObjBase argument, which the latter will not accept!

    So to summarize: even though a Obj is-a ObjBase a List<Obj> is not a List<ObjBase>.

    0 讨论(0)
  • 2020-12-09 16:06

    Lazarus:
    I thought that the compiler would realise that I wanted actions done on the objects of the list and not that I was trying to cast the list itself.

    Some more information:

    public abstract class ObjBase
       {
       }
    
    internal interface IDatabaseObject
       {
       }
    
    public class Obj : ObjBase, IDatabaseObject
       {
       }
    
    
    internal interface IDatabaseObjectManager
       {
          List<ObjBase> getSomeStuff();
       }
    
    public class ObjManager : IObjManager
    {
        public List<Obj> returnStuff()
        {
           return getSomeStuff().Cast <Customer>().ToList<Customer>();
        }
    
        private List<ObjBase> getSomeStuff()
        {
           return new List<ObjBase>();
        }
    }
    

    Now client code outside of this DLL can go: ObjManager objM = new ObjManager(); List listOB = objM.returnStuff(); I'm going to be creating several Obj and ObjManager types for this part (O/RM) of the application.

    (Darn comment block ran out of characters! :-)

    0 讨论(0)
提交回复
热议问题