Okay, so let me see if I can make this as concise as possible. I am going to be passing in an object of an unknown type into a method that is going to internally use the BinaryFormatter to serialize the data it’s passed (I chose this because I have no idea what the data is so it’s the most abstract mechanism I could imagine). And let’s assume that method looks like this currently:
public void ProvideData(Guid providerKey, ISerializable data, string dataType)...
Now let’s assume I need to make sure what’s passed to me can in fact be serialized and so that why I thought I would require the object to implement ISerializable. However, one issue with this model is that I can’t even pass in a string because eventhough a string is [Serializable] it doesn’t implement ISerializable.
So, what is the proper way to structure this method to ensure that the value passed to me, simple or complex, is serializable?
You can check by using the IsSerializable property on the
Type.For example:
EDIT BY OP: Final Implemented Method
Below is the final implementation because of this answer (very good answer). It is just a prototype so that’s why there’s not a lot going on in the method but it proves the answer. One thing to note is that checking for the existence of the
ISerializableinterface holds no value because you won’t know until you try and serialize the object whether or not it should have implementedISerializableso I was moving down the wrong path there.Thanks!