Generics in C# - how can I create an instance of a variable type with an argument?

Viewed 38400

I've got a generics class, where I want to instantiate an object with the generic type. I want to use an argument for the constructor of the type.

My code:

public class GenericClass<T> where T : Some_Base_Class, new()
{
    public static T SomeFunction(string s)
    {
        if (String.IsNullOrEmpty(s))
            return new T(some_param);
    }
}

I get an error on the

new T(some_param)

'T': cannot provide arguments when creating an instance of a variable type

Any ideas how can I do this?

1 Answers
Related