首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 跨語言互操作實戰(四):呼叫 C++/VB 組件的用戶端程式

     編輯:關於C語言

用戶端程式完整碼

// 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 指定輸出路徑與檔名。

執行測試結果

輸入 "English"(三組件皆支援)


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

輸入 "Japanese"(僅 VB 組件支援)


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

跨語言呼叫核心觀念

  • .NET 受控程式碼遵循 CLS(Common Language Specification),語言間互操作無縫銜接。
  • 用戶端程式碼幾乎相同,僅差異於命名空間與編譯參數。
  • C++/CLI 需加 /clr、__gc 標記;VB.NET 預設受控;C# 原生受控。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved