c# - C# 中的 "using"- 从 using 语句中调用的辅助函数是否使用包含的 IDisposable 对象?

标签 c# autocad-plugin

我正在使用 ObjectARX C#.Net 框架开发 AutoCAD 2021 的插件应用程序。我目前正在尝试重构它/使代码更容易理解,因为我很快就会返回学校,而且我目前是我工作地点唯一的全职编码员。

我正在考虑用辅助函数替换我的一部分代码,以便可以在未来的 AutoCAD 项目中重用它,但目前它依赖于 IDisposable“事务”对象来运行。

如果我从事务“using”语句中调用函数,它会自动使用事务本身吗?或者,我需要将交易对象传递给函数吗?

编辑:此处包括一些示例代码。

using (Transaction trans0 = destDB.TransactionManager.StartTransaction())
{
    //Setup for CSV Export, following code reads control panel, may be replaced with helper function that reads control panel
    int grain = -1;
    int thickness = 0;
    int aCounter = 0;
    int bCounter = 0;
    String material = "NOTINITIALIZED";
    //End setup

    BlockTable destBT = trans0.GetObject(destDB.BlockTableId, OpenMode.ForRead) as BlockTable;
    BlockTableRecord destBTR = trans0.GetObject(destBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
    List<Entity> Containers = new List<Entity>();
    List<Entity> Selectors = new List<Entity>();
    List<Entity> SelectorFrames = new List<Entity>();
    foreach (ObjectId id1 in destBTR)
    {
        Entity en = trans0.GetObject(id1, OpenMode.ForRead) as Entity;
        if ((en.Layer.Equals("MDF")) || (en.Layer.Equals("PFM")) || (en.Layer.Equals("x")) || (en.Layer.Equals("y") || (en.Layer.Equals("ng"))))
        {
            SelectorFrames.Add(trans0.GetObject(id1, OpenMode.ForRead) as Entity);
        }
        if (en.Layer.Equals("selector"))
        {
            Selectors.Add(trans0.GetObject(id1, OpenMode.ForRead) as Entity);
        }
        if (en.Layer.Equals("Container"))
        {
            Containers.Add(trans0.GetObject(id1, OpenMode.ForRead) as Entity);
        }
        if (en.Layer.Equals("tCounter"))
        {
            thickness++;
        }
        if (en.Layer.Equals("aCounter"))
        {
            aCounter++;
        }
        if (en.Layer.Equals("bCounter"))
        {
            aCounter++;
        }
    }
    foreach (Entity selector in Selectors)
    {
        using (Trimmer trimmer = new Autodesk.AutoCAD.ExportLayout.Trimmer())
        {
            foreach (Entity sFrame in SelectorFrames)
            {
                trimmer.Trim(selector, sFrame);
                if (trimmer.EntityCompletelyInside)
                {
                    if (sFrame.Layer.Equals("x"))
                    {
                        grain = 1;
                    }
                    else if (sFrame.Layer.Equals("y"))
                    {
                        grain = 2;
                    }
                    else if (sFrame.Layer.Equals("ng"))
                    {
                        grain = 0;
                    }
                    else
                    {
                        material = sFrame.Layer;
                    }
                }
            }
        }
    }
    ... some code that's in the same scope but not related to this proposed helper function ...
}

最佳答案

对于大多数情况,答案是“否”:using 只会影响您正在处理的对象的生命周期。

但是,事务是一种特殊情况。创建和处理事务会导致某些状态信息与当前执行上下文相关联,并且已经编写了许多库来检测和尊重该状态。因此,如果您的代码调用一个方法,然后该方法执行数据库访问,则数据库访问很可能会在该事务的范围内执行。

您可以找到有关其工作原理的更多详细信息 here 。例如,当您创建新的事务作用域时如何设置静态 Transaction.Current 属性、事务管理器如何确定参与哪些事务操作,以及如果需要,您如何抑制当前事务作用域具有需要在当前事件的任何事务之外执行的代码。

关于c# - C# 中的 "using"- 从 using 语句中调用的辅助函数是否使用包含的 IDisposable 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68759663/

相关文章:

c# - 使用自定义对象作为字典键

c# - 在 AutoCAD 中限制另一个形状内的形状移动

autocad - 在accoreconsole.exe中加载自定义.net dll

c# - autocad .net在不使用ObjectId的情况下存储和检索对象ID

c# - 将 C# 对象存储到 AutoCAD 实体的 XRecord 中

c# - 类型 "MyProject.Bike"不包含采用 '0' 参数的构造函数 C#

c# - 在 Windows Phone ListPicker 控件中绑定(bind)一个大列表

c# - 将表格列表导出到 CSV 文件

c# - 如何通过 SmartFormat 反射语法使用 C# 扩展方法?

visual-studio - 为什么 AutoCAD 插件依赖于特定的 Visual Studio 版本?