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



我正在尝试转换

 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)的类类型时,我无法获得值,并且不会将其转换为属性变量。

问题:为什么我不能得到的值,并将其转换为属性变量时,在传递一个类类型,有一个非属性变量?

解决方案

感谢下面评论部分的家伙帮助我可视化的事实,我是问一个对象的属性,而对象只有字段。

这里是函数的更新版本对于那些感兴趣的

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

相关内容

  • 没有找到相关文章

最新更新