Is there anyway to force a downcast in the abstract base-class when the derived type is actually known there (due to complicated generics)?
Right now my ugly workaround is to implement an abstract protected property This that simply return this... so the usual stuff about downcasting not being possible due to "missing extra parts" dont apply, all parts are there, I just have to force the type-system to cut me some slack :/
protected abstract T This { get; }
I know the derived type, I just cant find any way to force the cast to happen! Is there any way at all?
(This is part of a framework so forcing the consumer to implement silly things like a This-property really sucks. I would rather make the inner workings more complex, and force the consumer to write as little code as possible.)
Edit: Since it's hard to see what good this would do, I will try to add some more code. It will still look weird perhaps, I will try to add more again if needed.
Basically in this specific problem part of the code it involves two methods in this fashion (actual implementation details omitted, just an illustration)
abstract class Base<DerivedType, OtherType>
where .... //Complicated constraints omitted
{
protected abstract OtherType Factory(DerivedType d);
public bool TryCreate(out OtherType o)
{
//Omitted some dependency on fields that reside in the base and other stuff
//if success...
o = Factory((DerivedType)this); //<-- what I can not do
return true;
}
}
(As for the bigger picture it is part of a strongly typed alternative to some of the things you can do with xpath, working on top of Linq2Xml.)
The following class definition compiles. I don't know whether your omitted constraints would conflict with this.
public abstract class Base<DerivedType, OtherType>
where DerivedType : Base<DerivedType, OtherType>
{
protected abstract OtherType Factory(DerivedType d);
public bool TryCreate(out OtherType o)
{
o = Factory ((DerivedType)this);
return true;
}
}
public class MyClass : Base<MyClass, string>
{
protected override string Factory (MyClass d)
{
return d.GetType ().Name;
}
}