传递“未知枚举”到一个方法

发布于 2024-09-02 20:59:52 字数 1575 浏览 2 评论 0原文

我目前正在尝试创建一个可以将字符串注册为标识符并将它们与不同类型的枚举相关联的类,这些枚举的评估范围仅限于我确保在使用它时,参数传递给广播(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2024-09-09 20:59:52

看来泛型可能对您想要做的事情有用。然后你可以将这些调用组合成类似的内容

Diagnostics.Broadcast<TestEnum>("logger", TestEnum.Info, null, "Hello World", null);

It looks like Generics might be useful for what you're trying to do. Then you could combine the calls into something like

Diagnostics.Broadcast<TestEnum>("logger", TestEnum.Info, null, "Hello World", null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文