C#-如何将KeyValuePair <string,object>强制转换为KeyValuePair <object,object>?

use*_*495 2 c# casting key-value

在C#中,如何将KeyValuePair <string,object> 强制转换KeyValuePair <object,object>?我尝试了以下操作,但不起作用:

public static KeyValuePair<object, object> DoTheCast(KeyValuePair<string, object> the_obj)
{
    return (KeyValuePair<object, object>)the_obj;
}
Run Code Online (Sandbox Code Playgroud)

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

AAA*_*ddd 6

您将无法投射本质,但是您可以创建一个新的

public static KeyValuePair<object, object> DoTheCast(KeyValuePair<string, object> the_obj)    
     => new KeyValuePair<object, object>(the_obj.Key, the_obj.Value);
Run Code Online (Sandbox Code Playgroud)