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]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.