为什么我不能设置从 ObservableCollection派生的对象?等于作为 ObservableCollection返回的对象吗?
我收到编译错误“无法将类型‘System.Collections.ObjectModel.ObservableCollection 隐式转换为 ProddataRecsObservable’。存在显式转换”请参阅以下代码段中的注释。
//I created a custom class called ProddataRecsObservable derived from
//ObservableCollection<Proddata> so I can do some special CRUD operations for my production
//data records.
public class ProddataRecsObservable : ObservableCollection<Proddata>
{
}
//I have another class which maps an object to a reader and the function MappAll returns an
//Observable collection of type <T>.
public abstract class MapperBase<T>
{
protected abstract T Map(IDataRecord record);
public ObservableCollection<T> Mapall(IDataReader reader)
{
ObservableCollection<T> collection = new ObservableCollection<T>();
while (reader.Read())
{
try
{
collection.Add(Map(reader));
}
catch
{
throw;
}
}
return collection;
}
}
//I have another class derived from MapperBase called ProddataMapper.
public class ProddataMapper : WTS.Data.Mapper.MapperBase<Proddata>
{
protected override Proddata Map(System.Data.IDataRecord record)
{
Proddata p = new Proddata();
p.PSTAT = (DBNull.Value == record["PSTAT"]) ? "" : record["PSTAT"].ToString();
return p;
}
}
//In my calling code, I attempt to set a ProddataRecsObservable variable equal to the
//result of MapAll() from my ProddataMapper class but get the compile error. The error even
//tells me they are the same type which is strange. How can I get around this?
//Calling Code:
ProddataMapper prodMapper = new ProddataMapper();
ProddataRecsObservable recs = prodMapper.Mapall(ProddataReader); //ERROR'S HERE <-
I get a compile error, "Cannot implicitly conver type 'System.Collections.ObjectModel.ObservableCollection to ProddataRecsObservable'. An explicit conversion exists" See comments in following code segments.
//I created a custom class called ProddataRecsObservable derived from
//ObservableCollection<Proddata> so I can do some special CRUD operations for my production
//data records.
public class ProddataRecsObservable : ObservableCollection<Proddata>
{
}
//I have another class which maps an object to a reader and the function MappAll returns an
//Observable collection of type <T>.
public abstract class MapperBase<T>
{
protected abstract T Map(IDataRecord record);
public ObservableCollection<T> Mapall(IDataReader reader)
{
ObservableCollection<T> collection = new ObservableCollection<T>();
while (reader.Read())
{
try
{
collection.Add(Map(reader));
}
catch
{
throw;
}
}
return collection;
}
}
//I have another class derived from MapperBase called ProddataMapper.
public class ProddataMapper : WTS.Data.Mapper.MapperBase<Proddata>
{
protected override Proddata Map(System.Data.IDataRecord record)
{
Proddata p = new Proddata();
p.PSTAT = (DBNull.Value == record["PSTAT"]) ? "" : record["PSTAT"].ToString();
return p;
}
}
//In my calling code, I attempt to set a ProddataRecsObservable variable equal to the
//result of MapAll() from my ProddataMapper class but get the compile error. The error even
//tells me they are the same type which is strange. How can I get around this?
//Calling Code:
ProddataMapper prodMapper = new ProddataMapper();
ProddataRecsObservable recs = prodMapper.Mapall(ProddataReader); //ERROR'S HERE <-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我没有看到它告诉你类型完全相同。
ProddataRecsObservable
与ObservableCollection
不同 - 是什么让您认为它们是同一类型?第一种类型派生自第二种类型 - 这并不使它们成为相同类型。由于继承关系,
ProddataRecsObservable
的每个实例都是ObservableCollection
的实例,但反之则不然。你正在做的事情相当于:
你希望这能起作用吗?如果是这样,你为什么期望它能起作用?编译器如何知道
Foo
实际上会返回Test
的实例(实际上这里不存在 - 你的方法返回派生类的实例)?如果您不期望它能够工作,那么为什么您希望您的示例代码能够工作?I don't see that it tells you the types are the same at all.
ProddataRecsObservable
isn't the same thing asObservableCollection<Proddata>
- what makes you think they're the same type? The first type derives from the second type - that doesn't make them the same type.Every instance of
ProddataRecsObservable
is an instance ofObservableCollection<Proddata>
due to the inheritance relationship, but the reverse isn't true.What's you're doing is the equivalent of:
Would you expect that to work? If so, why would you expect it to work? How is the compiler meant to know that
Foo
will actually return an instance ofTest
(which in fact it doesn't here - nor does your method return an instance of your derived class)? If you wouldn't expect that to work, why would you expect your example code to work?ObservableCollection
无法向上转换为ProddataRecsObservable
,ProddataRecsObservable
中有ObservableCollection
没有的附加逻辑不知道。ObservableCollection<Proddata>
cannot be upcasted toProddataRecsObservable
, there is additional logic inProddataRecsObservable
thatObservableCollection<Prodddata>
doesn't know about.在基类中使 MapAll() 抽象,然后在 ProdDataMapper 中提供重写:
Make MapAll() abstract in the base class, and then in the ProdDataMapper provide the override:
约翰·斯基特是对的。尝试在出现错误的作业中将结果显式转换为 ProddataRecsObservable。
John Skeet is right. Try explicitly casting the result to ProddataRecsObservable in the assignment where you get the error.