I'm trying to write a class that will be in charge of persisting application options. Since the options need to be persisted the values that I'm sent must be serialisable.
Initially I thought I've be able to write a method with a signature like this:
Public Sub SaveOption(Of T As ISerializable)(ByVal id As String, ByVal value As T)
or if you prefer C#:
public void SaveOption<T>(string id, T value) where T : ISerializable
In principle this would be fine, but what about types that have the <Serializable>
attribute? The most notable example of this is System.String, it does not implement ISerializable
, but clearly it is a type that I should be able to save.
So, is there a way that I can restrict which types are allowed into a method at compiletime, based on their attributes?
You could have overloads for the other types - taking your string example:
public void SaveOption(string id, string value)
However; serializability is... tricky; I expect you're going to have to check this at runtime.