继承和转换错误(通用接口)

时间:2015-05-21 19:09:51

标签: c# generics casting covariance

如何重构我的代码以消除指示点发生的运行时错误?

DataSeries<SimpleDataPoint>需要能够以某种方式转回IDataSeries<IDataPoint>

我尝试过使用两个接口的继承,如下所示:

public class DataSeries<TDataPoint> : IDataSeries<TDataPoint>, IDataSeries<IDataPoint>但收到编译错误:

  

'DataSeries<TDataPoint>'无法同时实现

     

'IDataSeries<TDataPoint>'

     

'IDataSeries<IDataPoint>'   因为他们可能统一某些类型参数替换

使用协方差似乎不是一种选择,因为我不能使接口协变或逆变。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1 {

class Program {

    [STAThread]
    static void Main(string[] args) {

        var source = new object();

        // compiles fine, but ...
        // runtime error here - cannot cast
        var ds = (IDataSeries<IDataPoint>)new DataSeries<SimpleDataPoint>(source);

        Console.ReadKey();
    }
}

public interface IDataPoint {
    int Index { get; set; }
    double Value { get; set; }
    DateTime TimeStampLocal { get; set; }
    IDataPoint Clone();
}

public sealed class SimpleDataPoint : IDataPoint {
    public int Index { get; set; }
    public double Value { get; set; }
    public DateTime TimeStampLocal { get; set; }
    public IDataPoint Clone() {
        return new SimpleDataPoint {
            Index = Index,
            Value = Value,
            TimeStampLocal = TimeStampLocal,
        };
    }
}

public interface IDataSeries<TDataPoint> where TDataPoint : class, IDataPoint {
    object Source { get; }
    int Count { get; }
    double GetValue(int index);
    DateTime GetTimeStampLocal(int index);
    TDataPoint GetDataPoint(int index);
    TDataPoint GetLastDataPoint();
    void Add(TDataPoint dataPoint);
    IDataSeries<TDataPoint> Branch(object source);
}

public class DataSeries<TDataPoint> : IDataSeries<TDataPoint> where TDataPoint : class, IDataPoint {

    readonly List<TDataPoint> _data = new List<TDataPoint>();

    public object Source {
        get;
        private set;
    }

    public DataSeries(object source) {
        Source = source;
    }

    public int Count {
        get { return _data.Count; }
    }
    public TDataPoint GetDataPoint(int index) {
        return _data[index];
    }
    public TDataPoint GetLastDataPoint() {
        return _data[_data.Count - 1];
    }
    public DateTime GetTimeStampLocal(int index) {
        return _data[index].TimeStampLocal;
    }
    public double GetValue(int index) {
        return _data[index].Value;
    }
    public void Add(TDataPoint dataPoint) {
        _data.Add(dataPoint);
    }

    public IDataSeries<TDataPoint> Branch(object source) {
        throw new NotImplementedException();
    }
}
}

3 个答案:

答案 0 :(得分:2)

问题是new DataSeries<SimpleDataPoint> 不是 IDataSeries<IDataPoint>,因为调用IDataSeries<IDataPoint>.Value = new AnotherDataPoint()IDataPoint value = IDataSeries<IDataPointBase>.Value可能会失败。也就是说,运行时不能保证你所做的是类型安全的,所以它抛出一个异常来告诉你。只有当您的接口被标记为协变或逆变时,运行时才能保证这些操作的一个是安全的。它既没有标记,也没有类型安全,所以无法完成。

如果您打算绕过类型安全,可以创建不安全的代理:

public class DataSeries<TDataPoint> : IDataSeries<TDataPoint>
    where TDataPoint : class, IDataPoint
{
    // ...

    public IDataSeries<IDataPoint> GetUnsafeProxy ()
    {
        return new UnsafeProxy(this);
    }

    private class UnsafeProxy : IDataSeries<IDataPoint>
    {
        private readonly DataSeries<TDataPoint> _owner;

        public UnsafeProxy (DataSeries<TDataPoint> owner)
        {
            _owner = owner;
        }

        public object Source
        {
            get { return _owner.Source; }
        }

        public int Count
        {
            get { return _owner.Count; }
        }

        public double GetValue (int index)
        {
            return _owner.GetValue(index);
        }

        public DateTime GetTimeStampLocal (int index)
        {
            return _owner.GetTimeStampLocal(index);
        }

        public IDataPoint GetDataPoint (int index)
        {
            return _owner.GetDataPoint(index);
        }

        public IDataPoint GetLastDataPoint ()
        {
            return _owner.GetLastDataPoint();
        }

        public void Add (IDataPoint dataPoint)
        {
            _owner.Add((TDataPoint)dataPoint);
        }

        public IDataSeries<IDataPoint> Branch (object source)
        {
            return (IDataSeries<IDataPoint>)_owner.Branch(source);
        }
    }

您可以像这样使用此代理:

IDataSeries<IDataPoint> ds = new DataSeries<SimpleDataPoint>(source).GetUnsafeProxy();

请注意,最后两个方法使用类型转换,因此调用它们并不安全,它们可以抛出不兼容的类型。如果想要将DataSeries不仅转换为基类型,而且也转换为其他类型,则必须向不安全的代理添加更多类型转换,并失去更多的类型安全性。选择是你的。

答案 1 :(得分:2)

所以我的问题让我想到了代码味道,以及类似的东西,以及&#34;我真正想要实现的目标是什么?&#34;

嗯,这就是我想要实现的目标:我想将DataSeries<TDataPoint>转换为IReadOnlyDataSeries<IDataPoint>仅当我将其作为输入传递给处理读取的类时仅来自IReadonlyDataSeries<IDataPoint>对象的数据。

以下是重要的变化:

// here's the covariant, read-only part of the interface declaration
public interface IReadOnlyDataSeries<out TDataPoint> where TDataPoint : class, IDataPoint {
    object Source { get; }
    int Count { get; }
    double GetValue(int index);
    DateTime GetTimeStampLocal(int index);
    TDataPoint GetDataPoint(int index);
    TDataPoint GetLastDataPoint();
}

// add a few bits to the read-write fully-typed interface, breaking covariance,
// but being able to implicitly cast to the covariant readonly version when needed
public interface IDataSeries<TDataPoint> : IReadOnlyDataSeries<TDataPoint> where TDataPoint : class, IDataPoint {
    void Add(TDataPoint dataPoint);
    IDataSeries<TDataPoint> Branch(object source);
}

以下是修订后代码的完整版本:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1 {

class Program {

    [STAThread]
    static void Main(string[] args) {

        var source = new object();

        // implicit conversion works great!!
        // therefore I can achieve the goal of passing the fully-typed read-write dataseries
        // into objects that just want simple read-only data
        var inputSeries = new DataSeries<SimpleDataPoint>(source);

        // passing inputSeries into the constructor involves an implicit
        // conversion to IReadOnlyDataSeries<IDataPoint>
        var processor = new DataProcessor(inputSeries);

        Console.ReadKey();
    }

    public class DataProcessor {

        IReadOnlyDataSeries<IDataPoint> InputSeries;
        DataSeries<SimpleDataPoint> OutputSeries;

        public DataProcessor(IReadOnlyDataSeries<IDataPoint> inputSeries) {
            InputSeries = inputSeries;
            OutputSeries = new DataSeries<SimpleDataPoint>(this);
        }
    }
}

public interface IDataPoint {
    int Index { get; set; }
    double Value { get; set; }
    DateTime TimeStampLocal { get; set; }
    IDataPoint Clone();
}

public sealed class SimpleDataPoint : IDataPoint {
    public int Index { get; set; }
    public double Value { get; set; }
    public DateTime TimeStampLocal { get; set; }
    public IDataPoint Clone() {
        return new SimpleDataPoint {
            Index = Index,
            Value = Value,
            TimeStampLocal = TimeStampLocal,
        };
    }
}

// here's the covariant, read-only part of the interface declaration
public interface IReadOnlyDataSeries<out TDataPoint> where TDataPoint : class, IDataPoint {
    object Source { get; }
    int Count { get; }
    double GetValue(int index);
    DateTime GetTimeStampLocal(int index);
    TDataPoint GetDataPoint(int index);
    TDataPoint GetLastDataPoint();
}

// add a few bits to the read-write fully-typed interface, breaking covariance,
// but being able to implicitly cast to the covariant readonly version when needed
public interface IDataSeries<TDataPoint> : IReadOnlyDataSeries<TDataPoint> where TDataPoint : class, IDataPoint {
    void Add(TDataPoint dataPoint);
    IDataSeries<TDataPoint> Branch(object source);
}

public class DataSeries<TDataPoint> : IDataSeries<TDataPoint> where TDataPoint : class, IDataPoint {

    readonly List<TDataPoint> _data = new List<TDataPoint>();

    public object Source {
        get;
        private set;
    }

    public DataSeries(object source) {
        Source = source;
    }

    public int Count {
        get { return _data.Count; }
    }
    public TDataPoint GetDataPoint(int index) {
        return _data[index];
    }
    public TDataPoint GetLastDataPoint() {
        return _data[_data.Count - 1];
    }
    public DateTime GetTimeStampLocal(int index) {
        return _data[index].TimeStampLocal;
    }
    public double GetValue(int index) {
        return _data[index].Value;
    }
    public void Add(TDataPoint dataPoint) {
        _data.Add(dataPoint);
    }

    public IDataSeries<TDataPoint> Branch(object source) {
        throw new NotImplementedException();
    }

}
}

答案 2 :(得分:-1)

原始代码的这个最小纲要表明,可以通过在TDataPoint接口声明中使IDataSeries协变来修复此问题:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var ds = (IDataSeries<IDataPoint>)new DataSeries<SimpleDataPoint>();

            Console.ReadKey();
        }
    }

    public interface IDataPoint { }

    public sealed class SimpleDataPoint : IDataPoint { }

    public interface IDataSeries<out TDataPoint> where TDataPoint : class, IDataPoint { }

    public class DataSeries<TDataPoint> : IDataSeries<TDataPoint> where TDataPoint : class, IDataPoint { }
}