为什么我不能设置从 ObservableCollection派生的对象?等于作为 ObservableCollection返回的对象吗?

发布于 2024-08-07 19:46:44 字数 1847 浏览 8 评论 0原文

我收到编译错误“无法将类型‘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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

始终不够爱げ你 2024-08-14 19:46:44

我没有看到它告诉你类型完全相同。 ProddataRecsObservableObservableCollection 不同 - 是什么让您认为它们是同一类型?第一种类型派生自第二种类型 - 这并不使它们成为相同类型。

由于继承关系,ProddataRecsObservable 的每个实例都是 ObservableCollection 的实例,但反之则不然。

你正在做的事情相当于:

class Test : object {}

object Foo()
{
    return new object();
}
...
Test t = Foo();

你希望这能起作用吗?如果是这样,你为什么期望它能起作用?编译器如何知道 Foo 实际上会返回 Test 的实例(实际上这里不存在 - 你的方法返回派生类的实例)?如果您期望它能够工作,那么为什么您希望您的示例代码能够工作?

I don't see that it tells you the types are the same at all. ProddataRecsObservable isn't the same thing as ObservableCollection<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 of ObservableCollection<Proddata> due to the inheritance relationship, but the reverse isn't true.

What's you're doing is the equivalent of:

class Test : object {}

object Foo()
{
    return new object();
}
...
Test t = Foo();

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 of Test (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?

与风相奔跑 2024-08-14 19:46:44

ObservableCollection 无法向上转换为 ProddataRecsObservableProddataRecsObservable 中有 ObservableCollection 没有的附加逻辑不知道。

ObservableCollection<Proddata> cannot be upcasted to ProddataRecsObservable, there is additional logic in ProddataRecsObservable that ObservableCollection<Prodddata> doesn't know about.

善良天后 2024-08-14 19:46:44

在基类中使 MapAll() 抽象,然后在 ProdDataMapper 中提供重写:

public override ProddataRecsObservable Mapall(IDataReader reader)
{
   // do the downcast explicitly
    return (ProddataRecsObservable) base.MapAll(reader);
}

Make MapAll() abstract in the base class, and then in the ProdDataMapper provide the override:

public override ProddataRecsObservable Mapall(IDataReader reader)
{
   // do the downcast explicitly
    return (ProddataRecsObservable) base.MapAll(reader);
}
抚你发端 2024-08-14 19:46:44

约翰·斯基特是对的。尝试在出现错误的作业中将结果显式转换为 ProddataRecsObservable。

John Skeet is right. Try explicitly casting the result to ProddataRecsObservable in the assignment where you get the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文