如何获取从 iDictionary 返回的委托?
我试图使用字典来引用委托,但当我尝试检索委托指针时出现错误。为了获得更多上下文,我得到了一个字符串,用于在 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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
DBLookup
字段应该是Dictionary
。这样,在获取值时,返回的类型将是MyDel
,而不是object
。您会收到错误,因为作为
out
参数传递的引用类型必须与参数类型完全匹配。由于out
参数的类型为MyDel
,并且TryGetValue
上的参数为object
(因为这是字典中的值)你会得到一个错误。进行上述更改后,参数的类型将与参数的类型匹配,并且错误消息将消失。一般来说,如果您发现自己声明了一个包含
object
值的字典,请考虑您实际要存储的内容,并看看是否可以使用不同的类型。Your
DBLookup
field should be aDictionary<string, MyDel>
. That way, when getting the value, the returned type will be aMyDel
, rather than anobject
.You get the error because the type of the reference that you pass as an
out
argument must match the type of the parameter exactly. Since theout
argument is of typeMyDel
and the parameter onTryGetValue
isobject
(since that's the type of the value in your dictionary) you get an error. Once you make the above change, the type of the argument will match the type of the parameter, and the error message will disappear.In general, if you find yourself declaring a dictionary that holds
object
values, consider what you'll actually be storing, and see if you can use a different type instead.将您的字典定义为
并相应地更改其余代码......
define your Dictionary as
and change the rest of the code accordingly...
您的字典存储
对象
,而不是MyDel
的实例。因为您完全有可能这样做:编译器不会让您将无法保存字典可以保存的任何值的变量作为
out
传递。您有两个选择:Dictionary
(如果字典确实只存储带有字符串键的MyDel
实例,这是最佳选择)如下所示:
Your dictionary stores
objects
, not instances ofMyDel
. Because it would be entirely possible for you to do this:The compiler won't let you pass a variable as
out
that can't hold any value that the dictionary can hold. You have two choices:Dictionary<string, MyDel>
(this is the best option if the dictionary really only stores instances ofMyDel
with a string key)Like so:
或者,如果您需要像 TryGetValue 中的字典中的值这样的对象类型,请首先使用对象来获取值,然后在将其转换为委托类型后,您一定会被检索到。
Or if you need an object type like a value in your dictionary in TryGetValue first use an object to get a value and after cast it delegate type you are sure be retrieved.