简体   繁体   中英

How is performance going to be if I iterate through an object to get all the properties?

For example, let's say I have a simple class and I created an object for said that...

Public Class StackOverflow
    Public Property Questions As String
    Public Property Answers As String
    Public Property Accepted As Integer
    Public Property Boohoo As Boolean
End Class

Dim Noobie As New StackOverflow With {
    .Questions = "How do I  ?",
    .Answers = "Like This",
    .Accepted = 1,
    .Boohoo = True}

Let's say I have 1000 labels, each label contains a StackOverflow with its own content. When I mouse over the label, I want to show each of those properties in a popup. To be able to do this, from my search results of the answers on StackOverflow, it seems I have to use Reflection. And according to the other developers on here, using reflection is slow and I should only use it if I have to.

Is there a better way of iterating through the object to get all the information so I can display it, depending on the label that is mouse over?

EDIT: Adding some more details to my post. I am creating a custom map and I am plotting points onto that map. When I create a point, I inherit the class so it can contain some more information. For example...

Public Class PinPoint
    Public Property X as Double
    Public Property Y as Double
    Public Property ExtraInfo1 as String
    Public Property ExtraInfo2 as String
End Class

And when I create a new point for my map, I would do something like :

Dim Pin As New PinPoint With {.X = Xcoord, .Y = Ycoord, .ExtraInfo1 = "Info1", .ExtraInfo2 = "Info2"}

And when I mouse over those points...

Public Sub PinMouseOver()
Dim rowx As Label
Dim coly As Label

'Create a new Row and Col for the title
TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.AutoSize))
TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
TableLayoutPanel1.RowCount += 1
TableLayoutPanel1.ColumnCount += 1
rowx = New Label With {.Text = "Title: "} : coly = New Label With {.Text = Pin.Title}
TableLayoutPanel1.Controls.Add(rowx, 0, TableLayoutPanel1.RowCount - 1)
TableLayoutPanel1.Controls.Add(coly, 1, TableLayoutPanel1.ColumnCount - 1)

'And then do the same for all the other properties.
    End Sub

I have something which does almost this

<Runtime.CompilerServices.Extension>
Public Function AllPropertiesString(instance As Object) As String
    Try
        If instance Is Nothing Then Return ""
        Return String.Join(Environment.NewLine,
                           instance.GetType().
                           GetProperties().
                           Select(Function(pi) $"{pi.Name}{vbTab}{pi.GetValue(instance)}"))
    Catch
        Return ""
    End Try
End Function

usage

Dim Noobie As New StackOverflow With {
    .Questions = "How do I  ?",
    .Answers = "Like This",
    .Accepted = 1,
    .Boohoo = True}

Dim result = Noobie.AllPropertiesString()

Console.WriteLine(result)

output

Questions How do I ?
Answers Like This
Accepted 1
Boohoo True

and you can just format the returned string how you like

Based on your comment, you can return a Dictionary(Of String, Object) and manipulate the names and values how you wish.

<Runtime.CompilerServices.Extension>
Public Function AllPropertiesDictionary(instance As Object) As Dictionary(Of String, Object)
    Try
        If instance Is Nothing Then Return Nothing
        Return instance.GetType().GetProperties().ToDictionary(Function(pi) pi.Name, Function(pi) pi.GetValue(instance))
    Catch
        Return Nothing
    End Try
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM