title: Generic property- how to specify the type at run time
body: <p>I was reading a question on <a href="https://stackoverflow.com/questions/271347/making-a-generic-property">making a generic property</a>, but I'm a little confused by the last example from the <a href="https://stackoverflow.com/questions/271347/making-a-generic-property/271356#271356">first answer</a> (I've included the relevant code below):</p>
  
  <blockquote>
    <p>You have to know the type at compile
    time. If you don't know the type at
    compile time then you must be storing
    it in an object, in which case you can
    add the following property to the Foo 
    class:</p>
  </blockquote>
  
  <pre><code>public object ConvertedValue {
      get {
          return Convert.ChangeType(Value, Type);
      }
  }
  </code></pre>
  
  <p>That's seems strange: it's converting the value to the specified type, but it's returning it as an object when the value was stored as an object. Doesn't the returned object still require un-boxing? If it does, then why bother with the conversion of the type? </p>
  
  <p>I'm also trying to make a generic property whose type will be determined at run time:</p>
  
  <pre><code>public class Foo
  {
      object Value {get;set;}
      Type ValType{get;set;}
      Foo(object value, Type type)
      { Value = value; ValType = type; }
  
      // I need a property that is actually
      // returned as the specified value type...
      public object ConvertedValue {
          get {
              return Convert.ChangeType(Value, ValType);
          }
      }
  }
  </code></pre>
  
  <p>Is it possible to make a generic property? Does the return property <em>still</em> require unboxing after it's accessed?</p>
  
  <p>Note: I don't want to make <code>Foo</code> generic because I want <code>Foo</code> to contain values of different types and I want to put various <code>Foo</code>s into a collection. In other words, I want to have a collection that holds different types of objects.</p>