0

I have a situation like this:

abstract class DTAction<T_Owner> {
 public virtual void Process(T_Owner owner) { }
 // Other stuff...
}

abstract class SingletonAction<T_Owner, T_ActionImpl> : DTAction<T_Owner>
 where T_ActionImpl : SingletonAction<T_Owner, T_ActionImpl>, new() 
 where T_Owner : MyOwnerBase<T_Owner> {
      // just setting up the singleton here.
 }

 abstract class MyOwnerBase<T_Owner> : SomeOtherBase 
 where T_Owner : MyOwnerBase<T_Owner> {

 }

so, that's the setup. I use these like this:

class MyOwnerImpl : MyOwnerBase<MyOwnerImpl> { 
// specific work

// nested single-instance classes that do the processing on the "owner", which is this actual instance.
  class ActionA : SingletonAction<MyOwnerImpl, ActionA> {
    public override void Process(MyOwnerImpl owner) {
      // Processing A
    }
  }

  class ActionB : SingletonAction<MyOwnerImpl, ActionB> {
    public override void Process(MyOwnerImpl owner) {
      // Processing B
    }
  }
}

and this works, but I have an issue with the specification of inheritance. It allows me to specify improper types, such as:

class ActionB : SingletonAction<MyOwnerImpl, ActionA> {
     public override void Process(MyOwnerImpl owner) {
       // Processing B
     }
}

which would return the singleton instance of ActionA when calling ActionB.Instance. The compiler treats this as legal, and I haven't found a way to specify that I want only ActionB in the snippet above. Any ways to resolve this?

Striezel
  • 3,693
  • 7
  • 23
  • 37
Gru
  • 400
  • 2
  • 8
  • No, not possible. This is known as the "curiously recurring template pattern", and is the subject of several questions on SO. I've linked to the similar question, the [accepted answer](http://stackoverflow.com/a/3789193/69809) is by Eric Lippert, once a member of the C# language design team. – vgru Sep 10 '16 at 13:03
  • Thanks for the info, this answers my question. – Gru Sep 10 '16 at 13:43

0 Answers0