# C# template class

## Recommended Posts

vivendi    123
I want to add a template class to my project, but i'm not really sure how to do this. I'm using XNA in which you can load a model in two different ways: Model model = Content.Load<Model>("modelname"); or SkinnedModel = Content.Load<SkinnedModel>("modelname"); So i can use Model or SkinnedModel. Now i thought templates would come in handy here. But i'm not sure if the following is possible. How would something like this work: class ModelType<T> { T t; public void LoadModel <T> ( string modelname ) { Content.Load<t>(modelname); } public T getModel ( ) { return t; //return the Model or SkinnedModel. Depends what was created } } So i can use it in a way like this (not sure how it works)
//Somewhere in my main function
ModelType mymodel = new ModelType<SkinnedModel>("model");
somefunc ( mymodel );

//function
public void somefunc ( ModelType model )
{
//do stuff with model
//So this way it shouldn't matter if the ModelType is 'Model' or 'SkinnedModel'
}

In this case i passed SkinnedModel as ModelType to 'somefunc()' but it should also work if i passed 'Model' as ModelType to that function. That's what templates are for right?? If so, could someone please help me out with this?? Thanks for any help.