2012-11-29 28 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) 

线。

如果有任何改善此代码的建议 - 我假设我在这里仍然有很多错误 - 请为此答案的未来观众留言,以便我可以调整我的代码并学习更多关于如何所有这些工作。