传递“未知枚举”到一个方法
我目前正在尝试创建一个可以将字符串注册为标识符并将它们与不同类型的枚举相关联的类,这些枚举的评估范围仅限于我确保在使用它时,参数传递给广播(messageType ) 是关联枚举类型的实例。
这将像这样工作:
Diagnostics.RegisterIdentifier("logger", typeof(TestEnum));
Diagnostics.Broadcast("logger", TestEnum.Info, null, "Hello World", null);
这是我当前拥有的代码,我需要能够验证 messageTypesEnum 是否包含在 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);
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来泛型可能对您想要做的事情有用。然后你可以将这些调用组合成类似的内容
It looks like Generics might be useful for what you're trying to do. Then you could combine the calls into something like