I am trying to use a dictionary to reference a delegate but I get an error when I try and retrieve the delegate pointer. For more context I am being given a string to use to lookup a value in C structure. I have written methods to get/set data in the C structure but I now need to call the methods given a string. If you have a better way then please let me know.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestCode
{
public class tuboFillLinePresets
{
public IDictionary<string, object> DBLookup { get; set; }
public config_data Data;
delegate float MyDel(int chan);
public float ChanGain(int chan)
{
return Data.ChannelGain(chan);
}
public float MagCurr(int chan)
{
return Data.MagCurrent(chan);
}
public tuboFillLinePresets()
{
DBLookup = new Dictionary<string, object>();
MyDel cg = new MyDel(ChanGain);
MyDel mc = new MyDel(MagCurr);
DBLookup.Add("HBGain", cg);
DBLookup.Add("LBGain", cg);
DBLookup.Add("MagCurrent", mc);
}
public LinePresets tuboGetLinePresets(LinePresets LinePresets)
{
foreach (var item in LinePresets.Parameters)
{
String s = item.Key;
MyDel func;
DBLookup.TryGetValue(s, out func); // error here
LinePresets.Parameters[s] = func(3);
}
return LinePresets;
}
}
}