You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Jscript转为C#开发Sparx EA插件时Enumerator类型找不到的求助

How to Iterate Enterprise Architect Packages in C# (Replace JScript Enumerator)

The Enumerator type you’re trying to use is a JScript-specific tool for traversing COM automation collections—C# doesn’t include this type, but we have much cleaner, more idiomatic ways to work with EA’s Packages and other collections directly.

Simplest Solution: Use foreach Loop

EA’s COM collections like Packages and Diagrams implement .NET’s IEnumerable interface, which lets you iterate through them directly with a foreach loop. This is far more readable and less error-prone than manual enumerator handling:

using EA;
using System.Windows.Forms; // For MessageBox functionality

// ...

EA.Package theModel = Repository.Models.GetAt(0);

// Iterate through all top-level packages
foreach (EA.Package currentView in theModel.Packages)
{
    MessageBox.Show(currentView.Name);
    
    // Optional: Iterate through diagrams in the current package
    foreach (EA.Diagram diagram in currentView.Diagrams)
    {
        MessageBox.Show($"Diagram: {diagram.Name}");
    }
}

Manual Enumerator Approach (If You Prefer Matching JScript Logic)

If you want a pattern closer to your original JScript code, you can use .NET’s IEnumerator from the System.Collections namespace. Just add the necessary using directive first:

using EA;
using System.Collections;
using System.Windows.Forms;

// ...

EA.Package theModel = Repository.Models.GetAt(0);
IEnumerator viewEnumerator = theModel.Packages.GetEnumerator();

while (viewEnumerator.MoveNext())
{
    // Safe cast to EA.Package to avoid runtime type errors
    EA.Package currentView = viewEnumerator.Current as EA.Package;
    if (currentView != null)
    {
        MessageBox.Show(currentView.Name);
        
        // Iterate through diagrams in the current package
        IEnumerator diagramEnumerator = currentView.Diagrams.GetEnumerator();
        while (diagramEnumerator.MoveNext())
        {
            EA.Diagram diagram = diagramEnumerator.Current as EA.Diagram;
            if (diagram != null)
            {
                MessageBox.Show($"Diagram: {diagram.Name}");
            }
        }
    }
}

Key Notes

  • Ensure your project references the EA Interop assembly (typically Interop.EA.dll) and includes the using EA; directive at the top of your file.
  • The foreach approach is the standard in C# for collection iteration and should be your go-to choice—it’s simpler to maintain and avoids boilerplate code.

内容的提问来源于stack exchange,提问作者Binito Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:46:27