I want to make a generic class that accepts only serializable classes, can it be done with the where constraint?
The concept I'm looking for is this:
public class MyClass<T> where T : //[is serializable/has the serializable attribute]
Nope, I'm afraid not. The only things you can do with constraints are:
where T : class
- T must be a reference typewhere T : struct
- T must be a non-nullable value typewhere T : SomeClass
- T must be SomeClass or derive from itwhere T : ISomeInterface
- T must be ISomeInterface or implement itwhere T : new()
- T must have a public parameterless constructorVarious combinations are feasible, but not all. Nothing about attributes.