2012-11-29 4 views
2

저는 Reflection에 익숙하지 않지만 클래스 속성 값을 얻기 위해 며칠 동안이 코드를 작업 해 왔습니다. API를 사용하여 VisualCron 프로그램에서 관리하는 cron 작업 내부의 값을 찾습니다.PropertyInfo.GetValue에서 문자열 값이 누락되었습니다.

구조에 대해 조금 설명하겠습니다. 각 cron 작업에는 자체 설정이있는 여러 작업이 있습니다. 예를 들어 다음과 같이 선언 TaskClass 내부의 실행 특성이 있으므로, 각 속성은 자신의 클래스에 연결되어

Public Property <propertyname> As <classname> 

: 설정은과 같이 선언 된 TaskClass 클래스 내부 속성에 저장됩니다

Public Property Execute As TaskExecuteClass 

Inside TaskExecuteClass는 필요한 값을 보유하고있는 속성입니다. 아래 코드 블록을 사용하면 모든 유형의 문자열을 제외하고 속성 값을 검색 할 수있었습니다. 우연히도, 문자열 값은 내가 얻을 필요가있는 유일한 값입니다.

많은 문제가있는 사람과 비슷한 문제가있는 사람을 찾을 수 없기 때문에 내가 작성한 내용에 문제가 있다는 것을 알고 있습니다. 누구든지 제발 도와 주실 래요?

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) 

라인했다 : 여기에 작동하는 코드입니다.

이 코드를 개선하기위한 제안 사항이 있으시면 - 여기에도 여전히 많은 실수가 있다고 가정합니다 -이 답변의 미래 시청자를위한 의견을 말하십시오. 그래서 코드를 조정하고 학습 할 수 있습니다. 이 모든 것이 어떻게 작동하는지에 대한 자세한 정보.

관련 문제