C# Constrained Generic's and derived static methods.

Started by
3 comments, last by slicer4ever 11 years ago
I'm going over a design problem I have. basically I have a generic mesh class, that i feed some derived type that is of the base type Vertex. so, my problem is that I want the derived class to be able to pass information to the mesh class that is specific for it's implementation(it's pretty much the gpu formatting for that type).

something like this(note this is a sample to demonstrate the probelm, so try not to get caught up on the pointless-ness of the code):

public class abstract Foo{
  public abstract int GetX();
};

public class Foo_A : Foo{
  public override int GetX(){
     return 1;
  }
};

class class OtherClass<T> where T : Foo{

   public void GetTX(){
     return T.GetX();
   }
}
basically, the result of GetX doesn't need anything inside of the class, it just provides additional information for my OtherClass to access. One way I know i can make this work is to create an instance of T, in my particular case I have an array of T already, so i just call the method by accessing the first member of the array, but this feels incredibly dirty.

basically i need some type of static abstract class or const, since i'm going to be accessing OtherClass like so:
OtherClass<Foo_A> = new OtherClass<Foo_A>();
I feel their should be a way for me to access a static method in Foo_A through the OtherClass.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
Advertisement
The easiest way I know of to do this is how you're already doing it - access the static member via an instance. Every other possibility is a MUCH worse hack (static cross-construction, reflection, static table maintenance hell, etc).

I'm just hoping C# eventually supports static methods constraints - F# has something like this I believe:

// Member constraint with static member
type Class4<'T when 'T : (static member staticMethod1 : unit -> 'T) > =
    class end
Have you ever considered using a custom attribute? If the value is static you could get the custom attribute on OtherClass<T> static constructor and hold it in a static field for later use, this way you only have to do it once for each type of OtherClass<T>

If your example is representative, I'd think the problem can solved most easily like this:


class OtherClass
{
  public final Func<int> GetX;

  public OtherClass(Func<int> getX) { GetX = getX; }
}

Depending on your exact needs, there might be other solutions available to you. You would need functionality akin to C++'s template specialization; what comes closest in C# is extension methods which you can specialize dependent on type, but there are quite some limitations which I would think make it unusable in your case.

As a general solution, I would keep a Dictionary<Type, Func<int>> which I would create at initialization time using reflection (i.e. it's important I don't have to keep it up-to-date myself). OtherClass would then fetch it GetX from there once in its constructor.

Alright, so after consideration of what to do, I decided to use delegate's, and in the constructor for the OtherClass i do a typeof check and set the appropriate delegates, basically it looks like this:

public class abstract Foo{
};

public class Foo_A : Foo{
  public static int GetX(){
     return 1;
  }
};

public class Foo_B : Foo{
  public static int GetX(){
    return 2;
  }
};

class class OtherClass<T> where T : Foo{
   protected delegate int GetXFunc();
   protected GetXFunc m_GetXFunc();
   public OtherClass(){
     Type TType = typeof(T);
     if(TType==typeof(Foo_A)) m_GetXFunc = Foo_A.GetX();
     else if(TType==typeof(Foo_B)) m_GetXFunc = Foo_B.GetX();
     m_GetXFunc();
   }
}
it's a bit involved, but doesn't feel as dirty as what i was doing before. Also my implementation isn't creating OtherClass's at run-time, only during initialization, I shouldn't have any serious type of performance penalty. And for for my needs, the list of possible types is pretty small, so it's easily maintainable for now.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

This topic is closed to new replies.

Advertisement