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看起来像。是否缺少声明为公共的字符串?你确定他们是属性而不仅仅是领域? –