我收到此错误:
“无法将‘System.Windows.Forms.DataGridViewRow’类型的对象转换为‘Win.Models.ResourceModel’类型。”
执行此代码时:
var qResources = this.dataGridViewResources.Rows
.Cast<ResourceModel>()
.AsQueryable();
即使我已经在ResourceModel
:
public static implicit operator ResourceModel(DataGridViewRow row)
{
List<string> columns = new List<string> { "ResourceName", "Path" };
List<string> rColumns = new List<string>();
foreach (DataGridViewColumn c in row.DataGridView.Columns)
{
rColumns.Add(c.Name);
}
if (!columns.All(c => rColumns.Contains(c))) { return null; }
return new ResourceModel
{
name = row.Cells["ResourceName"].Value.AsString(),
path = row.Cells["Path"].Value.AsString()
};
}
现在,我已经用explicit
AND implicit
尝试过了。两者都因相同的错误而失败。有没有办法让Cast<ResourceModel>()
工作?
注意:在强制转换运算符中放置断点时,它永远不会到达那里,所以我可能误解了该Cast<>
方法。