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

C# - How can I cast KeyValuePair<string,object> to KeyValuePair<object,object>?

在 C# 中,如何将 KeyValuePair 转换为 KeyValuePair?我尝试了以下但它不起作用:

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

InvalidCastException: Unable to cast object of type 'System.Collections.Generic.KeyValuePair[System.String,System.Object]' to type 'System.Collections.Generic.KeyValuePair[System.Object,System.Object]'.

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

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