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
你能展示你正在使用的任務類之一的定義,包括字符串嗎? –
@ChrisDunaway我很抱歉,一個定義到底需要什麼?屬性列表及其類型? – Tim
我在想什麼TaskClass看起來像。是否缺少聲明爲公共的字符串?你確定他們是屬性而不僅僅是領域? –