title: PropertyGrid and Dynamic Types of Objects
body: <p>I'm writing a GUI application where I need to enable editing properties of arbitrary objects (their types are only known at run-time).</p>
  
  <p>I've decided to use the PropertyGrid control to enable this functionality. 
  I created the following class:</p>
  
  <pre><code>[TypeConverter(typeof(ExpandableObjectConverter))]
  [DefaultPropertyAttribute("Value")]
  public class Wrapper
  {
      public Wrapper(object val)
      {
          m_Value = val;
      }
  
      private object m_Value;
  
      [NotifyParentPropertyAttribute(true)]
      [TypeConverter(typeof(ExpandableObjectConverter))]
      public object Value
      {
          get { return m_Value; }
          set { m_Value = value; }
      }
  }
  </code></pre>
  
  <p>When I get an instance of an object I need to edit, I create a Wrapper for it and set it as the selected object:</p>
  
  <pre><code>Wrapper wrap = new Wrapper(obj);
  propertyGrid.SelectedObject = wrap;
  </code></pre>
  
  <p>But I've run into the following problem - the above works as expected only when the type of obj is some custom type (i.e a class that I defined by myself, or a built in complex type) but not when obj is a primitive.</p>
  
  <p>For example, if I define:</p>
  
  <pre><code>[TypeConverter(typeof(ExpandableObjectConverter))]
  public class SomeClass
  {
      public SomeClass()
      {
          a = 1;
          b = 2;
      }
  
      public SomeClass(int a, int b)
      {
          this.a = a;
          this.b = b;
      }
  
      private int a;
  
      [NotifyParentPropertyAttribute(true)]
      public int A
      {
          get { return a; }
          set { a = value; }
      }
  
      private int b;
  
      [NotifyParentPropertyAttribute(true)]
      public int B
      {
          get { return b; }
          set { b = value; }
      }
  }
  </code></pre>
  
  <p>And do:</p>
  
  <pre><code>Wrapper wrap = new Wrapper(new SomeClass());
  propertyGrid.SelectedObject = wrap;
  </code></pre>
  
  <p>Then everything works swell. On the other hand, when I perform the following:</p>
  
  <pre><code>int num = 1;
  Wrapper wrap = new Wrapper(num);
  propertyGrid.SelectedObject = wrap;
  </code></pre>
  
  <p>Then I can see the value "1" in the grid  (and it's not grayscaled) but I can't edit the value. I noticed that if I change Wrapper's "Value" property's type to int and remove the TypeConverter attribute, it works. 
  I get the same behavior for other primitive types and strings.</p>
  
  <p>What is the problem?</p>
  
  <p>Thanks in advance!</p>