This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
|
|
|
Okaaaay. My actual message disappeared! Let's try again: I have a property named 'MyEntityList' defined as Ilist<Entity>. I have a method on my repository class that returns a generic collection of type IList<MyEntity> where MyEntity is a simple Lightspeed Entity, derived from Entity (or Entity<int> to be more precise, but that doesn't seem to matter here). When I try to assign the list returned by my method to this property it always fails with an InvalidCastOperation exception (or whatever the thing is called): Cannot convert from 'System.Collections.Generic.IList<Repository.MyEntity>' to 'System.Collections.Generic.IList<Mindscape.LightSpeed.Entity>' Why, oh why can I not convert a list of objects derived from Entity to IList<Entity>??? Lucien. |
|
|
IList isn't covariant (or contravariant come to that). This is a .NET Framework restriction (scroll down), not anything specific to LightSpeed. You'll need to create an IList<Entity> and copy the MyEntities into it (or work with an IList<Entity> throughout). |
|
|
Thanks, Ivan. Not sure whether I get the co- or contravariant bit.:-) To be more precise I have generic repository method that returns a IList<T> where T : Entity. I want to create or use an equally generic method or property that takes that as a IList<Entity>. So you're saying such a thing is not possible at all? Never realised that! I have sorted it for now by wrapping my property in a method that takes a IList<T> where T : Entity. In that method I then create a new List<Entity> and add all entities passes into the method to it. That seems to work. I was hoping for a simpler approach, but no such luck... :-) Lucien. |
|