字典<T,Delegate>使用不同类型的委托:更简洁的非字符串方法名称?

Dictionarylt;T,Delegategt; with Delegates of different types: Cleaner, non string method names?(字典lt;T,Delegategt;使用不同类型的委托:更简洁的非字符串方法名称?)
本文介绍了字典<T,Delegate>使用不同类型的委托:更简洁的非字符串方法名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须有更清洁的方法.目前我有:

There has to be a cleaner method. Currently I have:

... Constructor()
{
      parseDictionary = new Dictionary<typeOfStream, Delegate>()
            {
                {typeOfStream.SOME_ENUM_VAL, Delegate.CreateDelegate(typeof(ParseDelegate<string>), this, "MyMethod")},
                {typeOfStream.SOME_OTHER_ENUM_VAL, Delegate.CreateDelegate(typeof(ParseDelegate<XmlNode>), this, "MyOtherMethod")}
            };
}

public bool MyMethod(string some_string)
{
    ...
}

public bool MyOtherMethod(XmlNode some_node)
{
    ...
}

我想摆脱 "MyMethod"MyOtherMethod 并使其成为 this.MyMethodthis.MyOtherMethod.选项?

and I would like to get rid of "MyMethod" and MyOtherMethod and make it this.MyMethod and this.MyOtherMethod. Options?

我对任何允许我使用字典查找并将我的数据 mojo 指向任意方法(以及带有任意一堆参数的已定义方法)进行解析的解决方案持开放态度.

I am open to any solution that allows me to use a Dictionary lookup, and point my data mojo into an arbitrary method (well a defined method with an arbitrary bunch of arguments) for parsing.

推荐答案

只需投射到正确的委托类型:

Just cast to the right kind of delegate:

parseDictionary = new Dictionary<typeOfStream, Delegate>()
{
    { typeOfStream.SOME_ENUM_VAL, (ParseDelegate<string>) MyMethod) },
    { typeOfStream.SOME_OTHER_ENUM_VAL, (ParseDelegate<XmlNode>) MyOtherMethod }
};

这篇关于字典&lt;T,Delegate&gt;使用不同类型的委托:更简洁的非字符串方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)