Is there a way to create a Generic Method that uses the new()
constructor constraint to require classes with constructors of specific types?
For Example:
I have the following code:
public T MyGenericMethod<T>(MyClass c) where T : class
{
if (typeof(T).GetConstructor(new Type[] { typeof(MyClass) }) == null)
{
throw new ArgumentException("Invalid class supplied");
}
// ...
}
Is it possible to have something like this instead?
public T MyGenericMethod<T>(MyClass c) where T : new(MyClass)
{
// ...
}
EDIT: There's a suggestion regarding this. Please vote so we can have this feature in C#!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…