When I call the method this way, the type that's inferred is Property, and not the more derived Well, or RealEstate, presumably because the type inference is being done at compile time. I've worked around this by casting P to dynamic, like:

var PropertyInfoPortion = getPropertyEditPortion((dynamic)P);

which works fine. I'm just wondering if there's a more elegant way to do this.

EDIT

Sorry, I always try to show the least amount of code to get the point across so things don't get too cluttered. Here's the full method:

I have an instance of Property (which is abstract) and I was using type inference to get the true type to pass to my IoC, without having to resort to reflection (to put together the right generic type). I was just wondering if there was a trick whereby this could be done without dynamic casting, but I guess not. Thanks all.

EDIT 2

I'm trying to create a IPortionOfPropertyInfoAddEditView<T>

My Property instance, P, is of the type that IPortionOfPropertyInfoAddEditView needs, but it's typed as Property, not the more derived type. I would just love it if I could say:

What's in the body of the method that makes it so important that the type-argument be the same as (presumably) the run-time type of the argument?
–
AniMar 18 '11 at 15:56

Interesting... I learned something today! I think what you did is pretty elegant, Adam... Like @Ani, I am also wondering what difference it makes...
–
Eugenio De HoyosMar 18 '11 at 15:59

@ani - The result of the method is based on the type argument - it must be the most derived type
–
Adam RackisMar 18 '11 at 16:00

@Adam: If you could provide a simple example of how the result is different, it might help us.
–
Eugenio De HoyosMar 18 '11 at 16:02

@Adam: But you haven't shown us the 'desirable' (non-dynamic) usage of the method. Without context, it's really hard to answer this question sensibly. One more question: Is the interface covariant?
–
AniMar 18 '11 at 16:08