PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Re: Converting Structures to Classes with reflection

Reply

Re: Converting Structures to Classes with reflection

 
Thread Tools Rate Thread
Old 13-01-2007, 03:35 PM   #1
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
Default Re: Converting Structures to Classes with reflection


Nigel,
You are asking the output object for the value of the field.

> Outfields(j).SetValue(oOut,
> Infields(i).GetValue(oIn), Nothing)


You are asking the output object for a list of properties, but you only
define fields.

> Public Class TestClass
> Public Property TestStr As String

...
> Public Property TestInt As Integer

...
> Public Sub New()
> Me.TestInt = 0
> Me.TestStr = ""
> End Sub
> End Class


You are using ByRef parameters, but you don't assign a value to the
parameter itself.

> Public Function StructToClass(ByVal oIn As ValueType, ByVal oOut As
> Object) As Boolean


ByRef parameters allows a called function to modify the callers variables,
it is not used for "optimizing"! Classes are already Reference types.
Passing an object ByVal means you are passing a reference to the actual
object on the heap. Passing an object ByRef means you are passing a
reference to variable that contains a reference to the actual object on the
heap.

Try:

Public Class Converter

Public Function StructToClass(ByVal oIn As ValueType, ByVal oOut As
Object) As Boolean
Dim i As Integer
Dim Infields() As FieldInfo
Infields = oIn.GetType.GetFields

Dim j As Integer
Dim Outfields() As PropertyInfo
Outfields = oOut.GetType.GetProperties

For i = 0 To Infields.Length - 1
For j = 0 To Outfields.Length - 1
If Outfields(j).Name = Infields(i).Name Then
Outfields(j).SetValue(oOut,
Infields(i).GetValue(oIn), Nothing)
Exit For
End If
Next
Next
Return True
End Function
End Class

Public Class TestClass
Private m_testStr As String
Private m_testInt As Integer
Public Property TestStr() As String
Get
Return m_testStr
End Get
Set(ByVal value As String)
m_testStr = value
End Set
End Property
Public Property TestInt() As Integer
Get
Return m_testInt
End Get
Set(ByVal value As Integer)
m_testInt = value
End Set
End Property
Public Sub New()
Me.TestInt = 0
Me.TestStr = ""
End Sub
End Class

Public Structure TestStruct
Public TestStr As String
Public TestInt As Integer
End Structure

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Nigel" <Nigel@discussions.microsoft.com> wrote in message
news:EBE3D843-FDA8-43FA-8921-233A28B71BB8@microsoft.com...
> Hallo Everyone,
>
> I want to transfer the contents of a structure to a class. I have tried
> the
> following but it does not work, can anyone see what I have done wronG
>
> Imports System.Reflection
> Public Class Converter
> Public Function StructToClass(ByRef oIn As ValueType, ByRef oOut As
> Object) As Boolean
> Dim i As Integer
> Dim Infields() As FieldInfo
> Infields = oIn.GetType.GetFields
>
> Dim j As Integer
> Dim Outfields() As PropertyInfo
> Outfields = oOut.GetType.GetProperties
>
> For i = 0 To Infields.Length - 1
> For j = 0 To Outfields.Length - 1
> If Outfields(j).Name = Infields(i).Name Then
> Outfields(j).SetValue(oOut,
> Infields(i).GetValue(oOut), Nothing)
> Exit For
> End If
> Next
> Next
> Return True
> End Function
>
> Public Class TestClass
> Public TestStr As String
> Public TestInt As Integer
> Public Sub New()
> Me.TestInt = 0
> Me.TestStr = ""
> End Sub
> End Class
>
> Public Structure TestStruct
> Public TestStr As String
> Public TestInt As Integer
> End Structure
>
> <TestMethod()> Public Sub TestMethod1()
> Dim iOut As New TestClass
> Dim inData As TestStruct = Nothing
> inData.TestInt = 10
> inData.TestStr = "Hallo"
> Dim iConverter As New ClassLibrary1.Converter
> iConverter.StructToClass(inData, iOut)
> Assert.IsTrue(iOut.TestInt = 10) ' <--- Failes here
> Assert.IsTrue(iOut.TestStr = "Hallo")
> End Sub


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off