2012-11-29 61 views
2

我不是很熟悉反射,但我一直在嘗試獲取類屬性的值的幾天這段代碼。我正在使用API​​來查找VisualCron程序管理的cron作業中的值。字符串值被遺漏PropertyInfo.GetValue

我會解釋一下結構。每個cron作業都有幾個任務,它們都有自己的設置。這些設置存儲在聲明像這樣的TaskClass類中的屬性:

Public Property <propertyname> As <classname> 

每個屬性綁定到自己的類,所以例如裏面有TaskClass一個執行財產聲明如下:

Public Property Execute As TaskExecuteClass 

Inside TaskExecuteClass是包含我需要的值的屬性。通過下面的代碼塊,我能夠檢索所有類型EXCEPT字符串的屬性值。巧合的是,字符串值是我需要得到的唯一值。

我知道我所寫的內容一定有問題,這是因爲我無法在大量搜索之後找到任何具有類似問題的人。任何人都可以幫助我嗎?

Dim strAdd As String = "" 
For Each t As VisualCronAPI.Server In vcClient.Servers.GetAll() 
    For Each f As VisualCron.JobClass In t.Jobs.GetAll 
    For Each s As VisualCron.TaskClass In f.Tasks 
     Dim propVal As Object 
     Dim propInfo As PropertyInfo() = s.GetType().GetProperties() 
     For i As Integer = 0 To propInfo.Length - 1 
     With propInfo(i) 
      If s.TaskType.ToString = propInfo(i).Name.ToString Then 
      Dim asm As Assembly = Assembly.Load("VisualCron") 
      Dim typeName As String = String.Format("VisualCron.{0}", propInfo(i).PropertyType.Name) 
      Dim tp As Type = asm.GetType(typeName) 
      Dim construct As ConstructorInfo = tp.GetConstructor(Type.EmptyTypes) 
      Dim classInst As Object = construct.Invoke(Nothing) 
      Dim classProps As PropertyInfo() = classInst.GetType().GetProperties() 
      For h As Integer = 0 To classProps.Length - 1 
       With classProps(h) 
       If .GetIndexParameters().Length = 0 Then 
        propVal = .GetValue(classInst, Nothing) 
        If Not propVal Is Nothing Then 
        strAdd = f.Name & " - " & s.Name & " - " & .Name & " - " & propVal.ToString 
        End If 
       End If 
       If strAdd <> "" Then 
        ListBox1.Items.Add(strAdd) 
       End If 
       End With 
      Next 
      End If 
     End With 
     Next 
    Next s 
    Next f 
Next t 
+0

你能展示你正在使用的任務類之一的定義,包括字符串嗎? –

+0

@ChrisDunaway我很抱歉,一個定義到底需要什麼?屬性列表及其類型? – Tim

+0

我在想什麼TaskClass看起來像。是否缺少聲明爲公共的字符串?你確定他們是屬性而不僅僅是領域? –

回答

0

我解決了我自己的問題,雖然以糟糕的方式。這可能是非常低效的,但在我的特殊情況下速度不是必需的。這裏是工作的代碼:

Dim strAdd As String = "" 
For Each t As VisualCronAPI.Server In vcClient.Servers.GetAll() 
    For Each f As VisualCron.JobClass In t.Jobs.GetAll 
    For Each s As VisualCron.TaskClass In f.Tasks 
     Dim propVal As Object 
     Dim propInfo As PropertyInfo() = s.GetType().GetProperties() 
     For i As Integer = 0 To propInfo.Length - 1 
     With propInfo(i) 
      If s.TaskType.ToString = propInfo(i).Name.ToString Then 
      Dim asm As Assembly = Assembly.Load("VisualCron") 
      Dim typeName As String = String.Format("VisualCron.{0}", propInfo(i).PropertyType.Name) 
      Dim tp As Type = asm.GetType(typeName) 
      Dim construct As ConstructorInfo = tp.GetConstructor(Type.EmptyTypes) 
      Dim classInst As Object = construct.Invoke(Nothing) 
      Dim classProps As PropertyInfo() = classInst.GetType().GetProperties() 
      For h As Integer = 0 To classProps.Length - 1 
       With classProps(h) 
       If .GetIndexParameters().Length = 0 Then 
        propVal = .GetValue(CallByName(s, propInfo(i).Name.ToString, [Get]), Nothing) 
        If Not propVal Is Nothing Then 
        If propVal.ToString.Contains("\\server\") Or propVal.ToString.Contains("\\SERVER\") Then 
         strAdd = f.Name & " - " & s.Name & " - " & .Name & " - " & propVal.ToString 
         ListBox1.Items.Add(strAdd) 
        End If 
        End If 
       End If 
       End With 
      Next 
      End If 
     End With 
     Next 
    Next s 
    Next f 
Next t 

是由區別的一段代碼是

classProps(h).GetValue(CallByName(s, propInfo(i).Name.ToString, [Get]), Nothing) 

線。

如果有任何改善此代碼的建議 - 我假設我在這裏仍然有很多錯誤 - 請爲此答案的未來觀衆留言,以便我可以調整我的代碼並學習更多關於如何所有這些工作。