关于企业架构师:将 Jscript 转换为 C# 为 Sparx EA 创建 Add-in

Converting Jscript into C# to create Add-in for Sparx EA

我用 Jscript 编写了我的代码,用于在 EA 项目浏览器中扫描图表,然后创建一个关于现有元素的元素列表。该代码可以正常工作。目前,当我尝试将我的代码 (Jscript) 转换为 C# 以创建 Enterprise Architect 的自定义插件时遇到问题。

这是我在 Jscript 中的代码的一部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var theModel as EA.Package;
theModel = Repository.Models.GetAt( 0 );
// Iterate through all views (top level packages) in the model
var viewEnumerator = new Enumerator( theModel.Packages );
while ( !viewEnumerator.atEnd() )
{
    var currentView as EA.Package;
    currentView = viewEnumerator.item();

    // Add the name of this view to the output window
    Session.Output( currentView.Name );

    // Iterate through all diagrams in this view

    viewEnumerator.moveNext();
}

这是转换后的c#代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
EA.Package theModel;
theModel = Repository.Models.GetAt( 0 );

// Iterate through all views (top level packages) in the model
var viewEnumerator = new Enumerator( theModel.Packages );

while ( !viewEnumerator.atEnd() )
{
     EA.Package currentView;
    currentView = viewEnumerator.item();

    // Add the name of this view to the output window
    MessageBox.Show( currentView.Name );

    // Iterate through all diagrams in this view

    viewEnumerator.moveNext();
}

但是,我遇到以下问题:

1
 var viewEnumerator = new Enumerator( theModel.Packages );

错误是:

The type or namespace name 'Enumerator' could not be found (are you missing a using directive or an assembly reference?)

实际上,我不知道如何在 C#

中创建类似的东西

任何建议


您可以使用 foreach 循环而不是 Enumerator

1
2
3
4
5
6
7
8
9
EA.Package theModel;
theModel = Repository.Models.GetAt( 0 );

// Iterate through all views (top level packages) in the model
foreach( EA.Package currentView in theModel.Packages )
{
    // Add the name of this view to the output window
    MessageBox.Show( currentView.Name );
}

确保键入 currentView(并且不要使用 var),因为 EA.Collection 不是强类型。