title: C# Get an object which is a property and execute a method in the object from a string value
body: <p>I have a class that have objects that are properties. Each of these objects has methods in them. From a string value/ variable I would like to get this property which is an object and execute its method.</p>
  
  <pre><code>var property = this.GetType().GetProperty( "SomePropertyObject" );
  MethodInfo getMethod = property.GetMethod;
  var obj = getMethod.Invoke......
  var runMethod = obj.GetType().GetMethod( "SomeMethod" );
  runMethod.Invoke(obj, new object[] { value1 } );
  </code></pre>
  
  <p>The problem is that when I get the property I only have the get and set methods to work with. If I can get the object via the get method, I can run the objects methods. Please can you tell me if this is possible and how do I go about doing it? </p>
  
  <p>Your help is greatly appreciated.</p>
  
  <p>Edit:</p>
  
  <p>Basically I want to add auditing to my entity framework DbContext class. I have successfully created a single table audit for smaller projects but now I want to create per table auditing using reflection. I am able to create an audit model which is the same as its respective Model with an AuditId and Action (update, add, soft delete, etc). I use reflection to transfer the property values between the Model and the ModelAudit. </p>
  
  <p>I would like to change my current switch statement that adds the ModelAudit to the respective DbSet collection.</p>
  
  <pre><code>private void SaveAudit( DbEntityEntry entry )
      {
          try
          {
              var name = entry.Entity.GetType().Name;
              var space = entry.Entity.GetType().Namespace;
              var type = Type.GetType( string.Format( "{0}.Audits.{1}Audit", space, name ) );
  
              object audit = null;
  
              if ( type != null )
              {
                  audit = Activator.CreateInstance( type );
  
                  var props =
                      entry.Entity.GetType()
                          .GetProperties( BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy );
  
                  audit.GetType().GetProperty( "Action" ).SetValue( audit, entry.State.ToString() );
  
                  foreach ( var prop in props )
                  {
                      if ( prop.CanWrite &amp;&amp; prop.CanRead )
                          switch ( prop.PropertyType.ToString() )
                          {
                              case "System.String":
                              case "System.string":
                              case "System.Int32":
                              case "System.Nullable`1[System.Int32]":
                              case "System.Int64":
                              case "System.bool":
                              case "System.Nullable`1[System.Boolean]":
                              case "System.DateTime":
                              case "System.Nullable`1[System.DateTime]":
                                  audit.GetType().GetProperty( prop.Name ).SetValue( audit, prop.GetValue( entry.Entity ) );
                                  break;
                          }
                  }
  
                  // TODO: Replace with reflection.
                  if ( !entry.State.ToString().Equals( "Unchanged" ) )
                  {
                      switch ( name )
                      {
                          case "Account":
                              AccountAudits.Add( ( AccountAudit ) audit );
  </code></pre>