Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用“将对象强制转换为KeyValuePair”;通用值;?_C#_Casting_Keyvaluepair - Fatal编程技术网

C# 使用“将对象强制转换为KeyValuePair”;通用值;?

C# 使用“将对象强制转换为KeyValuePair”;通用值;?,c#,casting,keyvaluepair,C#,Casting,Keyvaluepair,我有一个组合框,里面有两种不同类型的混合项目。类型为 KeyValuePair<Int32, FontFamily> KeyValuePair 或 KeyValuePair 现在,有时我只对所选项目的键感兴趣,它始终是Int32 访问所选项目的键的最简单方法是什么?我在想类似的事情 Int32 key = ((KeyValuepair<Int32, object/T/var/IdontCare>)combobox.SelectedItem).Key; Int32

我有一个组合框,里面有两种不同类型的混合项目。类型为

KeyValuePair<Int32, FontFamily>
KeyValuePair

KeyValuePair
现在,有时我只对所选项目的键感兴趣,它始终是Int32

访问所选项目的键的最简单方法是什么?我在想类似的事情

Int32 key = ((KeyValuepair<Int32, object/T/var/IdontCare>)combobox.SelectedItem).Key;
Int32 key=((KeyValuepair)组合框。SelectedItem)。key;
但这是行不通的

所以我只有

    Int32 key;
    if(combobox.SelectedItem.GetType().Equals(typeof(KeyValuePair<Int32, FontFamily)))
    {
        key = ((KeyValuePair<Int32, FontFamily)combobox.SelectedItem).Key;
    }
    else if(combobox.SelectedItem.GetType().Equals(typeof(KeyValuePair<Int32, String)))
    {
        key = ((KeyValuePair<Int32, String)combobox.SelectedItem).Key;
    }
Int32键;

如果(combobox.SelectedItem.GetType().Equals)(typeof(KeyValuePair您当然不需要使用
GetType()
。您可以使用:

int key;
var item = combobox.SelectedItem;
if (item is KeyValuePair<int, FontFamily>)
{
    key = ((KeyValuePair<int, FontFamily>) item).Key;
}
else if (item is KeyValuePair<int, string>)
{
    key = ((KeyValuePair<int, string>) item).Key;
}
int键;
var item=combobox.SelectedItem;
如果(项为KeyValuePair)
{
key=((KeyValuePair)项);
}
else if(项为KeyValuePair)
{
key=((KeyValuePair)项);
}

假设您不能将所选项目的类型更改为您自己的等价物,即使用一些非泛型基类型或接口的
KeyValuePair
,我认为没有更好的方法不使用反射或动态类型。

您可以像这样创建自己的类层次结构

public interface IComboBoxItem
{
    public int Key { get; }
}

public class ComboBoxItem<T> : IComboBoxItem
{
    public T Value { get; set; }

    public int Key { get; set; }
}

我猜它是在WPF中绑定的,在这种情况下,我建议不要使用
KeyValuePair
,而是使用自己的VM类

class MyComboItem
{
    private String _stringValue;
    private FontFamiliy _fontFamilyValue;

    public Int32 Key {get;set;}
    public object Value => (_fontFamilyValue!=null)?_fontFamilyValue:_stringValue;
}
或者你可以有一个像

interface IMyComboItem
{
    Int32 Key {get;}
    object Value {get;}
}
并实现两个VM类来实现它,并存储正确的值类型。
使用适当的构造函数等等。使用泛型无法按您想要的方式进行转换,而且您的解决方案案例也不优雅。

转换到
动态
(可怜人的反射)可以做到这一点

var key = (int) ((dynamic) comboxbox.SelectedItem).Key);

使用对象,如
KeyValuePair
您可以使用dynamic:
(int)((dyamic)selectedItem.Key)
Ehsan:以前尝试过,会导致无效的castexception。Lee:成功了,谢谢!(请参阅接受的答案))这是Lee在评论中建议的,但我收到一个编译器错误(CS0656),表示该成员“Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create"缺少。请在项目中包含
Microsoft.CSharp
程序集。帕特里克:谢谢,现在它真的可以工作了。理查德:看起来有点脆弱,但它正是我想要的。老实说,我以前从未使用过dynamic。我将不得不了解它。这看起来很优雅,但因为我从课堂外和洛杉矶获得了KeyValuePairster将放弃KeyValuePairs,我将尝试避免强制转换。这看起来确实很优雅,但由于我从类外获得KeyValuePairs,然后将放弃KeyValuePairs,我将尝试避免强制转换。不是强制转换而是映射,最终您的ComboItem VM类只能作为KeyValuePairs类的包装器来正确处理它
interface IMyComboItem
{
    Int32 Key {get;}
    object Value {get;}
}
var key = (int) ((dynamic) comboxbox.SelectedItem).Key);