Check if the property is an array

I want to check if a property from a class is an array (only for massive arrays here, NOT character arrays [ie strings]). Then I want to iterate over the array (i.e., "Do something" with each element). See My try below. Thank!!

Editing:
So, a bit more information ... neither IsArray nor my method work yet to check the array. MSDN is "typeof (Array) .IsAssignableFrom (type)", but I was not sure how to do this work with property information here. But maybe someone knows how to use them, and I just did not use it correctly.

Inside the Class3 class, I define an array, but not its size. I use "redim" when I create it in another thread and load it before passing it to this function. When I insert a breakpoint in the code here, I can look at "myobject" and see the elements and values ​​of the array, but in fact I am looking for a pure use of the propertyinfo type to generalize this method. I also need to be able to index the array as soon as I determine that it is an array ... again using the info property, not "myobject" directly.

Public Class Class2
Private Shared filelock As New Object
Public Shared Sub write2file(ByVal myobject As Class3)
    SyncLock filelock
        Dim sb As New StringBuilder
        Using sw As StreamWriter = New StreamWriter(File.Open(newfilename, FileMode.Append, FileAccess.Write, FileShare.None))
            'Dim pinfo() As PropertyInfo = GetType(Class3).GetProperties
            Dim pinfo() As PropertyInfo = CType(myobject.GetType.GetRuntimeProperties, PropertyInfo())
            sb.Clear()
            For Each p As PropertyInfo In pinfo
                If Not p.GetIndexParameters.Length > 0 Then 'if property is not an array
                    sb.Append(p.GetValue(myobject)).Append(",")

                Else ' if property is an array
                    For x As Integer = 0 To p.GetIndexParameters.Length - 1
                        sb.Append(p.GetValue(myobject, New Object() {x})).Append(",") 'append each value from array to the stringbuilder in .CSV format
                    Next
                End If
            Next
            sw.WriteLine(sb) 'write string to file
        End Using

    End SyncLock

End Sub

Final class

+4
source share
2 answers

, ( - ... - , , ), , :

For Each p As PropertyInfo In pinfo
    Dim typeString As String = p.PropertyType.Name.ToString
    If typeString = "Int32[]" Then 'if property is not an array
        sb.Append(p.GetValue(myobject)).Append(",")
    Else ' if property is an array
        For x As Integer = 0 To p.GetIndexParameters.Length - 1
            sb.Append(p.GetValue(myobject, New Object() {x})).Append(",") 'append each value from array to the stringbuilder in .CSV format
        Next
    End If
Next

, ?


, [. ]

"" , ( ), 100% , , ( , , ), :

If typeString.EndsWith("[]") And typeString <> "String[]" Then

, ...

-1

If - , , 3 Int32():

If p.PropertyType.IsArray Then
  ' Untested loop code for array case
Else
  ' Untested code for scalar (non-array) case
End If

, PropertyType, .

-1
source

Source: https://habr.com/ru/post/1534947/


All Articles