作成日 2015.06.06
最終更新日 2015.06.06
原文
VSTA Sample Host Integration.docxは、SDKと一緒にダウンロードできます。
ホスト統合のVSTAサンプル
[VSTA Sample Host Integration]
プロジェクトをコンパイルして、実行する
Compiling and Running a Project
あなたが、プロジェクトを作成したあと、あなたは、BinaryManagerクラスを用いて、アドインを構築して、読み込みます。
- あなたのフォ-ムに、「Run」と名前つけられたボタンを追加し、ボタンにイベントハンドラを追加します。
- あなたのフォ-ムに、次に示す命令を追加します。:
- イベントハンドラに、次に示すコードを追加します。:
using System.IO; using System.Reflection;
private void button3_Click(object sender, EventArgs e)
{
if (_project == null)
{
//Error handling...
// エラー処理...
return;
}
var binaryManager = _project.BinaryManager;
// Find the BinaryItem for the output assembly and its .pdb file.
// 出力アセンブリとその.pdbファイルのための、BinaryItemを検索します。
string assemblyName = binaryManager.AssemblyName;
string pdbName = Path.GetFileNameWithoutExtension(assemblyName) + ".pdb";
VSTA.BinaryItem assemblyItem = null;
VSTA.BinaryItem pdbItem = null;
foreach (VSTA.BinaryItem item in binaryManager.GetBinaryItems())
{
string itemName = item.Name;
if (String.Equals(itemName, assemblyName, StringComparison.OrdinalIgnoreCase))
{
assemblyItem = item;
if (pdbItem != null)
break;
}
else if (String.Equals(itemName, pdbName, StringComparison.OrdinalIgnoreCase))
{
pdbItem = item;
if (assemblyItem != null)
break;
}
}
if (assemblyItem == null)
{
// Error handling...
// エラー処理...
return;
}
// Get the assembly as a byte[].
// byte[]として、アセンブリを取得します。
Stream assemblyStream = assemblyItem.GetStream();
byte[] assemblyBytes = new byte[assemblyStream.Length];
assemblyStream.Read(assemblyBytes, 0, assemblyBytes.Length);
// Get the .pdb as a byte[].
// byte[]として、.pdbを取得します。
Stream pdbStream = pdbItem.GetStream();
byte[] pdbBytes = new byte[pdbStream.Length];
pdbStream.Read(pdbBytes, 0, pdbBytes.Length);
// Load the raw assembly.
// 未加工のアセンブリを読み込みます。
Assembly assembly = Assembly.Load(assemblyBytes, pdbBytes);
// Find the AddIn class.
// AddInクラスを検索します。
Type addInType = assembly.GetTypes().FirstOrDefault((t) => t.Name == "AddIn");
if (addInType == null)
{
//Error handling...
// エラー処理...
return;
}
// Find the Init method.
// Initメソッドを検索します。
MethodInfo initMethod = addInType.GetMethod("Init");
if (initMethod == null)
{
//Error handling...
// エラー処理...
return;
}
// Initialize the add-in
// add-inを初期化します
initMethod.Invoke(null, null);
}
備考:
提供された例は、解説を目的としており、完全に網羅されていません。 例えば、インテグレータは、内部に、あるいは、分離した処理のアドインに呼び出しを作成するために、 アドインのためのホスト・オブジェクトを公開し引き渡したいかもしれません。
この例では、アセンブリとそのシンボルを読み込みます。; これは、デバッグをサポートするためだけに必要で、純粋な実行時の筋書きでは必要ありません。