1

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.)

AnorZaken
  • 1,916
  • 1
  • 22
  • 34
  • Could you be a bit more specific as to what cast you have that isn't working? – BradleyDotNET Feb 11 '15 at 18:01
  • @BradleyDotNET T is a derived type of the abstract base, and problem: in base-class I cant write `(T)this`. I understand that the compiler doesn't like this, but `this` _is actually_ of type `T` :/ – AnorZaken Feb 11 '15 at 18:02
  • And what properties would `T` have? What derived properties would you be able to access? I'm not sure the type system isn't showing you an *actual* error here. – BradleyDotNET Feb 11 '15 at 18:04
  • Don't know if I understood your question correctly or not, it looks like that abstract class is not designed correctly. – Mouhong Lin Feb 11 '15 at 18:04
  • @BradleyDotNET Added some simplified example code – AnorZaken Feb 11 '15 at 18:17

1 Answers1

2

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;
   }
}
Steve Mitcham
  • 5,268
  • 1
  • 28
  • 56
  • Indeed it does. Good catch. Will have to experiment a bit more to find exactly what breaks it... (+1) – AnorZaken Feb 11 '15 at 18:33
  • If you figure out the bad constraint and add it to your question (if finding it doesn't make it obvious) add it to the question and I'll see what I can do. – Steve Mitcham Feb 11 '15 at 18:43
  • Ok.... now I can't reproduce it anymore o_0 ...?! I was struggeling with this for over 30min (and although it required changes to several related interfaces I was sure I had already tried to make those changes previously - guess I did something wrong). Sorry, marking this as answer. Working late + weird problems ==> probably need a coffee break. Well thanks to you I tried again and now it works. so I'm happy :) – AnorZaken Feb 11 '15 at 18:44