2008-11-21 20 views
7

Yansıma'ya ilk kez giriyorum ve gerçekten sıkışmış durumdayım. Düşünebildiğim her şeyi araştırdım. Şimdi olmak istediğim% 90'ım.PropertyInfo.GetValue() "Nesne, hedef türle eşleşmiyor."

Yansıma aracılığıyla özel bir sınıftaki bir özelliğin değerini döndürmeye çalışıyorum.

Public Class Class2 
    Private newPropertyValue2 As String 

    Public Property NewProperty2() As String 
     Get 
      Return newPropertyValue2 
     End Get 
     Set(ByVal value As String) 
      newPropertyValue2 = value 
     End Set 
    End Property 
End Class 

Ben yansıma yoluyla sınıfa bakmak için yazdım sınıf şuna benzer::

İşte benim sınıf ilanıdır

Public Class ObjectCompare 
    Private _OriginalObject As PropertyInfo() 

    Public Property OriginalObject() As PropertyInfo() 
     Get 
      Return _OriginalObject 
     End Get 
     Set(ByVal value As PropertyInfo()) 
      _OriginalObject = value 
     End Set 
    End Property 

    Public Sub CompareObjects() 
     Dim property_value As Object 

     For i As Integer = 0 To OriginalObject.Length - 1 
      If OriginalObject(i).GetIndexParameters().Length = 0 Then 
       Dim propInfo As PropertyInfo = OriginalObject(i) 

       Try 
        property_value = propInfo.GetValue(Me, Nothing) 
       Catch ex As TargetException 
       End Try 
      End If 
     Next 
    End Sub 
End Class 

Ben PROPERTY_VALUE = propInfo bir kesme noktası koydu. Sonuçta ne olduğunu görmek için GetValue (Me, Nothing) satırı. Ben PropertyName ve Type görebilirsiniz yansıması sayesinde

Dim test As New Class2 
test.NewProperty2 = "2" 

Dim go As New ObjectCompare 
Dim propInf As PropertyInfo() 
propInf = test.GetType.GetProperties() 

go.OriginalObject = propInf 

go.CompareObjects() 

, tüm ihtiyacım Mülkiyet değeridir:

İşte benim kod çağırmak nasıl! Şimdi kesme noktasına geldiğimde, bir TargetException alıyorum ve hata mesajı "Nesne hedef türüyle eşleşmiyor" yazıyor. Şimdi sabah 1AM ve ben harap oldum, şu anda herhangi bir yardım takdir edilecektir.

cevap

20

MePropertyInfo nesneler türetildiği sınıfı (Class2) farklıdır ObjectCompare nesne, ifade eder); eğlenmek için son kez daha sonra ölüme MSDN ve Google'ı arama ve ettik. Ayrıca, PropertyInfo nesnelerini aldığınız nesnenin bir nesnesini de geçirmeniz gerekir.

Public Sub CompareObjects(ByVal It as Object) 
    Dim property_value As Object 

    For i As Integer = 0 To OriginalObject.Length - 1 
     If OriginalObject(i).GetIndexParameters().Length = 0 Then 
      Dim propInfo As PropertyInfo = OriginalObject(i) 

      Try 
       property_value = propInfo.GetValue(It, Nothing) 
      Catch ex As TargetException 
      End Try 
     End If 
    Next 
End Sub 

go.CompareObjects(test) 
+0

Sadece uyandı ve bunu bir verdim ve bir çekicilik gibi çalışıyor! GetValue yönteminin ilk parametresinin, hangi PropertyInfo nesnesinden bir değer almak istediğini belirttiğini düşündüm. Tekrar teşekkürler! – StevenMcD

+0

+1 bu benim için de çalıştı. Benim durumum farklıydı; PropInfo.GetValue (It) kullanıyordum, ancak özellik infos yanlış sınıftan alındı. Şerefe dostum. – ashes999

+0

+1 GetValue yöntemini kullanarak sorun yaşıyordum. GetValue'nun PropertyInfo'nun oluşturulduğu nesne ile çağrıldığından emin olmanın açıklaması açıktı! –

1

Ben burada yapmaya çalışıyoruz ama bunu bir bıçak gerekecek biliyorum gerçekten emin değilim.

çağrılması:

 Dim test As New Class2 
     test.NewProperty2 = "2" 


     Dim go As New ObjectCompare 
     go.CompareObjects(test) 

Class:

Public Class Class2 
    Private newPropertyValue2 As String 

    Public Property NewProperty2() As String 
     Get 
      Return newPropertyValue2 
     End Get 
     Set(ByVal value As String) 
      newPropertyValue2 = value 
     End Set 
    End Property 
End Class 

karşılaştırın:

İşte

Ben geldi kodudur
Public Class ObjectCompare 

    Public Sub CompareObjects(ByVal MyType As Object) 

     For Each Prop In MyType.GetType().GetProperties() 
      Dim value = Prop.GetValue(MyType, Nothing) 
      Console.WriteLine(value) 
     Next 
     Console.ReadLine() 
    End Sub 
End Class 
İlgili konular