1

Looking for help to put such structure together.

Have base generic class:

public class A<T>
    where T: class
{
    public T info { get; set; }    
}

Works good for one step inheritance like:

class B : A<BInfoClass>{}

But need same for higher hierarchy members like, but without making all classes generics:

class C : B<CInfoClass>{}

Need possibility to have specific "info" type for each B, C, D etc. ("info" classes derive from one base):

Sloppy
  • 13
  • 4
  • according to [the documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters) you can add `where T : class, BaseClassName` – oleksa Jan 14 '20 at 09:25
  • 1
    Does this answer your question? [Can C# generics have a specific base type?](https://stackoverflow.com/questions/230736/can-c-sharp-generics-have-a-specific-base-type) – oleksa Jan 14 '20 at 09:26
  • edited the question. sorry, maybe haven't understood the answers properly, but still looking for solution – Sloppy Jan 14 '20 at 11:43
  • what is B class you are using in the question ? is it derived from `A` ? you can make it generic like `class B : A where T : class` – oleksa Jan 14 '20 at 11:51
  • yeah, class B : A{}. i'm working in unity and you can't add generic classes as components there properly – Sloppy Jan 14 '20 at 11:59
  • ok this case looks different now, and the duplicate does not fit anymore – Mong Zhu Jan 14 '20 at 11:59
  • I would just drop the generics in scenario like this. Too much hassle for little gain. – Euphoric Jan 14 '20 at 12:02
  • C# doesn't support multiple inheritance. You'd effectively end up with `C` inheriting two `info` properties, and that's just not allowed -- what should the type of `info` be in that case? (I realize you want it to be `CInfoClass`, but there are other reasonable choices.) You need a redesign of your class structure -- exactly what is hard to say with placeholder names. Of course you can always just start using `object` or `dynamic` for the property and force derived classes to cast, but that's clumsy. – Jeroen Mostert Jan 14 '20 at 12:03
  • i'm not talking about multiple inheritance, i mean few levels of complexity like A - B - C. i have "executor" classes heirarchy and "info" classes hierarchy. each executor class should have corresponding to it info property – Sloppy Jan 14 '20 at 12:11
  • 1
    could you please show your hierarchy levels completely (at least - class definitions with inheritance) ? Generic is about generic code, not about how to get detailed class definitions. Can you use some interface instead ? – oleksa Jan 14 '20 at 12:30
  • 1
    First hierarchy: `public class A {}` `public class B : A{}` `public class C : B{}`. Second hierarchy: `public class InfoA:{}` `public class InfoB: InfoA{}` `public class InfoC: InfoB{}`. What i need is to have InfoC in C, InfoB in B and InfoA in A. – Sloppy Jan 14 '20 at 13:06

1 Answers1

1

I'd like to suggest to add one more level of hierarchy just to make generic class to be pure generic.

You can put all common code to the AContainer also.

public class AContainer<T> where T : class, InfoA {
  public T info { get; set; } 
} 
public class A : AContainer<InfoA> {}
public class BContainer<T>: AContainer<T> {}
public class B : BContainer<InfoB> {} 
public class CContainer<T>: BContainer<T> {}
public class C : CContainer<InfoC> {}

A, B and C classes are empty classes for the without making all classes generics condition (to be used in Unity) AContainer BContainer and CContainer are added to make generic possible here.

However it does not look like a place to use generics

oleksa
  • 3,688
  • 1
  • 29
  • 54
  • yeah, big thanks for answers. what do you think is a better way? unfortunately can't put contents of info classes into "execution" classes – Sloppy Jan 14 '20 at 15:07
  • @Sloppy do you really need this inheritance between A, B and C classes ? Can you implement all those classes without inheritance, just class A { AInfo info; } class B { BInfo info; } class C { CInfo info } ? – oleksa Jan 14 '20 at 15:48
  • i can implement info stuff without inheritance, all in one class (possibly will do so for now), but was looking for better solution – Sloppy Jan 14 '20 at 17:16
  • @Sloppy you have to get some benefit from the inheritance and generics if class hierarchy becomes more complex. Some common algorithms, methods etc. – oleksa Jan 15 '20 at 07:56