Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Generics - trying to find the name of a pattern
    primarykey
    data
    text
    <p>I've been trying to find the name of a pattern of generics usage in C#, and I don't know of a better resource than stack overflow!</p> <p>What I'm doing is creating generic base classes into which the derived class is directly passed - I looked and looked for examples of this or a name for the pattern, and I was hoping one of the great members of this community could help me out.</p> <p>I did a write up on how it works at <a href="http://www.mobiusbay.com/home/alittlesomethingimcallingcascadinggenerics" rel="nofollow">http://www.mobiusbay.com/home/alittlesomethingimcallingcascadinggenerics</a> but if you would rather not read it there, an example of it follows:</p> <pre><code>public abstract class IndexedMemberBase&lt;TDerivedClass&gt; : IDisposable where TDerivedClass : IndexedMemberBase&lt;TDerivedClass&gt; { #region Declarations &amp; Properties private static List&lt;TDerivedClass&gt; derivedInstances = new List&lt;TDerivedClass&gt;(); public static List&lt;TDerivedClass&gt; DerivedInstances { get { return derivedInstances; } } #endregion #region Constructor(s) public IndexedMemberBase() { if (derivedInstances == null) derivedInstances = new List&lt;TDerivedClass&gt;(); derivedInstances.Add((TDerivedClass)this); } #endregion #region Methods public void Dispose() { if (derivedInstances.Contains(this) == true) { derivedInstances.Remove((TDerivedClass)this); } } #endregion } </code></pre> <p>That's the base class, and the derived class can be incredibly simple - for example:</p> <pre><code>public class IndexMember : IndexedMemberBase&lt;IndexMember&gt; { //Add all the crazy business you could ever want! } </code></pre> <p>That piece of code throws a pointer to each instance of the dervied class into a static collection for general use - the collection is distinct for each derived class so you won't have conflicts from it existing statically in the base. I find it to be quite useful in my projects, and I hope someone else can find use for it.</p> <p>The important thing to point out is that the derived class passes itself in the generic signature to the base class.</p> <p>If anyone has seen this pattern in use, or knows of a name for it - I would really love to see some examples, or at least call it by it's correct name! For now, I'm calling it recursive generics, since it seems to fit, but I'm sure there's a better name!</p> <h2>Thank you very much for your help!</h2> <p>Adam</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload