0

We have class 'SomeClass':

namespace Namespace
{
    class SomeClass
    {
        // something
    }
}

And attribute 'SomeAttribute':

class SomeAttribute : System.Attribute { }

Task: add to all classes market by SomeAttribute 'public const string Type' field. Modified classes must be following:

class SomeClass
{
    // something
    public const string Type = @"Namespace.SomeClass";
}

UPD:

I'm using following approach for message transaction:

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.
        }
    }
}

UPD 2:

More about messages. At this time I'm using next approach:

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; }
}

Conclusion

I think best case is to modify Manger like this:

class Manager
{
    // create event table

    public Action<BaseMessage> this[string eventName]
    {
        get 
        {
            if (!m_eventTable.ContainsKey(eventName))
            {
                m_eventTable.Add(eventName, new Action<BaseMessage>(message => { }));
            }
            return m_eventTable[eventName]; 
        }
        set
        {
            m_eventTable[eventName] = value;
        }
    }

    public void Send(BaseMessage message, string messageName)
    {
        if (m_eventTable.ContainsKey(messageName) && this[messageName].Method != null)
        {
            this[messageName].Invoke(message);
        }
    }

    private Dictionary<string, Action<BaseMessage>> m_eventTable = new Dictionary<string, Action<BaseMessage>>();
}
6
  • 1
    why would you need that? typeof(SomeClass) is much more versatile...? For example typeof(SomeClass).FullName May 3, 2012 at 13:26
  • And if the value of the constant is different from typeof(SomeClass).FullName: Why not put the value directly in the attribute?
    – dtb
    May 3, 2012 at 13:28
  • Attributes are Reflection stuff. What you need might be more like a property in a base class returning GetType().FullName.
    – fero
    May 3, 2012 at 13:29
  • I know about that case, but I use 'Type' in switch statement that required const values.
    – outoftime
    May 3, 2012 at 13:49
  • In that case, I'd suggest that you should replace the switch with if/else statements.
    – Tim S.
    May 3, 2012 at 13:53

1 Answer 1

0

Using switch with GetType is the wrong way to implement polymorphism, because it only checks the most-derived class (breaks extensibility).

In your particular case, where you want the Manager to be responsible for the behavior, you might use the dynamic keyword and overloaded methods. But this will again violate SOLID, because it isn't open for extension.

Instead of violating SOLID this way, try to find a way to use virtual methods to perform the type-specific action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.