// ClientCs.cs
using System;
using DICTVC;
using DICTCS;
using DICTVB;
class Test {
public static void Main() {
Console.Write("Input the language name of library to load: ");
string language = Console.ReadLine();
// 呼叫 C# 組件
DICTCS.DictionaryComponent csDict = new DICTCS.DictionaryComponent();
Console.WriteLine("\nDictionary from C# DictionaryComponent");
if (!csDict.LoadLibrary(language))
Console.WriteLine("Library not found in C# DictionaryComponent!");
else
Console.WriteLine("Library load successfully in C# DictionaryComponent");
// 呼叫 C++ 組件
DICTVC.DictionaryComponent vcDict = new DICTVC.DictionaryComponent();
Console.WriteLine("\nDictionary from C++ DictionaryComponent");
if (!vcDict.LoadLibrary(language))
Console.WriteLine("Library not found in C++ DictionaryComponent!");
else
Console.WriteLine("Library load successfully in C++ DictionaryComponent");
// 呼叫 VB 組件
DICTVB.DictionaryComponent vbDict = new DICTVB.DictionaryComponent();
Console.WriteLine("\nDictionary from VB DictionaryComponent");
if (!vbDict.LoadLibrary(language))
Console.WriteLine("Library not found in VB DictionaryComponent!");
else
Console.WriteLine("Library load successfully in VB DictionaryComponent");
// 顯示各組件目前載入的語言
Console.WriteLine("\nThe language of library in C# dictionary is: {0}", csDict.CurrentLibrary);
Console.WriteLine("The language of library in C++ dictionary is: {0}", vcDict.CurrentLibrary);
Console.WriteLine("The language of library in VB dictionary is: {0}", vbDict.CurrentLibrary);
// 釋放資源
csDict.FreeLibrary();
vcDict.FreeLibrary();
vbDict.FreeLibrary();
}
}
三個組件皆定義 DictionaryComponent,直接 using 會產生歧義。解法:
using VCDict = DICTVC.DictionaryComponent;
using CSDict = DICTCS.DictionaryComponent;
using VBDict = DICTVB.DictionaryComponent;
或在原始碼完整指定命名空間(如範例所示)。
csc /reference:..\Bin\DICTCS.dll;..\Bin\DICTVB.dll;..\Bin\DICTVC.dll /out:..\Bin\ClientCS.exe ClientCS.cs
/reference 以分號分隔多組件。/out 指定輸出路徑與檔名。
Dictionary from C# DictionaryComponent
Library load successfully in C# DictionaryComponent
Dictionary from C++ DictionaryComponent
Library load successfully in C++ DictionaryComponent
Dictionary from VB DictionaryComponent
Library load successfully in VB DictionaryComponent
The language of library in C# dictionary is: English
The language of library in C++ dictionary is: English
The language of library in VB dictionary is: English
Dictionary from C# DictionaryComponent
Library not found in C# DictionaryComponent!
Dictionary from C++ DictionaryComponent
Library not found in C++ DictionaryComponent!
Dictionary from VB DictionaryComponent
Library load successfully in VB DictionaryComponent
The language of library in C# dictionary is:
The language of library in C++ dictionary is:
The language of library in VB dictionary is: Japanese
/clr、__gc 標記;VB.NET 預設受控;C# 原生受控。