Is there any way to test if a type is Serializable by the NetDataContractSerializer in code? I'm writing a generic class that uses the NetDataContractSerializer internally and want to throw an exception in the constructor if there is an attempt to use it with a type that it can't handle.
Here's what I found so far. Is there a simpler way?
Private Sub ConfirmTypeIsNetDataContractSerializable(type As Type)
Dim specialAllowedTypes As Type() = {GetType(Decimal), GetType(String), _
GetType(DateTime), GetType(DateTimeOffset), GetType(TimeSpan), _
GetType(Guid), GetType(Uri), GetType(XmlQualifiedName), GetType(XmlElement), GetType(XmlNode)}
If type.IsPrimitive() OrElse specialAllowedTypes.Contains(type) Then Return
If type.IsDefined(GetType(DataContractAttribute), inherit:=False) Then Return
If type.IsDefined(GetType(SerializableAttribute), inherit:=False) Then Return
If type.IsEnum Then Return
If type.GetInterfaces().Contains(GetType(IXmlSerializable)) Then Return
Dim serializer As New NetDataContractSerializer
Using dummyStream As New System.IO.MemoryStream()
serializer.Serialize(dummyStream, Activator.CreateInstance(type)) ' throws exception if not serializable
End Using
End Sub