Basically I have a system that will take an unknown structure (that should be marked serializable) and attempt to serialize it to a stream. However, before the system will do that, I want to know if there's a way to check every single variable/property to make sure that all the types are serializable.
Anyone know a way?
EDIT: For now, I was able to do this:
I don't know if it works yet actually, kind of sad. I should probably test this out. Anyone have any suggestions still?Code:Private Function IsSerializable(ByVal obj As [Object]) As [Boolean] If obj.GetType.IsSerializable Then For Each _field As Reflection.FieldInfo In obj.GetType().GetFields(Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static) If Not _field.GetType().IsSerializable Then Return False Next Return True End If Return False End Function
EDIT 2: The above code does not work, GetFields() Returns nothing and forces an exit. Changing to have no binding flags seems to work but the issue is if there's another structure inside then it would not be able to check it...the simplest method of this all is to just try serializing it and throwing an exception if it fails but that feels dirty.




Reply With Quote