Update GetPropertyValue

This commit is contained in:
Jonathan Jenne
2019-05-09 17:07:53 +02:00
parent 809f8e2b43
commit ce21ae5dab
6 changed files with 104 additions and 45 deletions

View File

@@ -1,13 +1,17 @@
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Text.RegularExpressions
Public Class PropertyValues
Private Shared _indexPattern = "\((\d+)\)"
Private Shared _indexRegex As Regex = New Regex(_indexPattern)
Private Shared _indexRegex As New Regex(_indexPattern)
Public Shared Function GetPropValue(Obj As Object, PropertyName As String)
Public Shared Function GetPropValues(Obj As Object, PropertyName As String)
End Function
Public Shared Function GetPropValue(Obj As Object, PropertyName As String, Optional ReturnWhenEmpty As Boolean = True)
Dim oNameParts As String() = PropertyName.Split("."c)
Dim oIndexReplaceRegex = "\(\d+\)"
If IsNothing(Obj) Then
Return Nothing
@@ -34,15 +38,33 @@ Public Class PropertyValues
Return Nothing
End If
Obj = oInfo.GetValue(Obj, Nothing)
If IsNothing(Obj) Then
If IsNothing(oInfo.GetValue(Obj, Nothing)) Then
Return Nothing
End If
Obj = oInfo.GetValue(Obj, Nothing)
If oHasIndex Then
Obj = Obj(0)
End If
If IsArray(Obj) And Not oHasIndex Then
Dim oCurrentPart = oPart
Dim oPathFragment = PropertyName.
Split(New String() {"." & oCurrentPart & "."}, StringSplitOptions.None)
Dim oResults As New List(Of Object)
For Each oArrayItem In Obj
Dim oResult = GetPropValue(oArrayItem, oPathFragment(1), ReturnWhenEmpty:=False)
If Not IsNothing(oResult) Then
oResults.Add(oResult)
End If
Next
Return oResults
End If
Next
Return Obj
@@ -67,6 +89,4 @@ Public Class PropertyValues
Private Shared Function HasIndex(Prop As String) As Boolean
Return Regex.IsMatch(Prop, _indexPattern)
End Function
End Class