1

我有 Forms.DataVisualization.Charting.Chart 的扩展方法。在扩展方法中,我正在访问 chartarea 数组并在其中操作 chartarea 对象。然而,每次我运行我的代码时,我都会得到一个 EntryPointNotFoundException。

//This file is in another assembly and is being reference.
namespace Hetco.Forms
{
    public static class ChartingExtensions
    {
        public static void DoSomething( this Chart chart )
    {
            var c = chart.ChartAreas[0];
            c.Position.X = 0;   // Here I get EntryPointNotFoundException here.
            c.AxisY.ScrollBar.ButtonColor = Color.FromArgb(105, 96, 81);
    }
    }
}

//In another module
private System.Windows.Forms.DataVisualization.Charting.Chart c = new System.Windows.Forms.DataVisualization.Charting.Chart();
c.ChartAreas.Add(somechart);
c.DoSomething();

但是,如果我添加以下代码

//In another module
private System.Windows.Forms.DataVisualization.Charting.Chart c = new System.Windows.Forms.DataVisualization.Charting.Chart();
c.ChartAreas.Add(somechart);
var a = c.ChartAreas[0];
a.Position.X = 0;
c.DoSomething();

我没有得到 EntryPointNotFoundException 但我得到 NullReferenceException

    `c.AxisY.ScrollBar.ButtonColor = Color.FromArgb(105, 96, 81); //in the extension` method.

我可以通过在 c.DoSomething() 之前调用该代码来避免该异常,但我希望能够使用扩展方法。我正在使用 c# 4.0。我可能忘了做某事,但我就是不知道是什么。

4

0 回答 0