Dictionarylt;T,Delegategt; with Delegates of different types: Cleaner, non string method names?(字典lt;T,Delegategt;使用不同类型的委托:更简洁的非字符串方法名称?)
问题描述
必须有更清洁的方法.目前我有:
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.MyMethod 和 this.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 }
};
这篇关于字典<T,Delegate>使用不同类型的委托:更简洁的非字符串方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:字典<T,Delegate>使用不同类型的委托:更简洁的非字符串方法名称?


- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01