创建未知类型的实例

时间:2015-11-05 14:35:43

标签: c# generics activator

我有类绘制三种类型的图表,我想通过函数public void GetData(PlotModel PlotModel)更新它。主要问题是每个系列(AreaSeries,CandleStickSeries,HighLowSeries)都有不同的接口。如何更新函数public void GetData(PlotModel PlotModel)中的不同类型。我该怎么用Activator?通用

我认为这样的想法是个坏主意:

public void GetData(PlotModel PlotModel) {
    while(true) {
        System.Threading.Thread.Sleep(1000);
        // Add new Item?
        switch(PlotModel.Series.First().ToString()) {
            case "OxyPlot.Series.AreaSeries":
                Console.WriteLine("AreaSeries");
                break;
            case "OxyPlot.Series.CandleStickSeries":
                Console.WriteLine("CandleStickSeries");
                break;
            case "OxyPlot.Series.HighLowSeries":
                Console.WriteLine("HighLowSeries");
                break;
        }
    }
}

代码:

 namespace WpfApplication20 {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            DataContext = new PlotClass();
        }
    }
    public class PlotClass {
        public PlotModel PlotModel { get; set; }
        public PlotClass() {
            PlotModel = new PlotModel();
            DrawCandleChart(PlotModel);
            UpdateChartAsync(PlotModel);
        }
        public void DrawSimpleChart(PlotModel PlotModel) {
            Random rnd = new Random();
            LineSeries LS = new LineSeries();
            for (int i=0;i<10;i++) {
                LS.Points.Add(new DataPoint(i,rnd.NextDouble()));
            }
            PlotModel.Series.Add(LS);
            PlotModel.InvalidatePlot(false);
        }

        public void DrawCandleChart(PlotModel PlotModel) {
            Random rnd = new Random();
            CandleStickSeries CSS = new CandleStickSeries();
            for (int i=0;i<10;i++) {
                CSS.Items.Add(new HighLowItem { Close = rnd.NextDouble(), High = rnd.NextDouble(), Low = rnd.NextDouble(), Open = rnd.NextDouble(), X = i });
            }
            PlotModel.Series.Add(CSS);
            PlotModel.InvalidatePlot(false);
        }

        public void DrawHighLowChart(PlotModel PlotModel) {
            Random rnd = new Random();
            HighLowSeries HLS = new HighLowSeries();
            for (int i = 0; i < 10; i++) {
                HLS.Items.Add(new HighLowItem { Close = rnd.NextDouble(), High = rnd.NextDouble(), Low = rnd.NextDouble(), Open = rnd.NextDouble(), X = i });
            }
            PlotModel.Series.Add(HLS);
            PlotModel.InvalidatePlot(false);
        }

        public void UpdateChartAsync(PlotModel PlotModel) {
            Action<PlotModel> Update = new Action<PlotModel>(GetData);
            IAsyncResult result = Update.BeginInvoke(PlotModel, null, null);
        }

        public void GetData(PlotModel PlotModel) {
            while(true) {
                System.Threading.Thread.Sleep(1000);
                // Add new Item?
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

C#4及以上提供了处理这种情况的好方法:使用强制转换为dynamic,并调用每个子类型有一个重载的方法,如下所示:

private void Process(AreaSeries arSer) {
    ...
}
private void Process(CandleStickSeries csSer) {
    ...
}
private void Process(HighLowSeries hlSer) {
    ...
}
...
while(true) {
    System.Threading.Thread.Sleep(1000);
    Process((dynamic)PlotModel.Series.First());
    //      ^^^^^^^^^
}

dynamic的演员表演了#34;魔法&#34;发生:C#将检查PlotModel.Series.First()的运行时类型,并调度到您提供的三种Process方法之一。

这种方法存在一个危险:如果 匹配任何重载都发生PlotModel.Series.First(),则会出现运行时异常。编译器无法执行静态分析,以告诉您调用不会成功。考虑为您的图的公共超类添加一个catch-all方法,以便您可以更优雅地处理意外的子类型。