2016-07-31 32 views
-3

由于三是只读,并取决于非财产(四)。三个应该是未定义的。
没有设置四个的道具如何将三个设置为正确的值?为什么执行这个移动相应的工作?

public sub main 
    Dim x1 = New one, x2 = New two 
    x1.one = 3 
    MoveCorresponding(x1, x2) 
end sub 

Public Sub MoveCorresponding(a As Object, b As Object, Optional ignoreList As List(Of String) = Nothing) 
    Dim oType As Type = b.GetType 
    Dim iType = a.GetType 
    Dim props = iType.GetProperties() 
    For Each pInf As PropertyInfo In props 
     Dim parName = pInf.Name 
     If pInf.CanWrite Then 
      Try 
       Dim val = pInf.GetValue(a, Nothing) 
       pInf.SetValue(b, val) 
      Catch ex As Exception 
       MsgBox(ex.Message) 
      End Try 
     End If 
    Next 

End Sub 

End Class 

Class one 
    Public Property one As String = "1" 
    Dim four As Integer = 1 
    Public ReadOnly Property three As Integer 
     Get 
      Return four * 10 
     End Get 
    End Property 

End Class 

回答

1

你,你把它声明的同一行设置four

Dim four As Integer = 1 '<-- see? it's set to "1". 

酒店three只是从four计算:

Return four * 10 

难道我不理解你的问题?

-1

这不回答这个问题。 但为什么要这样写代码?

这很混乱。

dim four as integer = 1 ' <=== But why? Why use four as object name but hold 1 as it's value?? 
+0

这应该是一个评论下的问题,而不是一个答案。 –