There are no such restrictions in .NET. Only six types of restrictions are available (see Type parameter restrictions ):
where T: struct type argument must be a value typewhere T: class The type argument must be a reference typewhere T: new() the type argument must have an open constructor without parameterswhere T: <base class name> the type argument must be or inferred from the specified base classwhere T: <interface name> type argument must be or implement the specified interfacewhere T: U the type argument provided for T must be or derived from the argument provided for U
If you want to overlay a string on your type, you can cast for the object first. But you cannot set a restriction on the type parameter to make sure that this casting can happen:
static T GetObjectFromRegistry<T>(string regPath) { string regValue =
Another option is to create an interface:
public interface IInitializable { void InitFrom(string s); }
And put it as a restriction:
static T GetObjectFromRegistry<T>(string regPath) where T: IInitializable, new() { string regValue =
Sergey Berezovskiy
source share