Reputation: 51063
If I have classes:
class Useless
{
private string _message ;
public Useless(string message)
{
_message = message;
}
}
class UselessFactory<T> where T : new()
{
public Useless CreateUseless(string msg)
{
return new T(msg);
}
}
Why can't I instantiate T with parameters, like I could do with the following?
return (Useless)Activator.CreateInstance(typeof(T), msg);
Upvotes: 1
Views: 90
Reputation: 12944
It is simply not supported at this moment.
There are work arounds:
Func<string, T>
). You can call this delegate which will construct an instance with the given parameter.Upvotes: 1
Reputation: 17580
The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.
From the docs
Possible workaround (not the same thing)
If you make Message
a public and mutable property you can do:
class UselessFactory<T> where T : UselessBase, new()
{
public T CreateUseless(string msg)
{
return new T() { Message = msg };
}
}
Upvotes: 2
Reputation:
You could do the following:
return (T)Activator.CreateInstance(typeof(T), new object[] { msg });
I got it from this question: https://stackoverflow.com/a/731637/1522991
Upvotes: 1