类库中一个类方法之间的数据持久化

发布于 2024-09-10 03:56:05 字数 897 浏览 6 评论 0原文

我正在使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如梦 2024-09-17 03:56:05

我不了解 AutoCAD 编程,但我怀疑它每次都会创建一个新实例。您可以尝试将变量设为静态(例如类级别):

private static List<GeoData> dataList = new List<GeoData>();

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):

private static List<GeoData> dataList = new List<GeoData>();
习惯成性 2024-09-17 03:56:05

每次调用方法时都会实例化一个新类吗? (请原谅,我不熟悉 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

迎风吟唱 2024-09-17 03:56:05

根据您提供的信息猜测,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?

小忆控 2024-09-17 03:56:05

看起来您正在调用类的新实例,您可以实现单例模式以确保始终调用相同的实例或保留点并第二次加载它们。

这是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文