code_junkie

Can you use “where” to require an attribute in c#?

Nov 11th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. public class MyClass<T> where T : //[is serializable/has the serializable attribute]
  2.  
  3. public void Initialize<T>(T obj)
  4. {
  5. object[] attributes = obj.GetType().GetCustomAttributes(typeof(SerializableAttribute));
  6. if(attributes == null || attributes.Length == 0)
  7. throw new InvalidOperationException("The provided object is not serializable");
  8. }
Add Comment
Please, Sign In to add comment