In my experience even types which implement ISerializable must be marked with the SerializableAttribute or a runtime exception will result. No reason not to define it anyway.
That said, the most concise way at runtime would be the System.Type.IsSerializable property:
GetType(YourType).IsSerializable
(typeof(YourType).IsSerializable in C#)
or
yourInstance.GetType().IsSerializable
while the above methods are valid I do find this clearer and to the point. This covers your basic Binary and Soap serialization. However, with regards to XmlSerialization, all types are potentially serializable however they could as easily not be. Checking for the IXmlSerializable interface implies that the type author expected the type to be XmlSerialized (which means it's more likely that serialization will not fail), however this does not hold for the converse - that is to say that if they didn't implement IXmlSerialization it doesn't mean that you can't serialize it anyway.
How can I know whether a type(e.g. BindingList<>) is Serializable, both in design time and run time
That's a really vague question. A class can serialize data in many ways. Many of which you'd never be able to know about. If your question is, how can I tell if a class implements standard .NET serialization (ergo ISerializable), then you can simply try and cast the object to an ISerializable object. E.g.:
System.Runtime.Serialization.ISerializable serializable myObject as System.Runtime.Serialization.ISerializable;
How to know Is Serializable?
PeteDev1
The type can be serializable in two way
Aautomatic by using [Serializable] or
manually by implementing ISerializable Interface
so to check for a type is it serializable or not there are two ways
1-using reflection (i think it will work for both types of serialization but i do not test it)
BindingList<yourType> tee = new BindingList<yourType>(); Type tc = tee.GetType(); if ((tc.Attributes & TypeAttributes.Serializable) != 0){
MessageBox.Show("Serializable");}
else{
MessageBox.Show("Not Serializable");}
seconed for manual type
you can use is key word
BindingList<yourType> tee = new BindingList<yourType>();
Type tc = tee.GetType();
if (tc is ISerializable )
{
MessageBox.Show("Serializable");
}
else
{
MessageBox.Show("Not Serializable");
}
i hope this help
kalprin
In my experience even types which implement ISerializable must be marked with the SerializableAttribute or a runtime exception will result. No reason not to define it anyway.
That said, the most concise way at runtime would be the System.Type.IsSerializable property:
GetType(YourType).IsSerializable
(typeof(YourType).IsSerializable in C#)
or
yourInstance.GetType().IsSerializable
while the above methods are valid I do find this clearer and to the point. This covers your basic Binary and Soap serialization. However, with regards to XmlSerialization, all types are potentially serializable however they could as easily not be. Checking for the IXmlSerializable interface implies that the type author expected the type to be XmlSerialized (which means it's more likely that serialization will not fail), however this does not hold for the converse - that is to say that if they didn't implement IXmlSerialization it doesn't mean that you can't serialize it anyway.
vb2005
System.Runtime.Serialization.ISerializable serializable myObject as System.Runtime.Serialization.ISerializable;
if(myObject == null)
{
MessageBox.Show("Object is not serializable");
}
-- Peter
gibic
yes if the type impelement the ISerializable interface then
if(yourtype is ISerializable)
will return true
Vaassu
You means the second method can test for types which implement ISerializable, not those using attribute
Thanks.