有没有办法创建一个使用new()构造函数约束来要求具有特定类型构造函数的类的泛型方法?
例如:
我有以下代码:
public T MyGenericMethod<T>(MyClass c) where T : class
{
if (typeof(T).GetConstructor(new Type[] { typeof(MyClass) }) == null)
{
throw new ArgumentException("Invalid class supplied");
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
是否有可能有这样的东西?
public T MyGenericMethod<T>(MyClass c) where T : new(MyClass)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)