将通用字典转换为不同的类型 [英] Convert Generic Dictionary to different type

查看:107
本文介绍了将通用字典转换为不同的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种快速的方法来将泛型字典从一种类型转换为另一种类型

我有这个

  IDictionary< string,string> _commands; 

并且需要将它传递给一个带有稍微不同类型的字典的函数

  public void Handle(IDictionary< string,Object> _commands); 


解决方案

我想我会写

  Handle(_commands.ToDictionary(p => p.Key,p =>(object)p.Value)); 

不是世界上最有效的事情,但是直到协变为止,这就是休息时间。

Is there a quick way to convert a Generic Dictionary from one type to another

I have this

IDictionary<string, string> _commands;

and need to pass it to a function that takes a slightly different typed Dictionary

public void Handle(IDictionary<string, Object> _commands);

解决方案

I suppose I would write

Handle(_commands.ToDictionary(p => p.Key, p => (object)p.Value));

Not the most efficient thing in the world to do, but until covariance is in, that's the breaks.

这篇关于将通用字典转换为不同的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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