字符串值不在PropertyInfo.GetValue中

时间:2012-11-29 23:58:26

标签: vb.net reflection

我对Reflection并不是很精通,但是我一直在研究这段代码,试图获取类属性的值。我正在使用API​​来查找由VisualCron程序管理的cron作业中的值。

我会稍微解释一下这个结构。每个cron作业都有几个任务,它们有自己的设置。这些设置存储在TaskClass类中的属性中,这些属性声明如下:

Public Property <propertyname> As <classname>

每个属性都绑定到它自己的类,所以例如在TaskClass中有一个Execute属性,声明如下:

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

1 个答案:

答案 0 :(得分: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)

线。

如果有任何改进此代码的建议 - 我假设我在这里仍然有很多错误 - 那么请评论此答案的未来观众,以便我可以调整我的代码并了解更多关于这一切是如何运作的。