Do I have to downcast when subclassing?

Posts   
 
    
JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 01-Sep-2006 01:44:19   

Do I have to downcast when subclassing and referring to a property for a EntityCollection?

Looking at the generated code in my subclass file


        /// <summary>
        /// Gets the EntityCollection with the related entities of type 'MyTraceSetEntity' which are related to this entity via a relation of type '1:n'.
        /// If the EntityCollection hasn't been fetched yet, the collection returned will be empty.
        /// </summary>
        [TypeContainedAttribute(typeof(MyTraceSetEntity))]
        public override EntityCollection<TraceSetEntity> TraceSets
        {
            get
            {
                EntityCollection<TraceSetEntity> toReturn = base.TraceSets;
                toReturn.EntityFactoryToUse = new MyTraceSetEntityFactory();
                return toReturn;
            }
        }


I can see that the collection will now use the entity factory for the subtype, but it's overriding EntityCollection<TraceSetEntity> from the parent, so anytime I get this collection, will it be typed for the base entity? I think the answer is yes, and I know I could find out in a few minutes once my code compiles, but I'm not there yet, so I thought I'd ask this on the forum (apologies).

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 01-Sep-2006 10:59:00   

The problem is called co-variance, and C#/VB.NET don't support it.

So a List<string> can't be cast to List<object> even though string derives from object. This thus means that an EntityCollection<Foo> can't be cast to EntityCollection<MyFoo> or vice versa.

Frans Bouma | Lead developer LLBLGen Pro