类库中一个类方法之间的数据持久化
我正在使用 .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() 方法时这些 dataList 和点列表为空。以及如何避免这种情况?
I am creating a class library for AutoCAD with .NET.
The problem is that the methods are called one after another from AutoCAD and first one reads input file and creates List of data in memory. However when the new one is called the list is empty.
I need to find a solution how to keep that data.
The List contains data in my created structure. Methods are called independently, but in order.
Short code example:
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()
{
...
}...
So why these dataList and points lists are empty when I call SelectPoints() method. And how to avoid that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不了解 AutoCAD 编程,但我怀疑它每次都会创建一个新实例。您可以尝试将变量设为静态(例如类级别):
I don't know about programming for AutoCAD, but I'd suspect that it's creating a new instance each time. You could try making the variables static (e.g. class-level):
每次调用方法时都会实例化一个新类吗? (请原谅,我不熟悉 AutoCAD 编码。)尝试将类设为静态。如果这不起作用,您可以将第一个方法的值返回到 AutoCAD 并将其作为参数发送到下一个方法吗?请记住,这不是性能的最佳解决方案。
另外,作为参考,请查看 C# 中的单例实现:
http: //msdn.microsoft.com/en-us/library/ff650316.aspx
Is it instantiating a new class each time it calls a method? (Forgive me, I'm not familiar with coding for AutoCAD.) Try making the class static. If that doesn't work, can you return the value(s) from the first method to AutoCAD and have it send those as arguments to the next method? That wouldn't be the best solution for performance, keep in mind.
Also, for reference, take a look at the a Singleton implementation in C#:
http://msdn.microsoft.com/en-us/library/ff650316.aspx
根据您提供的信息猜测,AutoCAD 是否会为每个方法调用创建对象的新实例?这可以解释为什么你的实例变量是空的。
尝试将变量设置为静态并查看数据是否在方法调用中持续存在。
AutoCAD 文档是否有编写这些程序的说明?
At a guess, based on the information you've given, does AutoCAD create a new instance of your object for each method call? This would explain why your instance variables are empty.
Try making the variables static and see if the data persists across method calls.
Does the AutoCAD docs have any instructions for writing these programs?
看起来您正在调用类的新实例,您可以实现单例模式以确保始终调用相同的实例或保留点并第二次加载它们。
这是 C# 中 Singleton 实现的一个很好的链接,http://csharpindepth.com/Articles/General /Singleton.aspx
It looks like you are calling a new instance of your class, You could implement a singleton pattern to make sure you are always calling the same instance or persist the points and load them second time round.
Here's a good link for the Singleton implementation in c#, http://csharpindepth.com/Articles/General/Singleton.aspx