title: Adding Tags and its respective value to an object
body: <p>Let's assume we have a class called "Tag".</p>
  
  <pre><code>public class Tag
  {
      public string Name { get; private set; }
      public string Description { get; private set; }
      public Type Type { get; private set; }
  
      public Tag(string name, string description, Type type)
      {
          this.Name = name;
          this.Description = description;
          this.Type = type;
      }
  }
  </code></pre>
  
  <p>An example of an instance of a Tag would be:</p>
  
  <pre><code>Tag weightTag = new Tag("Weight", "Weight of item in kg", typeof(double))
  </code></pre>
  
  <p>Next assume that I have another class which has a collection of these Tags which a value relevant to that object mapped to each individual tag in the collection.</p>
  
  <pre><code>public class Item
  {
      public string Id { get; private set; }
      public string Name { get; private set; }
      public string Description { get; private set; }
  
      public Dictionary&lt;Tag, object&gt; Tags { get; private set; }
  
      public Item(string name, string description)
      {
          this.Name = name;
          this.Description = description;
  
          this.Tags = new Dictionary&lt;Tag, object&gt;();
      }
  
      public void AddTag(Tag tag, object value)
      {
          Tags.Add(tag, value);
      }
  }
  </code></pre>
  
  <p>I like this method due to the fact that you can take some predefined tags from some where and then just assign a value.</p>
  
  <pre><code>Tag buildDateTag = TagProvider.GetByName("BuildDate");
  Tag weightTag = TagProvider.GetByName("Weight");
  
  item.AddTag(buildDateTag, new DateTime(2013,12,06);
  item.AddTag(weightTag, 10.5);
  </code></pre>
  
  <p>The downside to this method is that there will be a lot of boxing/unboxing.  For instance, if I have a collection of Item and I want to sum all the values of the tag corresponding to weight I'd have to do something like:</p>
  
  <pre><code>  double totalWeight = 0d;
    foreach (var item in items)
    {
        if (Tags.ContainsKey(weightTag))
        {
            totalWeight += (double)Tags[weightTag];
        }
    }
  </code></pre>
  
  <p>In this instance I happened to know that I'm going to cast to double so the only performance penalty was the unboxing.  However there may be instances where I will need to get the type of the object from the Type member in the Tag object in order to figure out what the type of the value object is.</p>
  
  <p>My question is do you think there is a better way to architect this in order to get rid of the need to rely on using objects and unboxing everything every time I want to use a value?</p>
  
  <p>And although this should probably be a different question, in this usage should Tag be a struct rather than a class?</p>