将Jscript转为C#开发Sparx EA插件时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 theusing EA;directive at the top of your file. - The
foreachapproach 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

