title: Is there a way to dynamically specify property names in a class?
body: <p>VB.NET 2010~Framework 3.5</p>
  
  <p>Is there a way to dynamically specify property names of a class?</p>
  
  <p>Sometimes I need a list created from Prop1 and Prop2
  Other times I need a list created from Prop2 and Prop4 etc.. The target properties are not known ahead of time, they constantly change as the app is running. . .</p>
  
  <pre><code>Option Strict On
  Option Explicit On
  Public Class Form1
  
  Private Class Things
      Public Property Prop1 As String
      Public Property Prop2 As String
      Public Property Prop3 As String
      Public Property Prop4 As String
  End Class
  
  Private Class SubThing
      Public Property P1 As String
      Public Property P2 As String
  End Class
  
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      Dim mainLst As New List(Of Things)
  
      Dim count As Integer
  
      Do Until count = 20
          mainLst.Add(New Things With {.Prop1 = count.ToString, _
                                       .Prop2 = (count + 1).ToString, _
                                       .Prop3 = (count + 2).ToString, _
                                       .Prop4 = (count + 3).ToString})
          count += 1
      Loop
  
      ' Need to dynamically pick properties From mainLst into subLst.
      ' The commented code below wont compile but demonstrates what I'm trying to do
      ' can this be done without looping?
  
      'Dim propNameA As String = "Prop1"  ' Dynamically specify a property name
      'Dim propNameB As String = "Prop4"
      'Dim subLst = From mainItem In mainLst
      '             Select New SubThing() With {.P1 = mainItem.propNameA, .P2 = mainItem.propNameB}
  
  
      ' This code below compiles but lacks the dynamics I need?
      Dim subLst = From mainItem In mainLst
                    Select New SubThing() With {.P1 = mainItem.Prop1, .P2 = mainItem.Prop4}
  End Sub
  </code></pre>