我正在使用 .NET 为 AutoCAD 创建类库。
问题是这些方法从 AutoCAD 中一个接一个地被调用,第一个方法读取输入文件并在内存中创建数据列表。但是,当调用新列表时,列表是空的。
我需要找到一个解决方案来保存这些数据。 该列表包含我创建的结构中的数据。方法独立调用,但按顺序调用。
短代码示例:
namespace GeoPjuvis
{
...
public class Program
{
...
//program variables
private List<GeoData> dataList;
private List<DataPoint> points;
private int mapScale;
public Program()
{
dataList = new List<GeoData>();
points = new List<DataPoint>();
}
//Initialization method of the program. Makes praperations. Reads files. Add points to map.
[CommandMethod("geoinit", CommandFlags.Session)]
public void Init()
{
...
}
//method uses data gathered before and selects points
[CommandMethod("selectPoints", CommandFlags.Session)]
public void SelectPoints()
{
...
}...
那么为什么当我调用 SelectPoints() 方法时这些数据列表和点列表是空的。以及如何避免这种情况?
最佳答案
我不知道 AutoCAD 的编程,但我怀疑它每次都在创建一个新实例。您可以尝试将变量设为静态(例如类级别):
private static List<GeoData> dataList = new List<GeoData>();
关于c# - 一个类方法之间类库中的数据持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3281106/