c# - determine if a type/object is serialzable

In WCF, primitives type are serialzable, and some commonly used types are also serialzable, but some types are not serialzable , and when this types to be used in wcf as the data contract for message, then you might get into trouble.

there are ways to determine if a type is serializable or an instance is serializable. To check if a type (on the type object itself)

typeof(T).IsSerializable

 

or you can check on the instance 

private static bool IsSerializable(T obj)
{
    return ((obj is ISerializable) || (Attribute.IsDefined(typeof (T), typeof (SerializableAttribute))));
}

 

But be caereful that IsSerializable does not take into account the all types in the graph of objects being serialized for the serialable attribute.

You might want to check on that the reference page - how to check if an object is serializable in C#

References:

how to check if an object is serializable in C#

你可能感兴趣的:(C#)