【问题标题】:Convert a Variable To a Propery Variable Using Reflection使用反射将变量转换为属性变量
【发布时间】:2014-09-22 16:43:45
【问题描述】:

我正在尝试转换

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

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

【问题讨论】:

  • 不是因为你写了oSourceObjectProperties = oSourceObjectType.GetProperties()吗?
  • Alireza 是对的。我想你想.GetFields.
  • 谢谢你们 - 我不敢相信它是如此明显,我没有看到它!代码士兵!!!!!!
  • 如果您必须做很多此类事情,我强烈建议您查看 AutoMapper:nuget.org/packages/AutoMapper/3.2.1。这是一个工具,只需很少的设置即可自动执行大量“从这种类型转换为那种类型”的操作。
  • 感谢您的建议,克里斯!我没有深入研究它,但 AutoMapper 似乎需要更长的时间才能完成相同的任务。我可能是错的,如果我错了,请纠正我 - 但我相信直接使用反射可能比使用 AutoMapper 更有效,AutoMapper 是一个也使用反射为您自动映射的库。这是我用于当前项目的库,直到由于同样的原因我被告知不要使用它。不过感谢您的建议!

标签: vb.net class reflection types typeconverter


【解决方案1】:

解决方案

感谢下面评论部分的人帮助我想象我在向对象询问属性而对象只有字段的事实。

这里是功能的更新版本,感兴趣的朋友可以看看

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

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 2017-07-10
    • 2021-03-16
    • 2018-09-12
    • 2013-04-25
    • 2018-09-14
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多