Results 1 to 6 of 6

Thread: [RESOLVED] Get all types in a structure

Threaded View

  1. #1

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Resolved [RESOLVED] Get all types in a structure

    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:
    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
    I don't know if it works yet actually, kind of sad. I should probably test this out. Anyone have any suggestions still?


    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.
    Last edited by formlesstree4; Jun 27th, 2011 at 06:57 PM. Reason: Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width