語言

Regex.CompileToAssembly 方法

定義

編譯正則表達式並將它們儲存在單一組件中。

多載

名稱 Description
CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)
已淘汰.

將一個或多個指定的 Regex 物件及指定的資源檔案編譯成具指定屬性的命名組合語言。

CompileToAssembly(RegexCompilationInfo[], AssemblyName)
已淘汰.

將一個或多個指定的 Regex 物件編譯為命名的組裝。

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[])
已淘汰.

將一個或多個指定的 Regex 物件編譯為具有指定屬性的命名組裝。

備註

Note

在 .NET Core 和 .NET 5+ 中,呼叫 Regex.CompileToAssembly 方法會拋出 PlatformNotSupportedException。 不支援寫出組裝。

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)

來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs

警告

Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.

將一個或多個指定的 Regex 物件及指定的資源檔案編譯成具指定屬性的命名組合語言。

public:
 static void CompileToAssembly(cli::array <System::Text::RegularExpressions::RegexCompilationInfo ^> ^ regexinfos, System::Reflection::AssemblyName ^ assemblyname, cli::array <System::Reflection::Emit::CustomAttributeBuilder ^> ^ attributes, System::String ^ resourceFile);
[System.Obsolete("Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.", DiagnosticId="SYSLIB0036", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static void CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname, System.Reflection.Emit.CustomAttributeBuilder[]? attributes, string? resourceFile);
public static void CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname, System.Reflection.Emit.CustomAttributeBuilder[]? attributes, string? resourceFile);
public static void CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname, System.Reflection.Emit.CustomAttributeBuilder[] attributes, string resourceFile);
[<System.Obsolete("Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.", DiagnosticId="SYSLIB0036", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName * System.Reflection.Emit.CustomAttributeBuilder[] * string -> unit
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName * System.Reflection.Emit.CustomAttributeBuilder[] * string -> unit
Public Shared Sub CompileToAssembly (regexinfos As RegexCompilationInfo(), assemblyname As AssemblyName, attributes As CustomAttributeBuilder(), resourceFile As String)

參數

regexinfos
RegexCompilationInfo[]

一個描述要編譯的正則表達式的陣列。

assemblyname
AssemblyName

集合的檔名。

attributes
CustomAttributeBuilder[]

一個陣列,定義要套用到組裝的屬性。

resourceFile
String

要包含在組合語言中 Win32 資源檔案的名稱。

屬性

例外狀況

參數assemblyname屬性的Name值為空字串或空字串。

-或-

一個 regexinfos 或多個物件的正則表達式模式包含無效語法。

assemblyname 或 regexinfos 為 null。

該 resourceFile 參數表示一個無效的 Win32 資源檔案。

參數指定的 resourceFile 檔案無法找到。

不支援建立編譯後的正規表達式組合。

備註

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) 方法會產生一個 .NET Framework 組合語言,其中每個在 regexinfos 陣列中定義的正則表達式都由一個類別表示。 通常,該 CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) 方法會從一個獨立的應用程式呼叫,該應用程式會產生一組已編譯的正則表達式組合。 組合中包含的每個正則表達式具有以下特性:

  • 它是由 類別 Regex 衍生而來的。

  • 它被賦予由對應fullnamespace物件的參數name定義RegexCompilationInfo的全限定名稱。

  • 它有一個預設(或無參數)的建構子。

通常,實例化並使用編譯後正則表達式的程式碼,會存在於與產生組合語言的程式碼分開的組合語言或應用程式中。

因為 方法是從方法呼叫產生 .NET Framework 組合語言,而非使用特定語言的類別定義關鍵字(例如 C# 中的 或 ......),它不允許使用 .NET Framework 的標準屬性語法來指派給組合語言。 參數 attributes 提供了一種替代方法來定義適用於組裝的屬性。 對於你想套用到組件的每個屬性,請執行以下操作:

  1. 建立一個物件陣列 Type ,代表你想呼叫的屬性建構子的參數類型。

  2. 取得 Type 一個代表你想套用到新組合語言的屬性類別的物件。

  3. 呼叫 GetConstructor 屬性 Type 物件的方法,取得 ConstructorInfo 代表你想呼叫的屬性建構子的物件。 將代表建構子參數類型的物件陣列GetConstructor傳遞給Type該方法

  4. 建立一個 Object 陣列,定義要傳遞給屬性建構子的參數。

  5. 透過傳遞其建構子CustomAttributeBuilder、步驟 3 取得的物件及ConstructorInfo步驟 4 建立的陣列來實Object例化物件。

你可以將這些CustomAttributeBuilder物件的陣列傳給attributes方法,而不是參數CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)。

給呼叫者的注意事項

如果你在安裝了 Framework 4.5 或其點釋.NET.NET系統上開發,你會鎖定 Framework 4,並使用 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 方法建立包含編譯後正則表達式的組合語言。 嘗試在使用 .NET Framework 4 的系統中使用該組合語言中的正則表達式時,會拋出例外。 若要解決這個問題,您可以執行下列任何一項操作:

  • 請在安裝 .NET Framework 4 而非更新版本的系統上,建立包含已編譯正則表達式的組合語言。

  • 與其從組合語言呼叫CompileToAssembly(RegexCompilationInfo[], AssemblyName)和取得編譯後的正規表達式,不如使用靜態或實例Regex方法,並在實Compiled例化物件或呼叫正規表達式模式匹配方法時使用Regex選項。

另請參閱

適用於

CompileToAssembly(RegexCompilationInfo[], AssemblyName)

來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs

警告

Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.

將一個或多個指定的 Regex 物件編譯為命名的組裝。

public:
 static void CompileToAssembly(cli::array <System::Text::RegularExpressions::RegexCompilationInfo ^> ^ regexinfos, System::Reflection::AssemblyName ^ assemblyname);
[System.Obsolete("Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.", DiagnosticId="SYSLIB0036", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static void CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname);
public static void CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname);
[<System.Obsolete("Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.", DiagnosticId="SYSLIB0036", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName -> unit
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName -> unit
Public Shared Sub CompileToAssembly (regexinfos As RegexCompilationInfo(), assemblyname As AssemblyName)

參數

regexinfos
RegexCompilationInfo[]

一個描述要編譯的正則表達式的陣列。

assemblyname
AssemblyName

集合的檔名。

屬性

例外狀況

參數assemblyname屬性的Name值為空字串或空字串。

-或-

一個 regexinfos 或多個物件的正則表達式模式包含無效語法。

assemblyname 或 regexinfos 為 null。

不支援建立編譯後的正規表達式組合。

範例

以下範例會產生一個名為 RegexLib.dll的組裝。 該組合包含兩個編譯後的正則表達式。 第一個 Utilities.RegularExpressions.DuplicatedString,,與兩個相同且連續的單字相符。 第二個, Utilities.RegularExpressions.EmailAddress,檢查字串是否具有正確的電子郵件地址格式。

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;

public class RegexCompilationTest
{
   public static void Main()
   {
      RegexCompilationInfo expr;
      List<RegexCompilationInfo> compilationList = new List<RegexCompilationInfo>();

      // Define regular expression to detect duplicate words
      expr = new RegexCompilationInfo(@"\b(?<word>\w+)\s+(\k<word>)\b", 
                 RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, 
                 "DuplicatedString", 
                 "Utilities.RegularExpressions", 
                 true);
      // Add info object to list of objects
      compilationList.Add(expr);

      // Define regular expression to validate format of email address
      expr = new RegexCompilationInfo(@"^(?("")(""[^""]+?""@)|(([0-9A-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9A-Z])@))" + 
                 @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9A-Z][-\w]*[0-9A-Z]\.)+[A-Z]{2,6}))$", 
                 RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, 
                 "EmailAddress", 
                 "Utilities.RegularExpressions", 
                 true);
      // Add info object to list of objects
      compilationList.Add(expr);
                                             
      // Generate assembly with compiled regular expressions
      RegexCompilationInfo[] compilationArray = new RegexCompilationInfo[compilationList.Count];
      AssemblyName assemName = new AssemblyName("RegexLib, Version=1.0.0.1001, Culture=neutral, PublicKeyToken=null");
      compilationList.CopyTo(compilationArray); 
      Regex.CompileToAssembly(compilationArray, assemName);                                                 
   }
}
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Text.RegularExpressions

Module RegexCompilationTest
   Public Sub Main()
      Dim expr As RegexCompilationInfo
      Dim compilationList As New List(Of RegexCompilationInfo)
          
      ' Define regular expression to detect duplicate words
      expr = New RegexCompilationInfo("\b(?<word>\w+)\s+(\k<word>)\b", _
                 RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant, _
                 "DuplicatedString", _
                 "Utilities.RegularExpressions", _
                 True)
      ' Add info object to list of objects
      compilationList.Add(expr)

      ' Define regular expression to validate format of email address
      expr = New RegexCompilationInfo("^(?("")(""[^""]+?""@)|(([0-9A-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9A-Z])@))" + _
                 "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9A-Z][-\w]*[0-9A-Z]\.)+[A-Z]{2,6}))$", _
                 RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant, _
                 "EmailAddress", _
                 "Utilities.RegularExpressions", _
                 True)
      ' Add info object to list of objects
      compilationList.Add(expr)
                                             
      ' Generate assembly with compiled regular expressions
      Dim compilationArray(compilationList.Count - 1) As RegexCompilationInfo
      Dim assemName As New AssemblyName("RegexLib, Version=1.0.0.1001, Culture=neutral, PublicKeyToken=null")
      compilationList.CopyTo(compilationArray) 
      Regex.CompileToAssembly(compilationArray, assemName)                                                 
   End Sub
End Module

檢查字串是否有重複字的正則表達式會被實例化並用於以下範例。

using System;
using Utilities.RegularExpressions;

public class CompiledRegexUsage
{
   public static void Main()
   {
      string text = "The the quick brown fox  fox jumps over the lazy dog dog.";
      DuplicatedString duplicateRegex = new DuplicatedString(); 
      if (duplicateRegex.Matches(text).Count > 0)
         Console.WriteLine("There are {0} duplicate words in \n   '{1}'", 
            duplicateRegex.Matches(text).Count, text);
      else
         Console.WriteLine("There are no duplicate words in \n   '{0}'", 
                           text);
   }
}
// The example displays the following output to the console:
//    There are 3 duplicate words in
//       'The the quick brown fox  fox jumps over the lazy dog dog.'
Imports Utilities.RegularExpressions

Module CompiledRegexUsage
   Public Sub Main()
      Dim text As String = "The the quick brown fox  fox jumps over the lazy dog dog."
      Dim duplicateRegex As New DuplicatedString()
      If duplicateRegex.Matches(text).Count > 0 Then
         Console.WriteLine("There are {0} duplicate words in {2}   '{1}'", _
            duplicateRegex.Matches(text).Count, text, vbCrLf)
      Else
         Console.WriteLine("There are no duplicate words in {1}   '{0}'", _
                           text, vbCrLf)
      End If
   End Sub
End Module
' The example displays the following output to the console:
'    There are 3 duplicate words in
'       'The the quick brown fox  fox jumps over the lazy dog dog.'

成功編譯此第二個範例需在專案中加入對 RegexLib.dll(由第一個範例所建立的組件)的參考。

備註

CompileToAssembly(RegexCompilationInfo[], AssemblyName) 方法會產生一個 .NET Framework 組合語言,其中每個在 regexinfos 陣列中定義的正則表達式都由一個類別表示。 通常,該 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 方法會從一個獨立的應用程式呼叫,該應用程式會產生一組已編譯的正則表達式組合。 組合中包含的每個正則表達式具有以下特性:

  • 它是由 類別 Regex 衍生而來的。

  • 它被賦予由對應fullnamespace物件的參數name定義RegexCompilationInfo的全限定名稱。

  • 它有一個預設(或無參數)的建構子。

通常,實例化並使用編譯後正則表達式的程式碼,會存在於與產生組合語言的程式碼分開的組合語言或應用程式中。

給呼叫者的注意事項

如果你在安裝了 Framework 4.5 或其點釋.NET.NET系統上開發,你會鎖定 Framework 4,並使用 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 方法建立包含編譯後正則表達式的組合語言。 嘗試在使用 .NET Framework 4 的系統中使用該組合語言中的正則表達式時,會拋出例外。 若要解決這個問題,您可以執行下列任何一項操作:

  • 請在安裝 .NET Framework 4 而非更新版本的系統上,建立包含已編譯正則表達式的組合語言。

  • 與其從組合語言呼叫CompileToAssembly(RegexCompilationInfo[], AssemblyName)和取得編譯後的正規表達式,不如使用靜態或實例Regex方法,並在實Compiled例化物件或呼叫正規表達式模式匹配方法時使用Regex選項。

另請參閱

適用於

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[])

來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs
來源:
Regex.cs

警告

Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.

將一個或多個指定的 Regex 物件編譯為具有指定屬性的命名組裝。

public:
 static void CompileToAssembly(cli::array <System::Text::RegularExpressions::RegexCompilationInfo ^> ^ regexinfos, System::Reflection::AssemblyName ^ assemblyname, cli::array <System::Reflection::Emit::CustomAttributeBuilder ^> ^ attributes);
[System.Obsolete("Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.", DiagnosticId="SYSLIB0036", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static void CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname, System.Reflection.Emit.CustomAttributeBuilder[]? attributes);
public static void CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname, System.Reflection.Emit.CustomAttributeBuilder[]? attributes);
public static void CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname, System.Reflection.Emit.CustomAttributeBuilder[] attributes);
[<System.Obsolete("Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.", DiagnosticId="SYSLIB0036", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName * System.Reflection.Emit.CustomAttributeBuilder[] -> unit
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName * System.Reflection.Emit.CustomAttributeBuilder[] -> unit
Public Shared Sub CompileToAssembly (regexinfos As RegexCompilationInfo(), assemblyname As AssemblyName, attributes As CustomAttributeBuilder())

參數

regexinfos
RegexCompilationInfo[]

一個描述要編譯的正則表達式的陣列。

assemblyname
AssemblyName

集合的檔名。

attributes
CustomAttributeBuilder[]

一個陣列,定義要套用到組裝的屬性。

屬性

例外狀況

參數assemblyname屬性的Name值為空字串或空字串。

-或-

一個 regexinfos 或多個物件的正則表達式模式包含無效語法。

assemblyname 或 regexinfos 為 null。

不支援建立編譯後的正規表達式組合。

範例

以下範例建立一個名為 RegexLib.dll 的組裝,並將該 AssemblyTitleAttribute 屬性套用到它上。 該組合包含兩個編譯後的正則表達式。 第一個 Utilities.RegularExpressions.DuplicatedString,,與兩個相同且連續的單字相符。 第二個, Utilities.RegularExpressions.EmailAddress,檢查字串是否具有正確的電子郵件地址格式。

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;

public class RegexCompilationTest
{
   public static void Main()
   {
      RegexCompilationInfo expr;
      List<RegexCompilationInfo> compilationList = new List<RegexCompilationInfo>();

      // Define regular expression to detect duplicate words
      expr = new RegexCompilationInfo(@"\b(?<word>\w+)\s+(\k<word>)\b", 
                 RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, 
                 "DuplicatedString", 
                 "Utilities.RegularExpressions", 
                 true);
      // Add info object to list of objects
      compilationList.Add(expr);

      // Define regular expression to validate format of email address
      expr = new RegexCompilationInfo(@"^(?("")(""[^""]+?""@)|(([0-9A-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9A-Z])@))" + 
                 @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9A-Z][-\w]*[0-9A-Z]\.)+[zA-Z]{2,6}))$", 
                 RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, 
                 "EmailAddress", 
                 "Utilities.RegularExpressions", 
                 true);
      // Add info object to list of objects
      compilationList.Add(expr);
                                             
      // Apply AssemblyTitle attribute to the new assembly
      //
      // Define the parameter(s) of the AssemblyTitle attribute's constructor 
      Type[] parameters = { typeof(string) };
      // Define the assembly's title
      object[] paramValues = { "General-purpose library of compiled regular expressions" };
      // Get the ConstructorInfo object representing the attribute's constructor
      ConstructorInfo ctor = typeof(System.Reflection.AssemblyTitleAttribute).GetConstructor(parameters);
      // Create the CustomAttributeBuilder object array
      CustomAttributeBuilder[] attBuilder = { new CustomAttributeBuilder(ctor, paramValues) }; 
                                                         
      // Generate assembly with compiled regular expressions
      RegexCompilationInfo[] compilationArray = new RegexCompilationInfo[compilationList.Count];
      AssemblyName assemName = new AssemblyName("RegexLib, Version=1.0.0.1001, Culture=neutral, PublicKeyToken=null");
      compilationList.CopyTo(compilationArray); 
      Regex.CompileToAssembly(compilationArray, assemName, attBuilder);                                                 
   }
}
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Text.RegularExpressions

Module RegexCompilationTest
   Public Sub Main()
      Dim expr As RegexCompilationInfo
      Dim compilationList As New List(Of RegexCompilationInfo)
          
      ' Define regular expression to detect duplicate words
      expr = New RegexCompilationInfo("\b(?<word>\w+)\s+(\k<word>)\b", _
                 RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant, _
                 "DuplicatedString", _
                 "Utilities.RegularExpressions", _
                 True)
      ' Add info object to list of objects
      compilationList.Add(expr)

      ' Define regular expression to validate format of email address
      expr = New RegexCompilationInfo("^(?("")(""[^""]+?""@)|(([0-9A-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9A-Z])@))" + _ 
                 "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9A-Z][-\w]*[0-9A-Z]\.)+[A-Z]{2,6}))$", _
                 RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant, _
                 "EmailAddress", _
                 "Utilities.RegularExpressions", _
                 True)
      ' Add info object to list of objects
      compilationList.Add(expr)

      ' Apply AssemblyTitle attribute to the new assembly
      '
      ' Define the parameter(s) of the AssemblyTitle attribute's constructor 
      Dim params() As Type = { GetType(String) }
      ' Define the assembly's title
      Dim paramValues() As Object = { "General-purpose library of compiled regular expressions" }
      ' Get the ConstructorInfo object representing the attribute's constructor
      Dim ctor As ConstructorInfo = GetType(System.Reflection.AssemblyTitleAttribute).GetConstructor(params)
      ' Create the CustomAttributeBuilder object array
      Dim attBuilder() As CustomAttributeBuilder = { New CustomAttributeBuilder(ctor, paramValues) } 
                                                         
      ' Generate assembly with compiled regular expressions
      Dim compilationArray(compilationList.Count - 1) As RegexCompilationInfo
      Dim assemName As New AssemblyName("RegexLib, Version=1.0.0.1001, Culture=neutral, PublicKeyToken=null")
      compilationList.CopyTo(compilationArray) 
      Regex.CompileToAssembly(compilationArray, assemName, attBuilder) 
   End Sub
End Module

你可以透過使用像 ILDasm 這類反射工具檢查其清單來驗證該屬性是否 AssemblyTitleAttribute 已套用到組裝裝置上。

檢查字串是否有重複字的正則表達式會被實例化並用於以下範例。

using System;
using Utilities.RegularExpressions;

public class CompiledRegexUsage
{
   public static void Main()
   {
      string text = "The the quick brown fox  fox jumps over the lazy dog dog.";
      DuplicatedString duplicateRegex = new DuplicatedString(); 
      if (duplicateRegex.Matches(text).Count > 0)
         Console.WriteLine("There are {0} duplicate words in \n   '{1}'", 
            duplicateRegex.Matches(text).Count, text);
      else
         Console.WriteLine("There are no duplicate words in \n   '{0}'", 
                           text);
   }
}
// The example displays the following output to the console:
//    There are 3 duplicate words in
//       'The the quick brown fox  fox jumps over the lazy dog dog.'
Imports Utilities.RegularExpressions

Module CompiledRegexUsage
   Public Sub Main()
      Dim text As String = "The the quick brown fox  fox jumps over the lazy dog dog."
      Dim duplicateRegex As New DuplicatedString()
      If duplicateRegex.Matches(text).Count > 0 Then
         Console.WriteLine("There are {0} duplicate words in {2}   '{1}'", _
            duplicateRegex.Matches(text).Count, text, vbCrLf)
      Else
         Console.WriteLine("There are no duplicate words in {1}   '{0}'", _
                           text, vbCrLf)
      End If
   End Sub
End Module
' The example displays the following output to the console:
'    There are 3 duplicate words in
'       'The the quick brown fox  fox jumps over the lazy dog dog.'

成功編譯此第二個範例需在專案中加入對 RegexLib.dll(由第一個範例所建立的組件)的參考。

備註

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) 方法會產生一個 .NET Framework 組合語言,其中每個在 regexinfos 陣列中定義的正則表達式都由一個類別表示。 通常,該 CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) 方法會從一個獨立的應用程式呼叫,該應用程式會產生一組已編譯的正則表達式組合。 組合中包含的每個正則表達式具有以下特性:

  • 它是由 類別 Regex 衍生而來的。

  • 它被賦予由對應fullnamespace物件的參數name定義RegexCompilationInfo的全限定名稱。

  • 它有一個預設(或無參數)的建構子。

通常,實例化並使用編譯後正則表達式的程式碼,會存在於與產生組合語言的程式碼分開的組合語言或應用程式中。

因為 方法是從方法呼叫產生 .NET Framework 組合語言,而非使用特定語言的類別定義關鍵字(例如 C# 中的 或 ......),它不允許使用 .NET Framework 的標準屬性語法來指派給組合語言。 參數 attributes 提供了一種替代方法來定義適用於組裝的屬性。 對於你想套用到組件的每個屬性,請執行以下操作:

  1. 建立一個物件陣列 Type ,代表你想呼叫的屬性建構子的參數類型。

  2. 取得 Type 一個代表你想套用到新組合語言的屬性類別的物件。

  3. 呼叫 GetConstructor 屬性 Type 物件的方法,取得 ConstructorInfo 代表你想呼叫的屬性建構子的物件。 將代表建構子參數類型的物件陣列GetConstructor傳給Type方法。

  4. 建立一個 Object 陣列,定義要傳遞給屬性建構子的參數。

  5. 透過傳遞其建構子CustomAttributeBuilder、步驟 3 取得的物件及ConstructorInfo步驟 4 建立的陣列來實Object例化物件。

你可以將這些CustomAttributeBuilder物件的陣列傳給attributes方法,而不是參數Regex.CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[])。

給呼叫者的注意事項

如果你在安裝了 Framework 4.5 或其點釋.NET.NET系統上開發,你會鎖定 Framework 4,並使用 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 方法建立包含編譯後正則表達式的組合語言。 嘗試在使用 .NET Framework 4 的系統中使用該組合語言中的正則表達式時,會拋出例外。 若要解決這個問題,您可以執行下列任何一項操作:

  • 請在安裝 .NET Framework 4 而非更新版本的系統上,建立包含已編譯正則表達式的組合語言。

  • 與其從組合語言呼叫CompileToAssembly(RegexCompilationInfo[], AssemblyName)和取得編譯後的正規表達式,不如使用靜態或實例Regex方法,並在實Compiled例化物件或呼叫正規表達式模式匹配方法時使用Regex選項。

另請參閱

適用於