I would like to create a method in a base generic class to return a specialized collection of derived objects and perform some operations on them, like in the following example:
using System;
using System.Collections.Generic;
namespace test {
class Base<T> {
public static List<T> DoSomething() {
List<T> objects = new List<T>();
// fill the list somehow...
foreach (T t in objects) {
if (t.DoSomeTest()) { // error !!!
// ...
}
}
return objects;
}
public virtual bool DoSomeTest() {
return true;
}
}
class Derived : Base<Derived> {
public override bool DoSomeTest() {
// return a random bool value
return (0 == new Random().Next() % 2);
}
}
class Program {
static void Main(string[] args) {
List<Derived> list = Derived.DoSomething();
}
}
}
My problem is that to do such a thing I would need to specify a constraint like
class Base<T> where T : Base {
}
Is it possible to specify a constraint like that somehow?
This might work for you:
class Base<T> where T : Base<T>
You can't constrain T
to an open generic type. If you need to constrain T
to Base<whatever>
, you'll need to construct something like:
abstract class Base { }
class Base<T> : Base where T : Base { ... }
I have used the following to create not a linked list, but a generecic linked tree. It works wonderfully nicely.
public class Tree<T> where T : Tree<T>
{
T parent;
List<T> children;
public Tree(T parent)
{
this.parent = parent;
this.children = new List<T>();
if( parent!=null ) { parent.children.Add(this as T); }
}
public bool IsRoot { get { return parent == null; } }
public bool IsLeaf { get { return children.Count == 0; } }
}
Example usage from Mechanics (Coordinate System Hierarchy)
class Coord3 : Tree<Coord3>
{
Vector3 position;
Matrix3 rotation;
private Coord3() : this(Vector3.Zero, Matrix3.Identity) { }
private Coord3(Vector3 position, Matrix3 rotation) : base(null)
{
this.position = position;
this.rotation = rotation;
}
public Coord3(Coord3 parent, Vector3 position, Matrix3 rotation)
: base(parent)
{
this.position = position;
this.rotation = rotation;
}
public static readonly Coord3 World = new Coord3();
public Coord3 ToGlobalCoordinate()
{
if( IsRoot )
{
return this;
} else {
Coord3 base_cs = parent.ToGlobalCoordinate();
Vector3 global_pos =
base_cs.position + base_cs.rotation * this.position;
Matrix3 global_rot = base_cs.rotation * this.rotation;
return new Coord3(global_pos, global_ori );
}
}
}
The trick is to initialize the root object with null
parent. Remember you cannot do Coord3() : base(this) { }
.