title: C# get properties from result in entitysets wit SystemReflection
body: <p>So I have a want to write a piece of code that get's the result out of an EntitySet and puts the data in another object by utilizing SystemReflection.</p>
  
  <p><strong>Example</strong></p>
  
  <p>Object <code>Ticket</code> has a property called <code>Application</code>, this property's value has been defined in a relation table called <code>TicketRelation</code>. The program has to check for the <code>Application</code> property in <code>TicketRelation</code> and if it's there it will pass the data into the <code>Application</code> property of <code>Ticket</code>.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>private object ConvertRelations(object origin, object to)
  {
      // haal relTable op met getValue op relatie property.
      // The propertyInfo types are EntitySets
      List&lt;PropertyInfo&gt; relationProperties = new List&lt;PropertyInfo&gt;();
      PropertyInfo[] cRels = to.GetType().GetProperties();
  
      foreach (var property in origin.GetType().GetProperties())
      {
          // all the possible endings of relation table names
          if (property.Name.EndsWith("Relatie") || property.Name.EndsWith("Rel") || property.Name.EndsWith("Relaties"))
          {
              relationProperties.Add(property);
          }
      }
  
      foreach (var relProp in relationProperties)
      {
          var parent = relProp.GetValue(origin, null);
          var parentProps = parent.GetType().GetProperties();
          object match;
          PropertyInfo[] matchProps = null;
          foreach(var parentProp in parentProps)
          {
              // the property's name of which I assumed to hold the data I want was called Item
              if (parentProp.Name == "Item")
              {
                  match = parentProp.GetValue(parent, null);
                  if(match != null)
                  {
                      matchProps = match.GetType().GetProperties();
                  }
              }
          }
      }
      // this will return the result later on.
      return null;
  }
  </code></pre>
  
  <p>But somehow the GetValue method I try to invoke on parentProp doesn't work. I use this kind of method everytime for GetValue, but when I do it with an <code>EntitySet</code> it throws an exception which I don't understand.
  The stack trace it throws at <code>parentProp.GetValue(parent, null)</code>:</p>
  
  <pre><code>   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
     at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
     at TicketSystemDb.DTO.Helper.DTOHelper.ConvertRelations(Object origin, Object to) in C:\TicketSystem\TicketSystemDb\DTO\Helper\DTOHelper.cs:line 90
     at TicketSystemDb.DTO.Helper.DTOHelper.SimplifyResult(Object from, Object to) in C:\TicketSystem\TicketSystemDb\DTO\Helper\DTOHelper.cs:line 61
     at TicketSystemDb.REPO.TicketREPO.GetTickets() in C:\TicketSystem\TicketSystemDb\REPO\TicketREPO.cs:line 35
     at TicketSystemDb.UOW.TicketUOW.GetTickets() in C:\TicketSystem\TicketSystemDb\UOW\TicketUOW.cs:line 21
     at TicketSystemAPI.Controllers.TicketController.Get() in C:\TicketSystem\TicketSystemAPI\Controllers\TicketController.cs:line 22
     at lambda_method(Closure , Object , Object[] )
     at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.&lt;&gt;c__DisplayClass6_1.&lt;GetExecutor&gt;b__3(Object instance, Object[] methodParameters)
     at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
     at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
  </code></pre>