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

Vb.net 使用反射将变量转换为属性变量,vb.net,class,reflection,types,typeconverter,Vb.net,Class,Reflection,Types,Typeconverter,我正在努力改变信仰 Public Class TestClass Public FirstName As String End Class 到 我编写了一个函数,将一个类的成员转换为另一个类的成员,因此,如果我将某个具有公共属性LastName的类类型作为字符串传递,它将把它转换为(例如)另一个类类型变量,我将能够获得该值,因此我在这里很高兴 Public Shared Function ConvertModelToValidationDataModel(Of T)(ByV

我正在努力改变信仰

 Public Class TestClass

    Public FirstName As String 

 End Class

我编写了一个函数,将一个类的成员转换为另一个类的成员,因此,如果我将某个具有
公共属性LastName的类类型作为字符串传递
,它将把它转换为(例如)
另一个类类型
变量,我将能够获得该值,因此我在这里很高兴

 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

这不是因为您编写了
oSourceObjectProperties=oSourceObjectType.GetProperties()
?Alireza是对的。我想你们想要
.GetFields.
谢谢你们-我不敢相信这是显而易见的,我没有看到!!!代码士兵!!!!!如果你必须做很多这类事情,我强烈建议你看看AutoMapper:。这是一个只需很少的设置就能自动执行大量“从这种类型转换到那种类型”操作的工具。谢谢你的建议,克里斯!我并没有深入研究它,但是AutoMapper似乎需要更长的时间来完成同样的任务。我可能错了,如果我错了,请纠正我-但我相信直接使用反射可能比使用AutoMapper更有效,AutoMapper是一个库,它也使用反射为您自动化映射。这是我在当前项目中使用的一个库,直到出于同样的原因我被告知不要使用它。谢谢你的建议!
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