C# - Generics on this class type

Hello,

I am trying to instantiate a generic class, using the current class as a parameter. Normally you would do this like this (assuming a class MyClass):

private Generic<MyClass> _g = new Generic<MyClass>();

And then i wrap this instance in a read-only property called MyGeneric:

public MyGeneric<MyClass> MyGeneric {

get { return _g; }

}

However, this is not really object oriented. Since any class derived from MyClass (MyDerivedClass) would have an instance typed on MyClass, not MyDerivedClass. i.e: MyDerivedClass.MyGeneric returns a Generic<MyClass>.

So, how can i instantiate a Generic class using the current class-type as a parameter, in a truly object oriented (polymorphic) manner



Answer this question

C# - Generics on this class type

  • Bruce Thomas

    I can't say that i'm a Delphi programmer, however, unless you are able to declare something like:

    MyGeneric<self> _myGeneric;

    inside the definition of a class and that this will produce a class that holds a reference to a generic of the current type even if used in a derived class (for example, accessing _myGeneric in a derived class MyDerived will give a Mygeneric<MyDerived>) , unless you can do this, i can't see how is it possible!

    Any way, what i said was not that this is not applicable in other languages nor that it is wrong! In C#, it is not possible by means of a simple declaration. That still makes C# an OOP because that does not contradict with polymorphism. Other languages impleminting constructs enabling you to do this are just offering more extensibility (although i can argue that in many situations, this specifc feature can create looped/infinite reference problems and can produce code that even the one who wrote won't understand.)

    Still, it would have been nice if we were given the ability to do this. I like the idea of the self keyword in the static context and if it is possible to declare code as i mentioned earlier in delphi, that would be a great feature!

    Thanks for the info



  • Hexadecimator

    Something to say…

    To some extend I can follow your conversation. But you liberally speak Delphi ‘self’ and C++ ‘this’ in the same context as in C# ‘this’. Delphi and C++ uses real pointers to some vtable and in C# ‘this’ is merely a handle -known internally by memory manager. Like C++ has a ‘reference’ and C# reuses ‘reference’ namesake, their meaning is totally different. C++ reference is a pointer to some physical machine address and C# reference is reference to (object) type entity, again like a handle. Maybe THE CODE PRJOJECT Generic Singleton Pattern using Reflection, in C# - The Code Project - Design and Architecture is something your are talking about



  • Jean-Pierre Fouche

    What you're trying to do is something I wanted to since the old days of C++ templates! Unfortunatly, you can't.

    However, let's be fair, this is not bad OOP. Actually, it complies perfectly with all OOP specifications. Remember that polymorphism is for derived classes' members to behave differently according to their calling actual type when there is a member with the same signature in the base class (that is, when called from a base class reference, each object will actually behave according to its derived class specs ). Now what we want is for members with different signatures to behave the same in al derived classes. (let's suppose that you want to create a clas for which all classes that will be derived from it will have a collection of their own type, that very collection will have different signatures - MyGeneric<MyClass> is different than MyGeneric<MyDerivedClass1> and MyGeneric<MyDerivedClass2> and so on). This is not polymorphism we are asking for actually, this is some kind of a class template or meta class!

    Got the idea Hope this helps!



  • Juha Kauppila

    Please note that this is perfectly possible in another OO / .Net language: Delphi!

    In Delphi there is the keyword "self", which is equal to "this" in C++ / C#. However, in static methods in Delphi "self" refers to the classtype. For example, if you have a static method on a class, that calls

    self.Create();

    a new instance of the current classtype is created. Furthermore, when you derive a class (TMyDerived) from the base class (TMyBase) containing the call to self.Create, and you call the static method on that derived (TMyDerived) class type, an instance of the derived class will be created (!)

    This all relates to the fact that all delphi objects (read: TObject) have a static property ClassType that returns the type of the class.


  • C# - Generics on this class type