lundi 27 mars 2017

How can I reflect over and get the value from this inner class?

A user is passing me a string with a class name and another string with a field from that class that I'm supposed to use to extract some information with. Here are a couple of sample classes; they all inherit from the same base class:

public class PersonRow : Row
{
   public static readonly RowFields Fields = new RowFields.Init();

   public class RowFields : RowFieldBase
   {
      public Int32Field Id;
      public StringField FirstName;
      public StringField LastName;
   }

}


public class AutomobileRow : Row
{
  public static readonly RowFields Fields = new RowFields.Init();

  public class RowFields : RowFieldBase
  {
     public Int32Field Id;
     public StringField PaintColor;
     public StringField EngineSize;
  }
}

The user will give me something like 'PersonRow' and 'FirstName', from which I need to dive into the FirstName field and extract the value of a member in it called Expression.

I'll spare the details of what Int32Field and StringField are doing internally, but suffice it to say both these types have an Expression member which is a string I need to use.

I'm trying to use reflection to do this, and have gotten this far:

var myClassType = Type.GetType("PersonRow");
var myFields = myClassType.GetField("Fields", BindingFlags.Public | BindingFlags.Static);
var fieldListing = myFields.GetValue(null);

...at this point I have an object which is an array of PersonRow.RowFields. However I'm kind of stuck in going past this, in that I'm not sure how to enumerate over that array and extract the Expression value from the field I'm interested in?





Aucun commentaire:

Enregistrer un commentaire