如何重新构造代码以消除在指定点发生的运行时错误?
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();
    }
}
}

最佳答案

问题是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强制转换为基类型,还要转换为其他类型,则必须将更多类型强制转换添加到不安全的代理中,从而丢失更多的类型安全性。选择权在你。

09-19 06:50