C# 如何创建通用函数<;T>;在字典里
有可能吗C# 如何创建通用函数<;T>;在字典里,c#,.net,generics,C#,.net,Generics,有可能吗 public class MyClass : Dictionary<string, Func<string>> 公共类MyClass:字典 作为 公共类MyClass:字典 事实上我有 public class MyClass : Dictionary<string, Func<string>> { public void MyFunction(string key) { if (this.Conta
public class MyClass : Dictionary<string, Func<string>>
公共类MyClass:字典
作为
公共类MyClass:字典
事实上我有
public class MyClass : Dictionary<string, Func<string>>
{
public void MyFunction(string key)
{
if (this.ContainsKey(key))
this[key]();
else
this["no match"]();
}
}
公共类MyClass:字典
{
公共函数(字符串键)
{
如果(本文件包含密钥))
这个[钥匙]();
其他的
这[“不匹配”]();
}
}
我希望将该值设置为通用值。可能吗
谢谢。必须在某个地方指定类型,因此您还必须将类设置为泛型:
public class Myclass<T> : Dictionary<string, Func<T>> {
public T MyFunction(string key) {
if (this.ContainsKey(key)) {
return this[key]();
} else {
return this["no match"]();
}
}
}
公共类Myclass:字典{
公共myt函数(字符串键){
如果(本文件包含密钥)){
返回此[键]();
}否则{
返回此[“不匹配”]();
}
}
}
< /代码> 您可以将该类型指定为对象实例化的PAR。我提供了工作代码
using System;
using System.Collections.Generic;
namespace GenericDictionary
{
class Program
{
static void Main(string[] args)
{
DictionaryUser dictionaryUser = new DictionaryUser();
Console.ReadLine();
}
class GenericFuncDictionary<T> : Dictionary<string, Func<T>>
{
public void DisplayValues()
{
foreach(Func<T> fun in this.Values)
Console.WriteLine(fun());
}
}
class DictionaryUser
{
public DictionaryUser()
{
GenericFuncDictionary<string> myDictionary = new GenericFuncDictionary<string>();
myDictionary.Add("World", FunWorld);
myDictionary.Add("Universe", FunUniverse);
myDictionary.DisplayValues();
}
public string FunWorld()
{
return "Hello World";
}
public string FunUniverse()
{
return "Hello Universe";
}
}
}
}
使用系统;
使用System.Collections.Generic;
命名空间通用字典
{
班级计划
{
静态void Main(字符串[]参数)
{
DictionaryUser DictionaryUser=新DictionaryUser();
Console.ReadLine();
}
类GenericFuncDictionary:字典
{
public void DisplayValues()
{
foreach(Func fun在此.Values中)
Console.WriteLine(fun());
}
}
类词典
{
公共词典Yuser()
{
GenericFuncDictionary myDictionary=新建GenericFuncDictionary();
添加(“世界”,FunWorld);
添加(“宇宙”,FunUniverse);
myDictionary.DisplayValues();
}
公共字符串FunWorld()
{
返回“你好世界”;
}
公共字符串funuverse()
{
返回“你好,宇宙”;
}
}
}
}
不完全清楚您是否希望Func
的类型参数像其他答案一样成为MyClass
签名的一部分(例如MyType
),或者您是否希望能够在字典中存储任何类型的Func
,并在运行时找出正确的方法
如果您希望使用后一种情况,并使用C#4.0,则可以执行以下操作:
class MyClass : Dictionary<string, Func<object>>
{
public void MyFunction<T>(string key)
{
Func<object> func;
if (this.TryGetValue(key, out func) && func is Func<T>)
func();
else
{
func = this["no match"];
if (func is Func<T>)
func();
else
{ */ do something else, or maybe you don't care about the type of "no match" */ }
}
}
}
要使用它,你可以写:
MyClass myClass = ...;
myClass.MyFunction<string>("test");
MyClass MyClass=。。。;
myClass.MyFunction(“测试”);
以下场景允许您使用元素字典作为输入参数发送,并获得与输出参数相同的参数
首先在顶部添加以下行:
using TFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IDictionary<string, object>>;
myClass["test"] = () => stringFunc();
myClass["test"] = () => (object) stringFunc();
MyClass myClass = ...;
myClass.MyFunction<string>("test");
using TFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IDictionary<string, object>>;
private Dictionary<String, TFunc> actions = new Dictionary<String, TFunc>(){
{"getmultipledata", (input) =>
{
//DO WORKING HERE
return null;
}
},
{"runproc", (input) =>
{
//DO WORKING HERE
return null;
}
}
};
var output = actions["runproc"](inputparam);