title: C# add const field using attributes
body: <p>We have class 'SomeClass':</p>
  
  <pre><code>namespace Namespace
  {
      class SomeClass
      {
          // something
      }
  }
  </code></pre>
  
  <p>And attribute 'SomeAttribute':</p>
  
  <pre><code>class SomeAttribute : System.Attribute { }
  </code></pre>
  
  <p><strong>Task:</strong> add to all classes market by SomeAttribute 'public const string Type' field. Modified classes must be following:</p>
  
  <pre><code>class SomeClass
  {
      // something
      public const string Type = @"Namespace.SomeClass";
  }
  </code></pre>
  
  <p><strong>UPD:</strong></p>
  
  <p>I'm using following approach for message transaction:</p>
  
  <pre><code>class Manager
  {
      // message has 3 parts:
      // string message = String.Format("{0}{1}{2}",
      //     typeof(SomeClass).ToString(),
      //     splitter,
      //     Manager.Serialize(someClassObj)
      // )
      public static string GetType(string message) { /* some code */ }
      public static string Serialize(SomeClass message) { /* XML serialization */ }
      public static SomeClass Deserialize(string message) { /* deserialization */ }
  }
  
  class Logic 
  {
      public void ProcessMessage(string message)
      {
          switch (Manager.GetType(message))
          {
              case SomeClass.Type:
                  {
                      SomeClass msg = Manager.Deserialize(message) as SomeClass;
                      // send message to binded objects
                  }
                  break;
              case ClassInheritedFromSomeClass.Type:
                  {
                      // the same
                  }
                  break;
              // etc.
          }
      }
  }
  </code></pre>
  
  <p><strong>UPD 2:</strong></p>
  
  <p>More about messages. At this time I'm using next approach:</p>
  
  <pre><code>public class BaseMessage
  {
      public const string Type = @"Messages.BaseMessage";
  }
  
  public class LoginMessage : BaseMessage
  {
      public new const string Type = @"Messages.Client.LoginMessage";
  
      public string Nickname { get; set; }
      public string Password { get; set; }
  }
  </code></pre>
  
  <p><strong>Conclusion</strong></p>
  
  <p>I think best case is to modify Manger like this:</p>
  
  <pre><code>class Manager
  {
      // create event table
  
      public Action&lt;BaseMessage&gt; this[string eventName]
      {
          get 
          {
              if (!m_eventTable.ContainsKey(eventName))
              {
                  m_eventTable.Add(eventName, new Action&lt;BaseMessage&gt;(message =&gt; { }));
              }
              return m_eventTable[eventName]; 
          }
          set
          {
              m_eventTable[eventName] = value;
          }
      }
  
      public void Send(BaseMessage message, string messageName)
      {
          if (m_eventTable.ContainsKey(messageName) &amp;&amp; this[messageName].Method != null)
          {
              this[messageName].Invoke(message);
          }
      }
  
      private Dictionary&lt;string, Action&lt;BaseMessage&gt;&gt; m_eventTable = new Dictionary&lt;string, Action&lt;BaseMessage&gt;&gt;();
  }
  </code></pre>