You can check using the IsSerializable property on Type .
For example:
bool canSerialize = myParameter.GetType().IsSerializable;
EDIT BY OP: Final Implemented Method
Below is the final implementation due to this answer (very good answer). This is just a prototype, so why there is not much in the method, but it proves the answer. It should be noted that checking for the presence of the ISerializable interface ISerializable not matter, because you will not know until you try and serialize the object, regardless of whether it has ISerializable , so I moved the wrong way.
Thanks!
public void ProvideData(Guid providerKey, object data, string dataType) { if (!data.GetType().IsSerializable) { throw new ArgumentException("The data passed is not serializable and therefore is not valid.", "data"); } var formatter = new BinaryFormatter(); using (var fileStream = new FileStream("data.dat", FileMode.Create)) { formatter.Serialize(fileStream, data); fileStream.Close(); } }
Dave R.
source share