开发者

How do I get a delegate returned from a iDictionary?

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;
               MyDe开发者_开发百科l func;
               DBLookup.TryGetValue(s, out func);  // error here 
               LinePresets.Parameters[s] = func(3);
           }
           return LinePresets;
       }
   }
}


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.


define your Dictionary as

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

and change the rest of the code accordingly...


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)
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜