title: Tips on generating GUI from C# attributes in WinForms
body: <p><strong>Context</strong></p>
  
  <p>I have a <em>very large number of small classes or structures</em>. The implementation details of these classes are not important, but for each of these there will be a few simple built-in data type properties that I want to expose so that they can be edited in a WinForms control. These will be completely different from class to class though. For example:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class SleepAction : IGameAction
  {
      public float Duration { get; set; }
  }
  
  class TeleportCharacterAction : IGameAction
  {
      public string CharacterId { get; set;
      public string DestinationRoomId { get; set; }
      public Vector2 DestinationPosition { get; set; }
  }
  </code></pre>
  
  <p>The problem is that I wanted to have a <em>single</em> WinForms control that is capable of editing <em>all</em> of these object types. There would be a dropdown list of all the class types on top, and when selecting an item from this dropdown list, the interface would change to accomodate the properties of that type, as well as create an instance of that type to store the data.</p>
  
  <p>At first I was considering handcrafting each of these interfaces, possibly using a <code>TabControl</code> object, one tab per class, with the tabs hidden. But then the number of classes grew exponentially, so I'm turning to some other solution, probably using attributes and reflection. I'm just not sure how to get started.</p>
  
  <p>What I have in mind now is something like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class ActionEditorControl : UserControl
  {
      void ChangeEditorMode(Type type)
      {
          // Clear all GUI interface
          // Create object of type Type with default constructor
          // Use Type metadata to generate new GUI interface
          // Databind new interface to object properties
      }
  
      object GetObject()
      {
          // Return current object
      }
  }
  </code></pre>
  
  <p>And on my model objects I could use attributes to add the necessary metadata:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class SleepAction : IGameAction
  {
      [FieldLabel("Duration")]
      [FieldType("NumericSpinner")]
      public float Duration { get; set; }
  }
  
  class TeleportCharacterAction : IGameAction
  {
      [FieldLabel("Character")]
      [FieldType("CharacterList")]
      public string CharacterId { get; set;
  
      [FieldLabel("Room")]
      [FieldType("RoomList")]
      public string DestinationRoomId { get; set; }
  
      [FieldLabel("Position")]
      [FieldType("VectorPicker")]
      public Vector2 DestinationPosition { get; set; }
  }
  </code></pre>
  
  <p>Of course I'd need to teach my control how to interpret these attributes. Now for my actual questions.</p>
  
  <p><strong>Specific Questions</strong></p>
  
  <ol>
  <li>Would this work?</li>
  <li>Is there a better alternative to solving this problem that I'm overlooking?</li>
  <li>How to deal with the placement or layout of the controls in the interface?</li>
  <li>Finally, I've never used custom attributes before. Any good example to get me started?</li>
  </ol>