Basically this limitation new()comes down to:
class Factory<T> where T : new()
{
public T Create()
{
return new T();
}
}
Now you are not allowed to pass arguments to the constructor. If you still want to initialize a new object, you can achieve this, for example. introducing another restriction:
interface ILikeBananas
{
double GreenBananaPreferenceFactor { get; set; }
}
class Factory<T> where T : ILikeBananas, new()
{
public T Create(double greenBananaPreferenceFactor)
{
ILikeBananas result = new T();
result.GreenBananaPreferenceFactor = greenBananaPreferenceFactor;
return (T)result;
}
}
, Activator.CreateInstance, , , .
Activator.CreateInstance new(); .