Could someone please explain what’s the difference between inheriting from ISerializable interface and declaring your object as [Serializable]?
I know that in the first case you are have to implement the ISerializable interface members, while in the second case this work is likely to be done by the C# itself.
But what doesn’t make sense to me then is the following behavior:
public void Foo<T>() where T : ISerializable
{
// Whatever
}
Now, if I have some class like this:
[Serializable]
public class Value
{
public String Value { get; set; }
}
And unfortunately I can’t call my X.Foo<Value>(), because the compiler says:
There is no implicit reference conversion from 'Value' to 'System.Runtime.Serialization.ISerializable'
I’m pretty sure it’s my misunderstanding of something obvious, so please point out what am I doing wrong.
UPDATE (IMPORTANT 🙂
How do I make the where T : ISerializable statement work with [Serializable] class too? Is there a way?
What I’m trying to achieve is the compilation-time error if the supplied type T is not serializable (by using [Serializable] or ISerializable way).
Obviously, my current check handles only the second case, so how do I make it handle both of them?
Serializableis merely an attribute you place on a class to let classes such asSoapFormatterknow (via reflection) it can be serialized. Decorating a class with an attribute does not make a class implement an interface, which is why the compiler complains in your case. If memory serves, one implementsISerializableif one wants more control over the serialization process.