Visual C# でのクライアント
Visual C# ではクライアントがどのように記述されるかを次に示します。
リスト 1. Visual C# でのクライアント (ClientCS.cs)
using System;
using CompVC;
using CompCS;
using CompVB;
// This class exists to house the application's entry point.
class MainApp {
// The static method, Main, is the application's entry point.
public static void Main() {
// Iterate through the component's strings,
// and write them to the console.
CompCS.StringComponent myCSStringComp = new
CompCS.StringComponent();
Console.WriteLine("Strings from C# StringComponent");
for (int index = 0; index < myCSStringComp.Count;
index++) {
Console.WriteLine(myCSStringComp.GetString(index));
}
// Iterate through the component's strings,
// and write them to the console.
CompVC.StringComponent myVCStringComp = new
CompVC.StringComponent();
Console.WriteLine("\nStrings from Visual C++
StringComponent");
for (int index = 0; index < myVCStringComp.Count;
index++) {
Console.WriteLine(myVCStringComp.GetString(index));
}
// Iterate through the component's strings,
// and write them to the console.
CompVB.StringComponent myVBStringComp = new
CompVB.StringComponent();
Console.WriteLine("\nStrings from Visual Basic
StringComponent");
for (int index = 0; index < myVBStringComp.Count;
index++) {
Console.WriteLine(myVBStringComp.GetString(index));
}
}
}
C++ マネージ拡張の例とは異なり、この時点でライブラリをインポートする必要はありません。代わりに、コンパイル プロセス時にライブラリを指定できます。using ステートメントをとおしてライブラリを指定する利点は、using が名前空間をプログラムに組み込むため、型名を完全限定しなくても、ライブラリ内の型を参照できるようになることです。しかし、この場合は、すべてのサンプル コンポーネントの型名が同じ (StringComponent) であるため、すべてのコンポーネントに共通するメソッド (GetString) やプロパティ (Count) を参照する場合は、あいまいさをなくすために、依然として完全限定名を使用する必要があります。
また、Visual C# では、この問題に対処するために、エイリアシングと呼ばれる機構も用意されています。using ステートメントを次のように変更する場合は、名前を完全に限定する必要はありません。
using VCStringComp = CompVC.StringComponent;
using CSStringComp = CompCS.StringComponent;
using VBStringComp = CompVB.StringComponent;
このクライアント コードは、スコープ解決演算子を除けば、実質的に C++ マネージ拡張の例と同じです。また、使用するライブラリの指定を除いて、3 つの文字列コンポーネントを呼び出すコードもすべて同じです。C++ マネージ拡張の例と同様に、3 つの各セクションの最初のステートメントが、型 StringComponent の新しいローカル変数を宣言し、宣言した変数を初期化し、そのコンストラクタを呼び出しています。
CompCS.StringComponent myCSStringComp = new
CompCS.StringComponent();
クライアントは、文字列をコンソールに書き込み、プログラムがこの部分に到達したことを示してから、Count プロパティの値を使用して適切な文字列コンポーネントのメンバを反復処理します。
for (int index = 0; index < myVCStringComp.Count;
index++) {
Console.WriteLine(myVCStringComp.GetString(index));
}
以上で必要な処理はすべて完了し、他の 2 つの言語コンポーネントに対しても同じことが繰り返されます。
新しい Visual C# クライアントのビルドは非常に簡単です。独自の ..\Bin サブディレクトリのコンポーネントを使用しているため、/reference コンパイル スイッチを使用して、明示的にそのコンポーネントを組み込んでおく必要があります。
csc.exe /debug+ /reference:..\Bin\CompCS.dll;
..\Bin\CompVB.dll;..\Bin\CompVC.dll
/out:..\Bin\ClientCS.exe ClientCS.cs
また、ビルド対象がクライアント プログラムであり、他のプログラムから呼び出される可能性のあるコンポーネントではないため、/t スイッチを使用する必要はありません。
それ以外の点について、ビルド プロセスは上の Visual C# の例と同じです。生成されたプログラムを実行すると、次の出力が生成されます。
C:\...\CompTest\Bin>clientcs
Strings from C# StringComponent
C# String 0
C# String 1
C# String 2
C# String 3
Strings from Visual C++ StringComponent
Visual C++ String 0
Visual C++ String 1
Visual C++ String 2
Visual C++ String 3
Strings from Visual Basic StringComponent
Visual Basic String 0
Visual Basic String 1
Visual Basic String 2
Visual Basic String 3
参照
Visual Basic でのクライアント | Windows フォームを使用する Windows クライアント | ASP.NET を使用するクライアント | 開発チュートリアルのまとめ | 付録 A: 名前空間を検索するためのツール