Basically I\'m trying to make a string to function dictionary in c#, I\'ve seen it done like this:
Dictionary>
<
One possible approach to this problem is to create a wrapper class to your own dictionary that simply stores the function delegates as an object. You can then use generic methods to add a retrieve the function that match your signatures.
The following code example shows a very basic implementation:
class FunctionDictionary
{
///
/// Internal dictionary that will store the function delegates as Object.
///
private Dictionary m_Map = new Dictionary();
///
/// Add method to dictionary for specified key. Encapsulated method has no parameters.
///
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
///
/// Get method for specified key. Encapsulated method has no parameters.
///
public Func Function(string key)
{
return (Func)m_Map[key];
}
///
/// Add method to dictionary for specified key. Encapsulated method has one parameters.
///
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
///
/// Get method for specified key. Encapsulated method has one parameters.
///
public Func Function(string key)
{
return (Func)m_Map[key];
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public void Add(string key, Func function)
{
m_Map.Add(key, function);
}
public Object this[string key]
{
get
{
return m_Map[key];
}
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
public Func Function(string key)
{
return (Func)m_Map[key];
}
}
The following code example demonstrates the usage:
class FunctionMapUsage
{
private FunctionDictionary functions = new FunctionDictionary();
public string FunctionA()
{
return "A";
}
public string FunctionB(int value)
{
return value.ToString();
}
public int FunctionC(string str1, string str2)
{
return str1.Length + str2.Length;
}
public void CreateFunctionMap()
{
functions.Add("A", FunctionA); // Add Function A to map
functions.Add("B", FunctionB); // Add Function B to map
functions.Add("C", FunctionC); // Add Function C to map
}
public void CallFunctions()
{
var functionA = functions.Function("A"); // Get Function A
var functionB = functions.Function("B"); // Get Function B
var functionC = functions.Function("C"); // Get Function C
string resultA = functionA();
string resultB = functionB(123);
int resultC = functionC("parameter 1", "parameter 2");
}
}
The CreateFunctionMap method simply adds new functions to the dictionary. The CallFunctions methods show how to extract and execute functions from the dictionary.