title: How do you deal with object type switching?
body: <p>In C# from time to time the issue creeps out. Usually I get an object value and then I have to call &quot;real&quot; function for it. Something like this:</p>
  <pre><code>if (type==typeof(byte))
    val = rs.GetByte(col);
  else if (type==typeof(int))
    val = rs.GetInt32(col);
  ...
  </code></pre>
  <p>or</p>
  <pre><code>if (type==typeof(byte))
    Call((byte)val);
  else if (type==typeof(int))
    Call((int)val);
  ...
  </code></pre>
  <p>I can see a textual pattern here, but I didn't come up with solution which would deal once for good with all the dirty work.</p>
  <p><strong>How do you deal it?</strong> You receive the object value and you have to either set it or pass it but not as an object but concrete type (PODs, Nullable of PODs and string) and...?</p>
  <h2>Edits</h2>
  <p>Complete <strong>example</strong> (this one is the cause of the question):</p>
  <pre><code>    protected IRecord ReadRecord()
      {
          if (!ResultSet.Read())
              return null;
  
          IRecord record = CreateIRecord();
  
          var type = record.GetType();
          int column = -1;
  
          foreach (var prop in type.GetProperties())
          {
              ++column;
              object val;
  
              if (prop.PropertyType == typeof(byte))
                  val = ResultSet.GetByte(column);
              else if (prop.PropertyType == typeof(byte?))
                  val = ResultSet.GetSqlByte(column).ToNullable();
              else if (prop.PropertyType == typeof(int))
                  val = ResultSet.GetInt32(column);
              else if (prop.PropertyType == typeof(int?))
                  val = ResultSet.GetSqlInt32(column).ToNullable();
              else if (prop.PropertyType == typeof(double))
                  val = ResultSet.GetDouble(column);
              else if (prop.PropertyType == typeof(double?))
                  val = ResultSet.GetSqlDouble(column).ToNullable();
              else if (prop.PropertyType == typeof(DateTime))
                  val = ResultSet.GetDateTime(column);
              else if (prop.PropertyType == typeof(DateTime?))
                  val = ResultSet.GetSqlDateTime(column).ToNullable();
              else if (prop.PropertyType == typeof(string))
                  val = ResultSet.GetString(column);
              else
                  throw new ArgumentException(&quot;Invalid property type {0}&quot;.Expand(prop.PropertyType.ToString()));
  
              prop.SetValue(record, val, null);
          }
  
          return record;
  
  
      }
  </code></pre>