passing an "unknown enumeration" to a method
- by firoso
I'm currently trying to make a class that can register strings as identifiers and accociate them with different types of Enumerations, these enumerations are being evaluated only in so much that I am ensuring that when it's used, that the parameter passed to broadcast (messageType) is an instance of the associated Enum Type.
This would work something like this:
Diagnostics.RegisterIdentifier("logger", typeof(TestEnum));
Diagnostics.Broadcast("logger", TestEnum.Info, null, "Hello World", null);
here's the code I currently have, I need to be able to verify that messageTypesEnum is contained in messageTypesFromIdentifier.
private static Dictionary<string, Type> identifierMessageTypeMapping = new Dictionary<string, Type>();
private static List<IListener> listeners = new List<IListener>();
public static void RegisterIdentifier(string identifier, Type messageTypesEnum)
{
if (messageTypesEnum.BaseType.FullName == "System.Enum")
{
identifierMessageTypeMapping.Add(identifier, messageTypesEnum);
}
else
{
throw new ArgumentException("Expected type of messageTypesEnum to derive from System.Enum", "messageTypesEnum");
}
}
public static void Broadcast(string identifier, object messageType, string metaIdentifier, string message, Exception exception)
{
if (identifierMessageTypeMapping.ContainsKey(identifier))
{
Type messageTypesFromIdentifier = identifierMessageTypeMapping[identifier];
foreach (var listener in listeners)
{
DiagnosticsEvent writableEvent = new DiagnosticsEvent(identifier, messageType, metaIdentifier, message, exception);
listener.Write(writableEvent);
}
}
}