Passing unconstrained generic type parameter to a constrained method

I have too methods:

public TValueType? DoStuffWithValueType<TValueType>(int x, int y) 
   where TValueType: struct {}

public TRefType DoStuffWithRefType<TRefType>(int x, int y) 
   where TRefType: class {} 

How can i wrap them in a new third method?

The following is not compiling since i cannot persuade the compiler that T is in fact a struct when calling DoStuffWithValueType:

public T DoStuff<T>(int x, int y) {

   if(typeof(T).IsValueType)
   {
      return DoStuffWithValueType<T>(x, y);
   }

   return DoStuffWithRefType<T>(x, y);
}

I already tried overloading DoStuff, but this attempt failed since generic-constraints are not part of the method signature.
I also tried to get rid of the constraints, but i could not.

Any ideas? Thank you!

Jon Skeet
people
quotationmark

You can't, basically - you'd have to invoke the relevant methods with reflection, which is ugly.

Of course you can do this with dynamic typing, which hides the reflection from you:

public T DoStuff<T>(int x, int y) {
   dynamic d = this;
   if(typeof(T).IsValueType)
   {
       return d.DoStuffWithValueType<T>(x, y);
   }    
   return d.DoStuffWithRefType<T>(x, y);
}

You may think that's cleaner than doing it manually with reflection - or you may not :)

There's no way that I'm aware of to make the compiler "trust" a type argument where it wouldn't normally do so.

people

See more on this question at Stackoverflow