EDN Admin
Well-known member
That is a few time that I see some peoples, including some well know Microsoft employees, saying that if a structure (not a class) is pass as a generic argument to a method (Therefore the type of the fields are unknown at design time), it is impossible
to set inside the method the fields of the structure, even using reflection.
I am surprised that so many people are saying that since it is actually fairly easy to do. The trick is simply to box the structure.
This example
1) receive a structure as generic argument,
2) read a line from a CDF
3) Will find the type of the structure and create a boxed structure of the same type
4) will find the type of every field, cast the strings read to the right type and set those fields
5) unbox the structure and return it
<pre class="prettyprint lang-vb Public Function ReadLineToStructure(Of T)(ByVal Struct As T) As T
Dim StructType As Type = Struct.GetType
Dim line As String() = Me.ReadLineCDF(",")
Dim Index As Integer = 0
If StructType.IsValueType Then True => Struct is a structure
Dim Box As ValueType = DirectCast(Activator.CreateInstance(StructType, Nothing, Nothing), ValueType)
For Each field As Reflection.FieldInfo In StructType.GetFields
Dim StringCastedType As Object = Convert.ChangeType(line(Index), field.FieldType)
field.SetValue(Box, StringCastedType)
Index += 1
Next
Dim UnboxedObject As Object = Convert.ChangeType(Box, StructType)
Return DirectCast(UnboxedObject, T)
Else
Struct is an instance ... We know how to do it
End If
End Function[/code]
<br/>
An example of structure could be
<pre class="prettyprint lang-vb Structure Z
Public a As String
Public b As Integer
Public c As Integer
Public d As Boolean
End Structure[/code]
<br/>
If the line read is
CrazyPennie, 4, 1234, True
View the full article
to set inside the method the fields of the structure, even using reflection.
I am surprised that so many people are saying that since it is actually fairly easy to do. The trick is simply to box the structure.
This example
1) receive a structure as generic argument,
2) read a line from a CDF
3) Will find the type of the structure and create a boxed structure of the same type
4) will find the type of every field, cast the strings read to the right type and set those fields
5) unbox the structure and return it
<pre class="prettyprint lang-vb Public Function ReadLineToStructure(Of T)(ByVal Struct As T) As T
Dim StructType As Type = Struct.GetType
Dim line As String() = Me.ReadLineCDF(",")
Dim Index As Integer = 0
If StructType.IsValueType Then True => Struct is a structure
Dim Box As ValueType = DirectCast(Activator.CreateInstance(StructType, Nothing, Nothing), ValueType)
For Each field As Reflection.FieldInfo In StructType.GetFields
Dim StringCastedType As Object = Convert.ChangeType(line(Index), field.FieldType)
field.SetValue(Box, StringCastedType)
Index += 1
Next
Dim UnboxedObject As Object = Convert.ChangeType(Box, StructType)
Return DirectCast(UnboxedObject, T)
Else
Struct is an instance ... We know how to do it
End If
End Function[/code]
<br/>
An example of structure could be
<pre class="prettyprint lang-vb Structure Z
Public a As String
Public b As Integer
Public c As Integer
Public d As Boolean
End Structure[/code]
<br/>
If the line read is
CrazyPennie, 4, 1234, True
View the full article