我收到一个编译错误,“无法将类型'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 <-