I want to create a generic method that is only applicable to classes that have the Serializable attribute, e.g.
public static int Foo<T>(T obj) where T : Serializable {
...
}
but obviously the above doesn’t compile. And I’m guessing if I put SerializableAttribute there, it’ll insist that T is an attribute, not a class with that attribute.
How do you do something like this?
Constraints on attributes cannot be done. One solution could be to use reflection to analyse the object being passed to your method and see if it has the
Serializableattribute declared.