我正在尝试使用字典来引用委托,但是当我尝试检索委托指针时出现错误。对于更多上下文,我得到了一个用于在 C 结构中查找值的字符串。我已经编写了在 C 结构中获取/设置数据的方法,但现在我需要调用给定字符串的方法。如果您有更好的方法,请告诉我。
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;
}
}
}