共用方式為


在程式碼中開啟檔案的模型

您可以在任何應用程式中開啟 DSL 模型。

從 Visual Studio 延伸模組,您可就此目的使用 ModelBus。 ModelBus 提供標準機制,以供參考模型或模型中的原則,並在模型移動後尋找模型。 如需詳細資訊,請參閱使用 Visual Studio Modelbus 整合模型

目標 Framework

將應用程式專案的 [目標架構] 設為 .NET Framework 4.5 或更新版本。

  1. 開啟您要在其中讀取 DSL 模型之應用程式的 Visual Studio 專案。

  2. 在 [方案總管] 中,以滑鼠右鍵按一下專案,然後按一下 [屬性]

  3. 在 [專案屬性] 視窗的 [應用程式] 索引標籤上,將 [目標架構] 欄位設定為 [.NET Framework 4] (或更新版本)。

注意

目標架構不應該是 .NET Framework 4 用戶端設定檔

參考資料

將這些參考新增至 Visual Studio 應用程式專案:

  • Microsoft.VisualStudio.Modeling.Sdk.11.0

    • 如果您在 [新增參考] 對話方塊的 [.NET] 索引標籤底下看不到此參考, 請按一下 [瀏覽] 索引標籤,然後瀏覽至 %Program Files%\Microsoft Visual Studio\2022\YourVSversionSKU\VSSDK\VisualStudioIntegration\Common\Assemblies
  • 您的 DSL 組件,您會在 DSL 專案的 bin 資料夾下找到該組件。 其名稱的格式通常為:YourCompany.YourProject.Dsl.dll

DSL 中的重要類別

您應該先知道 DSL 所產生的某些類別名稱,才能撰寫可讀取 DSL 的程式碼。 在您的 DSL 解決方案中,開啟 Dsl 專案,然後查看 generatedCode 資料夾。 或者,在專案 [參考] 中按兩下 DSL 組件,然後在 [物件瀏覽器] 中開啟 DSL 命名空間。

以下是您應該識別的類別:

  • YourDslRootClass - 這是您的 DslDefinition.dsl 中的根類別名稱。

  • YourDslName SerializationHelper - 此類別定義於 DSL 專案的 SerializationHelper.cs 中。

  • YourDslName DomainModel - 此類別定義於 DSL 專案的 DomainModel.cs 中。

從檔案讀取

下列範例的設計訴求是要讀取 DSL,其中的重要類別如下所示:

  • FamilyTreeModel

  • FamilyTreeSerializationHelper

  • FamilyTreeDomainModel

此 DSL 中的其他領域類別是 Person。

using System;
using Microsoft.VisualStudio.Modeling;
using Company.FamilyTree; // Your DSL namespace

namespace StandaloneReadDslConsole
{ class Program
  { static void Main(string[] args)
    {
      // The path of a DSL model file:
      string dslModel = @"C:\FamilyTrees\Tudor.ftree";
      // Set up the Store to read your type of model:
      Store store = new Store(
        typeof(Company.FamilyTree.FamilyTreeDomainModel));
      // The Model type generated by the DSL:
      FamilyTreeModel familyTree;
      // All Store changes must be in a Transaction:
      using (Transaction t =
        store.TransactionManager.BeginTransaction("Load model"))
      {
        familyTree =
           FamilyTreeSerializationHelper.Instance.
              LoadModel(store, dslModel, null, null, null);
        t.Commit(); // Don't forget this!
      }
      // Now we can read the model:
      foreach (Person p in familyTree.People)
      {
        Console.WriteLine(p.Name);
        foreach (Person child in p.Children)
        {
          Console.WriteLine("    " + child.Name);
        }
} } } }

儲存至檔案

下列對先前程式碼的新增會變更模型,然後將其儲存至檔案。

using (Transaction t =
  store.TransactionManager.BeginTransaction("update model"))
{
  // Create a new model element:
  Person p = new Person(store);
  // Set its embedding relationship:
  p.FamilyTreeModel = familyTree;
  // - same as: familyTree.People.Add(p);
  // Set its properties:
  p.Name = "Edward VI";
  t.Commit(); // Don't forget this!
}
// Save the model:
try
{
  SerializationResult result = new SerializationResult();
  FamilyTreeSerializationHelper.Instance
    .SaveModel(result, familyTree, @"C:\FamilyTrees\Tudor-upd.ftree");
  // Report any error:
  if (result.Failed)
  {
    foreach (SerializationMessage message in result)
    {
      Console.WriteLine(message);
    }
  }
}
catch (System.IO.IOException ex)
{ ... }