代码之家  ›  专栏  ›  技术社区  ›  user3163495

如何将KeyValuePair<string,object>转换为KeyValuePair<object,object>?

  •  2
  • user3163495  · 技术社区  · 7 年前

    在C#中,我如何才能 键值对<字符串,对象> 键值对<对象,对象>

    public static KeyValuePair<object, object> DoTheCast(KeyValuePair<string, object> the_obj)
    {
        return (KeyValuePair<object, object>)the_obj;
    }
    

    InvalidCastException:无法强制转换类型为的对象 'System.Collections.Generic.KeyValuePair[System.String,System.Object]' 键入

    1 回复  |  直到 7 年前
        1
  •  6
  •   TheGeneral    7 年前

    你不可能 不管怎样,你可以创建一个新的

    public static KeyValuePair<object, object> DoTheCast(KeyValuePair<string, object> the_obj)    
         => new KeyValuePair<object, object>(the_obj.Key, the_obj.Value);