I am looking for an easy way to check if an object in C # is serializable.
As we know, you create an object serializable by implementing the ISerializable interface or by placing [Serializable] at the top of the class.
What I'm looking for is a quick way to test this without thinking about the class to get its attributes. The interface would be fast using the is operator.
Using @Flard's suggestion, this is the code I came up with, screaming is the best way.
private static bool IsSerializable(T obj) { return ((obj is ISerializable) || (Attribute.IsDefined(typeof (T), typeof (SerializableAttribute)))); }
Or even better, just get the type of the object, and then use the IsSerializable property for the type:
typeof(T).IsSerializable
Remember that this only looks like the class we are facing if the class contains other classes that you probably want to test, or try and serialize, and wait for errors, as @pb pointed out.
c # serialization
FryHard Sep 17 '08 at 10:08 2008-09-17 10:08
source share