How to make a generic Func<T> in a dictionary
Is it possible to make
public class MyClass : Dictionary<string, Func<string>>
as
public class MyClass : Dictionary<string, Func<T>>
Actually I have
public class MyClass : Dictionary<string, Func<string>>
{
public void MyFunction(string key)
{
if (this.ContainsKey(key))
this[key]();
else
this["n开发者_如何学JAVAo match"]();
}
}
I want to make the value as generic. is it possible?
Thanks.
The type has to be specified somewhere, so you would have to make your class generic also:
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"]();
}
}
}
You can specify the type as par the object instantiation. I have provided working code.
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";
}
}
}
}
It wasn't entirely clear if you want the type parameter of your Func<T>
to be part of the MyClass
signature (e.g. MyType<T>
) as other answers assumed or if you wanted to be able to store any type of Func<T>
in the dictionary and figure out the right thing at runtime.
If you want the latter case and you use C# 4.0 then you can do this:
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" */ }
}
}
}
The reason for using C# 4.0 with this is that you can now write:
MyClass myClass = ...;
Func<string> stringFunc = ...;
myClass["test"] = stringFunc;
Prior to C# 4.0 it was not possible to cast an Func<string>
to a Func<object>
. In that case you would have to write either of these two lines (can't be bothered to check if the first line would compile prior to 4.0):
myClass["test"] = () => stringFunc();
myClass["test"] = () => (object) stringFunc();
And to use it you could write:
MyClass myClass = ...;
myClass.MyFunction<string>("test");
The following scenario would allow you to use a dictionary of elements to send in as input parameters and get the same as the output parameters.
First add the following line at the top:
using TFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IDictionary<string, object>>;
Then inside your class, define the dictionary as follows:
private Dictionary<String, TFunc> actions = new Dictionary<String, TFunc>(){
{"getmultipledata", (input) =>
{
//DO WORKING HERE
return null;
}
},
{"runproc", (input) =>
{
//DO WORKING HERE
return null;
}
}
};
This would allow you to run these anonymous functions with a syntax similar to this:
var output = actions["runproc"](inputparam);
精彩评论