title: .NET Generics, pull object member value by name
body: <p>I am passing in 2 generic objects and a string into a function and I would like to find the property of the objects that match the string and compare their values.</p>
  
  <p>Here is a sample model:</p>
  
  <pre><code>Public Structure Foo
      Public Bar As String
      Public Nan As String
      Public Tucket As String
  End Structure
  </code></pre>
  
  <p>and the calling class:</p>
  
  <pre><code>&lt;TestMethod()&gt; 
  Public Sub TestEquality_Class_Pass_Identifier()
      _tester = new GeneralAssert
      Dim expectedFoo = New Foo With {.Bar = "asdf", .Nan = "zxcv", .Tucket = "qwer"}
      Dim actualFoo = New Foo With {.Bar = "qwer", .Nan = "zxcv", .Tucket = "qwer"}
      _tester.TestEquality(expectedFoo, actualFoo, "Bar")
  End Sub
  </code></pre>
  
  <p>The Generic Assert class that will be doing the business logic</p>
  
  <pre><code>Public Class GenericAssert
      Public Sub TestEquality(Of t)(expected As t, actual As t, Optional identifier As String = Nothing)
          Dim expectedType = expected.GetType(), actualType = actual.GetType()
          Assert.AreEqual(expectedType, actualType)
  
          If (Not identifier Is Nothing) Then
              Dim expectedProperty = expectedType.GetMember(identifier).FirstOrDefault()
              Dim actualProperty = actualType.GetMember(identifier).FirstOrDefault()
  
              TestEquality(*WhatHere1*, *WhatHere2*)
          End If
      End Sub
  End Class
  </code></pre>
  
  <p>The expected outcome will be that the Bar member of <code>expectedFoo</code> ("asdf") and <code>actualFoo</code> ("qwer") would be compared. I cannot find any value in FieldInfo, PropertyInfo or MemberInfo that will allow me to get the value assigned to that property by name.</p>