跨語言互通性

語言的獨立性可能會使句子產生不同的意義。 其中一種表示,是在這篇文章 語言的獨立性和語言獨立元件中討論,涉及完美地從一個以一種語言撰寫的應用程式卻以另一種語言讀入型別。 第二個表示,是本文的焦點所在,包含合併多語言撰寫的程式碼至單一的 .NET Framework 組件。

下列範例會建立包含兩個類別 NumericLib 和 StringLib的 Utilities.dll 的類別庫,來說明跨語言互通性 (Interoperability)。 NumericLib 類別以 C# 撰寫,不過 StringLib 類別是以 Visual Basic 撰寫。 以下是 StringUtil.vb 的原始程式碼,包含它的StringLib 類別有單一成員 ToTitleCase。

Imports System.Collections.Generic
Imports System.Runtime.CompilerServices

Public Module StringLib
   Private exclusions As List(Of String) 

   Sub New()
      Dim words() As String = { "a", "an", "and", "of", "the" }
      exclusions = New List(Of String)
      exclusions.AddRange(words)
   End Sub

   <Extension()> _
   Public Function ToTitleCase(title As String) As String
      Dim words() As String = title.Split() 
      Dim result As String = String.Empty

      For ctr As Integer = 0 To words.Length - 1
         Dim word As String = words(ctr)
         If ctr = 0 OrElse Not exclusions.Contains(word.ToLower()) Then
            result += word.Substring(0, 1).ToUpper() + _
                      word.Substring(1).ToLower()
         Else
            result += word.ToLower()
         End If
         If ctr <= words.Length - 1 Then
            result += " "             
         End If   
      Next 
      Return result 
   End Function
End Module

以下是 NumberUtil.cs 的原始程式碼,它定義 NumericLib 類別有兩個成員 - IsEven 和 NearZero。

using System;

public static class NumericLib 
{
   public static bool IsEven(this IConvertible number)
   {
      if (number is Byte ||
          number is SByte ||
          number is Int16 ||
          number is UInt16 || 
          number is Int32 || 
          number is UInt32 ||
          number is Int64)
         return ((long) number) % 2 == 0;
      else if (number is UInt64)
         return ((ulong) number) %2 == 0;
      else
         throw new NotSupportedException("IsEven called for a non-integer value.");
   }

   public static bool NearZero(double number)
   {
      return number < .00001; 
   }
}

若要將兩個類別封裝在單一組件中,您必須將它們編譯成模組。 若要編譯 Visual Basic 原始程式碼檔案至模組中,請使用這個命令:

vbc /t:module StringUtil.vb 

如需 Visual Basic 編譯器命令列語法的詳細資訊,請參閱從命令列建置 (Visual Basic)

若要編譯 C# 原始程式碼檔案至模組中,請使用這個命令:

csc /t:module NumberUtil.cs

如需 C# 編譯器的命令列語法的詳細資訊,請參閱 使用 csc.exe 建置命令列

然後您可以使用 連結工具 (Link.exe) 將兩個模組編譯到組件中:

link numberutil.netmodule stringutil.netmodule /out:UtilityLib.dll /dll 

下列範例呼叫 NumericLib.NearZero 與 StringLib.ToTitleCase 方法。 請注意,Visual Basic 程式碼和 C# 程式碼都可以存取這兩個類別中的方法。

Module Example
   Public Sub Main()
      Dim dbl As Double = 0.0 - Double.Epsilon
      Console.WriteLine(NumericLib.NearZero(dbl))

      Dim s As String = "war and peace"
      Console.WriteLine(s.ToTitleCase())
   End Sub
End Module
' The example displays the following output:
'       True
'       War and Peace
using System;

public class Example
{
   public static void Main()
   {
      Double dbl = 0.0 - Double.Epsilon;
      Console.WriteLine(NumericLib.NearZero(dbl));

      string s = "war and peace";
      Console.WriteLine(s.ToTitleCase());
   }
}
// The example displays the following output:
//       True
//       War and Peace

若要編譯 Visual Basic 程式碼,請使用這個命令:

vbc example.vb /r:UtilityLib.dll

若要使用 C# 編譯,請將編譯器的名稱從 vbc 變更為 csc,並將副檔名從 .vb 變更為 .cs:

csc example.cs /r:UtilityLib.dll

相關主題

標題

描述

語言獨立性以及與語言無關的元件

說明如何建立CLS 標準的軟體元件,此元件可由任何語言撰寫的應用程式所使用 。

一般類型系統

描述 Common Language Runtime 如何宣告、使用及管理型別。

中繼資料和自我描述元件

說明 Common Language Runtime 描述型別和以型別本身儲存這項資訊的機制。