创建具有值作为Action< T>实例的字典的语法是什么? [英] What is the syntax for creating a Dictionary with values as instances of Action<T>?

查看:243
本文介绍了创建具有值作为Action< T>实例的字典的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个Dictionary对象作为成员字段

key = string

value = 的一个实例Action< T> 其中T可能是不同的每个条目,例如long,int,string(一个ValueType或一个RefType)

然而无法让编译器在这里同意我的看法。要创建一个成员字段,它似乎需要一个固定的T规范。我已经通过了我的奋斗时间限制,然后才意识到'它不应该这么难'。可能是泛型noob错误。请指教..



使用情况类似于这样

  m_Map.Add(Key1,
new delegate(long l){Console.Writeline(Woohoo!{0},l.ToString();));


解决方案

基本上,编译器如何知道你对什么类型的条目感兴趣?

你甚至不能解释 Dictionary< Type, Action< T>> 其中每个字典条目都有一个类型的键和一个使用该类型的操作。泛型只是不支持这种关系。



如果您在尝试使用它时知道这种类型,那么您可以将其设置为字典<字符串,委托> ,并在获取它时转换该值。另外,你也可以使用 Action< object> 并且和拳击一起生活, >请注意,要使用匿名方法或lambda表达式(仅限 Delegate ),您需要强制转换 - 或者编写一个方便的转换方法:

  public static Delegate ConvertAction< T>(Action< T> action)
{
return action;
}

这样你就可以写出:

 委托dlg = ConvertAction((long x)=> Console.WriteLine(Got {0},x)); 

或字典上下文中:

  var dict = new Dictionary< string,Delegate>(); 
dict [Key1] = ConvertAction((long x)=> Console.WriteLine(Got {0},x));

当您再次从字典中提取值时,您仍然需要转换为正确的类型虽然...



不同的方式...



直接使用字典,可以将字典封装在自己的类型中,并且有一个通用的 Add 方法:

  public void Add< T>(string key,Action< t> action)

因此它在后台仍然是一个 Dictionary< string,Delegate> ,但是你的类型将确保它只包含代表一个参数的值。


I need to create a Dictionary object as a member field
key = string
value = an instance of Action<T> where T could be different per entry, e.g. long, int, string (a ValueType or a RefType)

However can't get the compiler to agree with me here.. To create a member field it seems to need a fixed T specification. I've passed my limit of struggle time before awareness that 'It shouldn't be this difficult'

Might be a generics noob mistake. Please enlighten..

The usage would be something like this

m_Map.Add("Key1", 
   new delegate(long l) {Console.Writeline("Woohoo !{0}", l.ToString();));

解决方案

You can't, basically. How would the compiler know what type you were interested in for any particular entry?

You can't even explain a relationship of Dictionary<Type, Action<T>> where each dictionary entry has a key which is a type and an action which uses that type. Generics just doesn't support that relationship.

If you will know the kind when you try to use it, you can just make it a Dictionary<string, Delegate> and cast the value when you fetch it. Alternatively, you could use Action<object> and live with the boxing and cast you'll end up with.

Note that to use anonymous methods or lambda expressions with just Delegate, you'll need to cast - or write a handy conversion method:

public static Delegate ConvertAction<T>(Action<T> action)
{
    return action;
} 

That way you can write:

Delegate dlg = ConvertAction((long x) => Console.WriteLine("Got {0}", x));

or in the dictionary context:

var dict = new Dictionary<string, Delegate>();
dict["Key1"] = ConvertAction((long x) => Console.WriteLine("Got {0}", x));

You'll still need to cast to the right type when you fetch the value out of the dictionary again though...

A different tack...

Instead of exposing a dictionary directly, you could encapsulate the dictionary in your own type, and have a generic Add method:

public void Add<T>(string key, Action<T> action)

So it would still be a Dictionary<string, Delegate> behind the scenes, but your type would make sure that it only contained values which were delegates taking a single argument.

这篇关于创建具有值作为Action&lt; T&gt;实例的字典的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆