The new()
constraint will allow you to create a new T
. For instance
public void Method1<T>(BaseReportContent content) where T : BaseReportContent, new()
{
var myT = new T();
}
Note : There are caveats
new constraint (C# Reference)
The new constraint specifies that any type argument in a generic class
declaration must have a public parameterless constructor. To use the
new constraint, the type cannot be abstract.
Additional Resources
You can find more information on generic constraints here
Constraints on type parameters (C# Programming Guide)
Constraints inform the compiler about the capabilities a type argument
must have. Without any constraints, the type argument could be any
type. The compiler can only assume the members of System.Object, which
is the ultimate base class for any .NET type. For more information,
see Why use constraints. If client code tries to instantiate your
class by using a type that is not allowed by a constraint, the result
is a compile-time error. Constraints are specified by using the where
contextual keyword. The following table lists the seven types of
constraints:
Lastly, if you need to create something that need constructor parameters, you can use
Activator.CreateInstance Method
Creates an instance of the specified type using the constructor that
best matches the specified parameters.