C# 行为奇怪的扩展方法#
我有Forms.DataVisualization.Charting.Chart的扩展方法。在扩展方法中,我访问chartareas数组并在其中操作chartarea对象。但是,每次运行代码时,我都会收到EntryPointNotFoundExceptionC# 行为奇怪的扩展方法#,c#,extension-methods,entrypointnotfoundexcept,C#,Extension Methods,Entrypointnotfoundexcept,我有Forms.DataVisualization.Charting.Chart的扩展方法。在扩展方法中,我访问chartareas数组并在其中操作chartarea对象。但是,每次运行代码时,我都会收到EntryPointNotFoundException //This file is in another assembly and is being reference. namespace Hetco.Forms { public static class ChartingExten
//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,但在
`c.AxisY.ScrollBar.ButtonColor = Color.FromArgb(105, 96, 81); //in the extension` method.
我可以通过在c.DoSomething()之前调用该代码来避免该异常,但我希望能够使用扩展方法。我用的是c#4.0。我可能忘了做些什么,但我不知道该做什么。建议我调试代码,这真的很有帮助。首先,我做到了。我将断点放在扩展方法中,并查看所有局部变量,以查看是否有任何设置为null的内容。如果我没有调试它,我就不会花这么多精力来编写所有这些内容。您在两个项目中引用的程序集版本相同吗(请在“引用”下查找并调出引用的属性)?此外,第一个项目和第二个项目中的chart的名称空间是否相同?chtMain应该只是c。我不得不缩短代码。代码确实生成,但运行时失败。两个项目都使用System.Windows.Forms.DataVisualization.Charting。两者都在框架4上。我通过右键单击引用节点并选择第一个项目来创建引用。