C# - Question About Generic Classes

Simple question here, I’ve googled it and cannot find an answer.

When using a generic class, normally any objects of classes derived from the original one can also be used with it, as per the normal inheritance behaviour. My question is, is there any way to make it only accept objects of that specific class and not the derived ones?

The only way I can think of achieving this right now is to add an explicit test in every method in the generic class that accepts the objects to ensure no classes derived from the original are passed by throwing an exception when a derived class is detected.

This may sound like an odd thing to want to do, but while I want to benefits of inheritance in one part of my program, with this generic class I’m working on I want all classes to be treated separately, even if they are derived.


Not really sure I understand your question but sounds like you want to use constraints. e.g. https://msdn.microsoft.com/en-us/library/d5x73970.aspx

There is no built-in way to do this. The closest you can do is use the constraint that the generic type must be or derive from a specified base class.

I suppose you could have a Type enum, and a corresponding property in the generic class. Like

enum Type
     generic, specific

internal Type ObjectType { get; set; }

And in all of the constructors for the generic class and its inherited classes, assign the ObjectType property its corresponding value. Then just check the ObjectType property to make sure it’s “generic” in the methods you want to only apply to the generic class.

There is no way to check the base class at compile time. The only way around it is to define an abstract class or an interface and have all your classes derived from this base type. You can then have a sealed class that you use as a constrain in your generic class, while you can freely derive other classes from the base class at any level of inheritance at other parts of your code.

ok, the base class doesn’t have to be abstract, but you can only constrain the sealed class in the generics. As a bonus your can mark this class as ‘internal’.
Some more info on what you are trying to do would be helpful to give you other alternatives.

It seems that what I was after isn’t actually specific to generics, to look at it another way, I wanted to get the compiler to ignore inheritance in specific cases, i.e. treat derived classes as completely separate from their base class rather than as per normal inheritance rules.

Again, I realise this is an odd thing to consider, but I was wondering if it was possible.

Since I posted that question I’ve thought about my problem more, and I think I can achieve my end result without needing this.

Thanks for the feedback and taking the time to answer.

Here’s an idea I have, in case anyone happens to want the answer:

class BaseClass {
    virtual void Method(){}

class InheritedClass_Allow : BaseClass {
    override void Method(){}

class InheritedClass_Disallow : BaseClass {
    override void Method(){
        // new code

And make your required datatype InheritedClass_Allow

What I’m trying to do has since changed and I’m trying some other things.

Thanks for taking the time, though.