Public Constructors in Classes should be usable in Generic Methods #7004
Unanswered
LupusInferni315
asked this question in
Language Ideas
Replies: 1 comment 4 replies
-
Constructors aren't inherited. There is no guarantee that any given class that extends Using abstract static interface members you can require that the generic type provide a factory method: public interface ISomeInterface<T> where T : ISomeInterface<T> {
string Key { get; set; }
static abstract T Create(string key);
}
public class SomeChildClass : ISomeInterface<SomeChildClass> {
public string Key { get; set; }
private SomeChildClass(string key) => Key = key;
public static SomeChildClass Create(string key) => new SomeChildClass(key);
}
public T CreateNew<T>(string key) where T : ISomeInterface<T> {
return T.Create(key);
} |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If I have a type
and a child class
and a method
Beta Was this translation helpful? Give feedback.
All reactions