GetFields运算符

时间:2016-06-06 12:56:18

标签: vb.net entity-framework-6

我使用函数从列表中获取字段数据。我需要知道.GetFields运算符是否只返回字段,或者它是否真的用它们存储的数据填充它们?我认为在阅读msdn之后是后者,但我没有任何线索,而且我从未使用过这些"测试方法"之前:(。任何帮助表示赞赏!(或者即使你可以告诉我如何做一个测试方法会有所帮助!) 这是代码:

   ''' <summary>
''' This function will return all of the fields for a certain class as well as the data stored in them
''' </summary>
''' <param name="list"></param>
''' <returns></returns>
Public Shared Function GetFieldData(ByVal list As IList(Of Object)) As FieldInfo()

    Dim fields() As FieldInfo = list.Single.GetType().GetFields()

    Return fields

End Function

结束班

以下是创建新项目的代码

  ''' <summary>
''' This function will create new Before and After objects
''' everything should be passed in as a IEnum
''' </summary>
''' <param name="Before"></param>
''' <param name="After"></param>
''' <returns></returns>
Function NewItem(Before As IEnumerable(Of Object), After As IEnumerable(Of Object))

    If (Not ObjectsAreSameClass(Before, After)) Then    'If object classes are not the same, send an error message, else continue 
        'Pop error

    Else

        Dim BeforeFields() As FieldInfo = GetFieldData(Before)

        Dim AfterFields() As FieldInfo = GetFieldData(After)

        ObjectCounter += 1

        'Now check and make sure the objects are not the same
        If (BeforeFields.Equals(AfterFields)) Then
            'Objects are the same so pop error?

        End If

    End If

    Return Nothing
End Function

1 个答案:

答案 0 :(得分:1)

FieldInfo是有关该字段的信息,不包括其值。要获得该值,您必须提供该对象类型的实例。这是一个示例,您可以将其放在表单上以查看其工作原理:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim info() As FieldInfo = GetType(Form).GetFields(BindingFlags.NonPublic Or 
                                                      BindingFlags.Instance)
    For Each item As FieldInfo In info
        Dim value As Object = item.GetValue(Me) ' Get the value from 'Me'
        If Not IsNothing(value) Then
            Debug.Print("{0} = {1}", item.Name, value.ToString())
        Else
            Debug.Print("{0} = Nothing", item.Name)
        End If
    Next
End Sub