Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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# 从新语句获取对象_C#_Winforms_Linq_Combobox - Fatal编程技术网

C# 从新语句获取对象

C# 从新语句获取对象,c#,winforms,linq,combobox,C#,Winforms,Linq,Combobox,我正在尝试将所有可移动设备放入一个组合框中 _cbDevice.DataSource = DriveInfo.GetDrives().Where(d => d.IsReady == true && d.DriveType == DriveType.Removable).Select(n => new { obj = n, disp = (n.Name + " " + n.VolumeLabel) }).ToList(); _cbDevice.Displ

我正在尝试将所有可移动设备放入一个组合框中

_cbDevice.DataSource = DriveInfo.GetDrives().Where(d => d.IsReady == true && d.DriveType == DriveType.Removable).Select(n =>        
new { obj = n, disp = (n.Name + " " + n.VolumeLabel) }).ToList();
_cbDevice.DisplayMember = "disp";
_cbDevice.ValueMember = "obj";
现在,当组合框的SelectedIndex被更改时,我想获取DriveInfo对象,它是obj。通过简单地强制转换SelectedValue或SelectedItem,我将得到一个无效的强制转换异常

Additional Informations: The object of Type "
<>f__AnonymousType0`2[System.IO.DriveInfo,System.String]" can not be converted into 
"System.IO.DriveInfo".

希望s.o.能够解决这个问题

这是WebForms、WinForms、Wpf吗?哦,对不起,我正在WinForms中开发,但我不认为这很重要,或者我错了吗?旁注:请避免讨论你的生活有多艰难。从问题中可以明显看出,您已经花了多少时间搜索解决方案,类似于,它没有在帖子中添加详细信息。好的,对不起,我对StackOverflow还很陌生,我只是不想开始我的文字来描述我的问题。。我将在接下来的问题中牢记这一点,谢谢你的代码。发布您是如何强制转换它的,因为DriveInfo\u cbDevice.SelectedValue应该可以工作。