|
-
Jun 27th, 2011, 05:18 PM
#1
[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
-
Jun 27th, 2011, 07:47 PM
#2
Re: Get all types in a structure
If you want to test the object's type, and the types of all its fields, and the types of all their fields, and the types of all their fields, etc, does that sound kind of recursive?
-
Jun 27th, 2011, 08:36 PM
#3
Re: Get all types in a structure
It does and I have a rather hack-y done code that seems to work!
Code:
''' <summary>
''' Loops through the entire object to see if it is indeed serializable.
''' </summary>
''' <param name="type"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function IsTypeSerializable(ByVal type As [Type]) As Boolean
'Final serializable result
Dim _result As Boolean = True
'Make sure our current type is serializable
If type.IsSerializable Then
'Ok, so now get any sub-fields.
Dim _subFields() As Reflection.FieldInfo = type.GetFields(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.DeclaredOnly Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
'Loop each subfield
For Each _field As Reflection.FieldInfo In _subFields
'Get the type
Dim _type As Type = _field.FieldType
'Write it out
Console.WriteLine(String.Format("Checking Type: {0}", _type.ToString()))
'Check
If _type Is GetType(String) Then
_result = True
ElseIf _type Is GetType(Integer) Then
_result = True
ElseIf _type Is GetType(Int32) Then
_result = True
ElseIf _type Is GetType(Int16) Then
_result = True
ElseIf _type Is GetType(Int64) Then
_result = True
ElseIf _type Is GetType(Long) Then
_result = True
ElseIf _type Is GetType(ULong) Then
_result = True
ElseIf _type Is GetType(UInt16) Then
_result = True
ElseIf _type Is GetType(UInt32) Then
_result = True
ElseIf _type Is GetType(UInt64) Then
_result = True
ElseIf _type Is GetType(UInteger) Then
_result = True
ElseIf _type Is GetType(UIntPtr) Then
_result = True
ElseIf _type Is GetType(IntPtr) Then
_result = True
ElseIf _type Is GetType(UShort) Then
_result = True
ElseIf _type Is GetType(Short) Then
_result = True
ElseIf _type Is GetType(Byte) Then
_result = True
ElseIf _type Is GetType(SByte) Then
_result = True
Else
_result = IsTypeSerializable(_type)
If Not _result Then
Console.WriteLine("Type {0} isn't serializable, exiting check!", _type.ToString())
Exit For
End If
End If
'Write out the result
Console.WriteLine("Type {0} {1} serializable", _type.ToString(), If(_result, "is", "is not"))
Console.WriteLine()
Next
'_result = True
Else
'The structure is not serializable
_result = False
End If
'Give back the result
Return _result
End Function
There's a long series if ElseIf's because I couldn't find a way to make it make sense at all otherwise; could end up getting false positives but I'll run some more testing soon!
Last edited by formlesstree4; Jun 27th, 2011 at 08:37 PM.
Reason: Small Update
-
Jun 27th, 2011, 08:40 PM
#4
Re: Get all types in a structure
You might want to look at the Type.IsPrimitive property.
-
Jun 27th, 2011, 08:52 PM
#5
Re: Get all types in a structure
 Originally Posted by jmcilhinney
You might want to look at the Type.IsPrimitive property.
It kept doing some funky stuff on me but I'll try it again!
EDIT: Fun fact, A string is not considered a primitive!
EDIT2: String reduced to Char, makes me feel stupid...anyway gonna fix this code up a little and update it.
Last edited by formlesstree4; Jun 27th, 2011 at 09:01 PM.
-
Jun 27th, 2011, 09:20 PM
#6
Re: Get all types in a structure
I made a codebank that contains the updated code. It works perfectly!
Here it is.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|