Hi again!
I pushed further that exemple
I designed a strong type collection inherited from CollectionBase, for
Player..Player only have field myName...I added 4 players to collection
All I could came up with was
Case MemberTypes.Field
Dim myMemberInfo As FieldInfo =
oType.GetField(oMemberInfo.Name, BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Instance)
Try
For Each o As Object In
CType(myMemberInfo.GetValue(oObj), CollectionBase)
strFlagedFields +=
SerializeTypeMembers(o)
Next
GoTo continue
Catch e As Exception
End Try
Try
For Each o As Object In
CType(myMemberInfo.GetValue(oObj), Array)
strFlagedFields +=
SerializeTypeMembers(o)
Next
GoTo continue
Catch e As Exception
End Try
strFlagedFields = strFlagedFields + "Field " +
oMemberInfo.Name + " have value " + myMemberInfo.GetValue(oObj).ToString +
vbCrLf
End Select
continue:
Now the listing:
Field FirstMember have value 1234
Field thirdMember have value 222
Field fourMember have value 3333
Field FirstMemberT1 have value aaaa
Field FirstMemberT1 have value bbbbb
Field FirstMemberT1 have value ccccc
Field myName have value Player1
Field myName have value Player2
Field myName have value Player3
Field myName have value Player4
As you can see, I succeded listing all members from my graph..but...
I dont like that try's-catch specyaly because of forcing the returned type
of the field to CollectionBase..that mean I need to add every type of
collection there...
(
Crirus
Crirus said:
I got this far in custom serialisation issue:
Option Strict On
Imports System.Reflection
Imports Extensions.CustomAttributes
Namespace Extensions.CustomAttributes
<AttributeUsage(AttributeTargets.Field, Inherited:=True,
AllowMultiple:=True)> _
Public Class NoteAttribute
Inherits System.Attribute
End Class
End Namespace
Public Class Test
<Note()> Public FirstMember As Integer = 1234
Public secondMember As Integer = 11
<Note()> Public thirdMember As Integer = 222
<Note()> Public fourMember As Integer = 3333
<Note()> Public test1List() As Test1 = {New Test1("aaaa"), New
Test1("bbbbb"), New Test1("ccccc")}
End Class
Public Class Test1
<Note()> Public FirstMemberT1 As String
Public secondMemberT1 As Integer = 11
Public Sub New(ByVal firstmemberValue As String)
FirstMemberT1 = firstmemberValue
End Sub
End Class
Module modComments
Public Sub Main()
Dim myTest As New Test
Dim strAllFlagged As String = SerializeTypeMembers(myTest)
Console.Write(strAllFlagged)
End Sub
Private Function SerializeTypeMembers(ByVal oObj As Test) As String
Dim strFlagedFields As String
Dim oAttribs() As Object
' Get members of type
Dim oType As Type = oObj.GetType
Dim oMembersInfo() As MemberInfo = oType.GetMembers
For Each oMemberInfo As MemberInfo In oMembersInfo
' Determine if attribute is present
oAttribs = oMemberInfo.GetCustomAttributes(False)
If oAttribs.Length > 0 Then 'I dont test for multiple custom
attribute here
If TypeOf oAttribs(0) Is NoteAttribute Then
' determine member type
Select Case oMemberInfo.MemberType
Case MemberTypes.Field
Dim myMemberInfo As FieldInfo =
oType.GetField(oMemberInfo.Name, BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Instance)
strFlagedFields = strFlagedFields + "Field " +
oMemberInfo.Name + " have value " + myMemberInfo.GetValue(oObj).ToString +
vbCrLf
End Select
End If
End If
Next
Return strFlagedFields
End Function
End Module
this is the output by now:
Field FirstMember have value 1234
Field thirdMember have value 222
Field fourMember have value 3333
Field test1List have value Test1[]
Now, I have to figure out that test1List is an aray of instances of type
Test1 and make the same call for them as for myTest
I'm questioning myself how they do it on serialisation?