I've got two classes:
public abstract class Uniform<T>
public class UniformMatrix4 : Uniform<Matrix4>
(So far....there will be more that implement different types)
And now lets say I want to write a function that will accept any uniform object... but I can't do that because there is no class called Uniform
, only the generic Uniform<T>
. So what's the best approach to solving this problem?
Uniform<T>
implement IUniform
Uniform<T>
extend Uniform
Uniform<T>
directly?Make your methods generic as well, and you're good.
Note that you always have the choice of using all generic type arguments on the function if needed, like this:
public void MyMethod<TUniform, T>(TUniform uniform) where TUniform: Uniform<T> {...}
The compiler will usually infer the type arguments on his own whenever you have a parameter, so that the call in fact looks like a normal method invocation in the C# source code:
UniformMatrix4 uniform;
MyMethod(uniform); // the types of the generic functions are inferred
Admittedly intimidated to post this answer, but think it may be correct:
public static Uniform<dynamic> MyMethod(dynamic myObject) {
//do stuff
return uniform;
}
Here is a Dino Esposito blog on the topic:
http://msdn.microsoft.com/en-us/magazine/ff796227.aspx
Cheers,
Matt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With