在Reflection中,PropertyInfo和FieldInfo有什么区别?

来源:互联网 发布:笑道人捏脸数据 编辑:程序博客网 时间:2024/05/01 15:53
public   class   Test     {       public   int   A;       private   int   _b;       public   int   B       {           get           {               return   _b;           }       }     }         A就是Field,B就是Property。那么什么是FieldInfo,什么是PropertyInfo应该很显然了。不过呢,二者的确很相似也应该很相似(A,B不也很相似吗):都继承自MemberInfo。  Public Sub CopyEntity(ByRef DestObj As Object, ByRef SrcObj As Object)        Dim type As Type = SrcObj.GetType()        Dim p As PropertyInfo() = type.GetProperties()        Dim i As Integer        For i = 0 To p.Length - 1            p(i).SetValue(DestObj, p(i).GetValue(SrcObj, Nothing), Nothing)        Next    End Sub Private Function GetCriteria(ByVal fieldName As String) As AddrBookCriteria        Dim criteria As AddrBookCriteria = New AddrBookCriteria()        criteria.COMPCODE = Me.txtCode.Text.Trim        Dim type As Type = criteria.GetType()        Dim field As System.Reflection.FieldInfo = type.GetField(fieldName)        If field Is Nothing Then            Throw New Exception(String.Format("There is not this type[{0}] in the AddrBook", fieldName))        End If        field.SetValue(criteria, True)        Return criteria    End Function
原创粉丝点击