次の方法で共有


プログラム コード内のファイルからモデルを開く

DSL モデルは任意のアプリケーションで開くことができます。

この目的のため、Visual Studio 拡張機能から ModelBus を使用できます。 ModelBus には、モデルまたはモデル内の要素を参照したり、モデルが移動された場合にそのモデルを検索したりするための標準的なメカニズムが用意されています。 詳細については、「Visual Studio Modelbus を使用してモデルを統合する」を参照してください。

ターゲット フレーム

アプリケーション プロジェクトのターゲット フレームワークを .NET Framework 4 以降に設定します。

  1. DSL モデルを読み取るアプリケーションの Visual Studio プロジェクトを開きます。

  2. ソリューション エクスプローラーでプロジェクトを右クリックし、 [プロパティ] をクリックします。

  3. プロジェクトのプロパティ ウィンドウの [アプリケーション] タブで、 [ターゲット フレームワーク] フィールドを .NET Framework 4 (またはそれ以降) に設定します。

Note

ターゲット フレームワークを .NET Framework 4 Client Profile にすることはできません。

References

次の参照を 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 のもう 1 つのドメイン クラスは 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)
{ ... }