如何获取从 iDictionary 返回的委托?

发布于 2024-11-30 10:05:57 字数 1378 浏览 1 评论 0原文

我试图使用字典来引用委托,但当我尝试检索委托指针时出现错误。为了获得更多上下文,我得到了一个字符串,用于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万水千山粽是情ミ 2024-12-07 10:05:57

您的 DBLookup 字段应该是 Dictionary。这样,在获取值时,返回的类型将是 MyDel,而不是 object

您会收到错误,因为作为 out 参数传递的引用类型必须与参数类型完全匹配。由于 out 参数的类型为 MyDel,并且 TryGetValue 上的参数为 object(因为这是字典中的值)你会得到一个错误。进行上述更改后,参数的类型将与参数的类型匹配,并且错误消息将消失。

一般来说,如果您发现自己声明了一个包含 object 值的字典,请考虑您实际要存储的内容,并看看是否可以使用不同的类型。

Your DBLookup field should be a Dictionary<string, MyDel>. That way, when getting the value, the returned type will be a MyDel, rather than an object.

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 the out argument is of type MyDel and the parameter on TryGetValue is object (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.

习惯成性 2024-12-07 10:05:57

将您的字典定义为

public IDictionary<string, MyDel> DBLookup { get;  set; }

并相应地更改其余代码......

define your Dictionary as

public IDictionary<string, MyDel> DBLookup { get;  set; }

and change the rest of the code accordingly...

夏尔 2024-12-07 10:05:57

您的字典存储对象,而不是MyDel的实例。因为您完全有可能这样做:

DBLookup.Add("foo", "HAH!");

编译器不会让您将无法保存字典可以保存的任何值的变量作为 out 传递。您有两个选择:

  • 将字典更改为 Dictionary (如果字典确实只存储带有字符串键的 MyDel 实例,这是最佳选择)
  • 使用中间变量进行检索,

如下所示:

object value;

if(DBLookup.TryGetValue(s, out value) && value is MyDel)
{
    func = (MyDel)value;
    LingPresents.Parameters[s] = func(3)
}

Your dictionary stores objects, not instances of MyDel. Because it would be entirely possible for you to do this:

DBLookup.Add("foo", "HAH!");

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:

  • Change your dictionary to a Dictionary<string, MyDel> (this is the best option if the dictionary really only stores instances of MyDel with a string key)
  • Use an intermediate variable for retrieval

Like so:

object value;

if(DBLookup.TryGetValue(s, out value) && value is MyDel)
{
    func = (MyDel)value;
    LingPresents.Parameters[s] = func(3)
}
过期以后 2024-12-07 10:05:57

或者,如果您需要像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文