dimanche 4 mai 2014

VB.net - vérifier si la propriété est un tableau - Stack Overflow


I want to check if a property from a class is an array (only concerned about numerical arrays here, NOT character arrays [i.e. strings]). I then want to iterate through the array (i.e. 'do something' with each element). See my attempt below. Thanks!!


edit:
So, a little more info... neither IsArray nor my method shown has worked thus far to check for an array. MSDN suggestions "typeof(Array).IsAssignableFrom(type)", but I wasn't sure how to make that work with the property info here. But maybe someone else knows how to use them and I just didn't use properly.


Within the "Class3" I define an array but to not dimension it. I use "redim" when I instantiate it in another thread and load it up prior to passing it to this function. When I insert a breakpoint in the code here, I can look at "myobject" and see the array elements and values, but really I'm looking to cleanly use the propertyinfo type to generalize this method. I also need to be able to index into the array once I've determined that it is an array...again using propertyinfo, 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

End Class



I want to check if a property from a class is an array (only concerned about numerical arrays here, NOT character arrays [i.e. strings]). I then want to iterate through the array (i.e. 'do something' with each element). See my attempt below. Thanks!!


edit:
So, a little more info... neither IsArray nor my method shown has worked thus far to check for an array. MSDN suggestions "typeof(Array).IsAssignableFrom(type)", but I wasn't sure how to make that work with the property info here. But maybe someone else knows how to use them and I just didn't use properly.


Within the "Class3" I define an array but to not dimension it. I use "redim" when I instantiate it in another thread and load it up prior to passing it to this function. When I insert a breakpoint in the code here, I can look at "myobject" and see the array elements and values, but really I'm looking to cleanly use the propertyinfo type to generalize this method. I also need to be able to index into the array once I've determined that it is an array...again using propertyinfo, 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

End Class


0 commentaires:

Enregistrer un commentaire