使用反射将变量转换为属性变量

时间:2014-07-30 21:10:49

标签: vb.net class reflection types typeconverter

我正在尝试转换

 Public Class TestClass

    Public FirstName As String 

 End Class

 Public Class AnotherClass 
        Public Property FirstName As String 

 End Class 

我写了一个函数,它将一个类的成员转换为另一个类的成员,所以如果我传递一个具有Public Property LastName AS String的类类型,它会将它转换为(例如)AnotherClass Type变量我将能够获得价值,所以我在这里很开心。

 Public Shared Function ConvertModelToValidationDataModel(Of T)(ByVal oSourceObject As Object) As T
        Dim oSourceObjectType As Type
        Dim oSourceObjectProperties() As PropertyInfo

        Dim oDestinationObjectProperties() As PropertyInfo

        Dim oDestinationObject As Object
        Dim oDestinationObjectType As Type

        oDestinationObject = Activator.CreateInstance(Of T)()

        oDestinationObjectType = GetType(T)

        oDestinationObjectProperties = oDestinationObjectType.GetProperties

        oSourceObjectType = oSourceObject.GetType()
        oSourceObjectProperties = oSourceObjectType.GetProperties()

        If Not oSourceObjectProperties Is Nothing Then

            If oSourceObjectProperties.Count > 0 Then

                For Each oDestinationObjectPropertyInfo As PropertyInfo In oDestinationObjectProperties

                    For Each oSourceObjectPropertyInfo As PropertyInfo In oSourceObjectProperties

                        If oDestinationObjectPropertyInfo.Name = oSourceObjectPropertyInfo.Name Then

                            oDestinationObjectPropertyInfo.SetValue(oDestinationObject, oSourceObjectPropertyInfo.GetValue(oSourceObject, Nothing))

                        End If

                    Next
                Next
            End If
        End If

        Return oDestinationObject
    End Function

问题是我要传递TestClass变量FirstName不是属性,但我希望它转换为属性变量)并且能够转换它并获取值但由于某种原因它没有传递值,显然它看起来像函数将其转换为另一个类的非属性变量 - 而不是像我希望它的属性变量

**

简短版本:

**

当我传入一个具有属性变量(Public Property FirstName As String)的类类型时 - 我返回另一个类的类,所有值都被传递并转换为属性变量。

当我传入包含变量(Public FirstName As String)的类类型时,我无法获取该值,并且它不会将其转换为属性变量。

问题:为什么在传入具有非属性变量的类类型时,我无法获取值并将其转换为属性变量?

1 个答案:

答案 0 :(得分:0)

解决方案

感谢下面评论部分中的人员帮助我想象一下这样一个事实,即我在询问对象的属性,而对象只有字段。

以下是感兴趣的人的功能的更新版本

Public Shared Function ConvertModelToValidationDataModel(Of T)(ByVal oSourceObject As Object) As T
    Dim oSourceObjectType As Type
    Dim oSourceObjectFields() As FieldInfo

    Dim oDestinationObjectProperties() As PropertyInfo

    Dim oDestinationObject As Object
    Dim oDestinationObjectType As Type

    oSourceObjectType = oSourceObject.GetType()
    oSourceObjectFields = oSourceObjectType.GetFields()

    oDestinationObject = Activator.CreateInstance(Of T)()

    oDestinationObjectType = GetType(T)

    oDestinationObjectProperties = oDestinationObjectType.GetProperties

    If Not oSourceObjectFields Is Nothing Then

        If oSourceObjectFields.Count > 0 Then

            For Each oSourceObjectFieldInfo As FieldInfo In oSourceObjectFields

                For Each oDestinationObjectPropertyInfo As PropertyInfo In oDestinationObjectProperties

                    If oSourceObjectFieldInfo.Name = oDestinationObjectPropertyInfo.Name Then

                        oDestinationObjectPropertyInfo.SetValue(oDestinationObject, oSourceObjectFieldInfo.GetValue(oSourceObject))

                    End If

                Next

            Next
        End If
    End If

    Return oDestinationObject
End Function