Regex.CompileToAssembly 方法

定義

編譯規則運算式並將它們以單一組件 (Assembly) 儲存至磁碟。

多載

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

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

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

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

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

將一個或多個指定的 Regex 物件和指定的資源檔編譯為具有指定之屬性的具名組件。

備註

注意

在 .NET Core 和 .NET 5+上,呼叫 方法會 Regex.CompileToAssembly 擲回 PlatformNotSupportedException 。 不支援寫出元件。

CompileToAssembly(RegexCompilationInfo[], AssemblyName)

Source:
Regex.cs
Source:
Regex.cs
Source:
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);
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}")]
public static void CompileToAssembly (System.Text.RegularExpressions.RegexCompilationInfo[] regexinfos, System.Reflection.AssemblyName assemblyname);
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName -> unit
[<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
Public Shared Sub CompileToAssembly (regexinfos As RegexCompilationInfo(), assemblyname As AssemblyName)

參數

regexinfos
RegexCompilationInfo[]

陣列,描述要編譯的規則運算式。

assemblyname
AssemblyName

組件的檔案名稱。

屬性

例外狀況

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

-或-

regexinfos 中一個或多個物件的規則運算式模式包含無效的語法。

assemblynameregexinfosnull

僅限 .NET Core 和 .NET 5+:不支援建立已編譯正則運算式的元件。

範例

下列範例會建立名為 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 類別。

  • 它會指派其對應 RegexCompilationInfo 物件的 和 name 參數所 fullnamespace 定義的完整名稱。

  • 它有預設 (或無參數) 建構函式。

一般而言,具現化和使用已編譯正則運算式的程式碼,可在與建立元件的程式碼分開的元件或應用程式中找到。

給呼叫者的注意事項

如果您要在已安裝 .NET Framework 4.5 或其點版本的系統上進行開發,則以 .NET Framework 4 為目標,並使用 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 方法來建立包含已編譯正則運算式的元件。 嘗試在該元件中使用其中一個具有 .NET Framework 4 的正則運算式,會擲回例外狀況。 若要解決這個問題,您可以執行下列任何一項操作:

另請參閱

適用於

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

Source:
Regex.cs
Source:
Regex.cs
Source:
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);
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}")]
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);
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName * System.Reflection.Emit.CustomAttributeBuilder[] -> unit
[<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
Public Shared Sub CompileToAssembly (regexinfos As RegexCompilationInfo(), assemblyname As AssemblyName, attributes As CustomAttributeBuilder())

參數

regexinfos
RegexCompilationInfo[]

陣列,描述要編譯的規則運算式。

assemblyname
AssemblyName

組件的檔案名稱。

attributes
CustomAttributeBuilder[]

陣列,定義要套用至組件的屬性。

屬性

例外狀況

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

-或-

regexinfos 中一個或多個物件的規則運算式模式包含無效的語法。

assemblynameregexinfosnull

僅限 .NET Core 和 .NET 5+:不支援建立已編譯正則運算式的元件。

範例

下列範例會建立名為 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

您可以檢查 AssemblyTitleAttribute 屬性是否已套用至元件,方法是使用 ILDasm 等反映公用程式檢查其資訊清單。

然後,下列範例會具現化並使用檢查重複字組字串的正則運算式。

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 類別。

  • 它會指派其對應 RegexCompilationInfo 物件的 和 name 參數所 fullnamespace 定義的完整名稱。

  • 它有預設 (或無參數) 建構函式。

一般而言,具現化和使用已編譯正則運算式的程式碼,可在與建立元件的程式碼分開的元件或應用程式中找到。

CompileToAssembly由於方法會從方法呼叫產生.NET Framework元件,而不是使用特定語言的類別定義關鍵字 (,例如 class C# 或 Class Visual Basic) ... End Class 中的類別定義關鍵字,因此不允許使用開發語言的標準屬性語法將.NET Framework屬性指派給元件。 參數 attributes 提供替代方法來定義套用至元件的屬性。 針對您想要套用至元件的每個屬性,請執行下列動作:

  1. 建立 物件的陣列,此陣列 Type 代表您要呼叫之屬性建構函式的參數類型。

  2. Type擷取物件,此物件代表您要套用至新元件的屬性類別。

  3. GetConstructor呼叫屬性 Type 物件的 方法,以擷取 ConstructorInfo 代表您要呼叫之屬性建構函式的物件。 GetConstructor傳遞 物件陣列,這個陣列 Type 表示建構函式的參數型別。

  4. 建立 Object 陣列,定義要傳遞至屬性建構函式的參數。

  5. 藉由傳遞物件建構函式, ConstructorInfo 傳遞步驟 3 中擷取的物件,以及步驟 4 中建立的 Object 陣列來具現化 CustomAttributeBuilder 物件。

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

給呼叫者的注意事項

如果您要在已安裝 .NET Framework 4.5 或其點版本的系統上進行開發,則以 .NET Framework 4 為目標,並使用 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 方法來建立包含已編譯正則運算式的元件。 嘗試在該元件中使用其中一個具有 .NET Framework 4 的正則運算式,會擲回例外狀況。 若要解決這個問題,您可以執行下列任何一項操作:

另請參閱

適用於

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

Source:
Regex.cs
Source:
Regex.cs
Source:
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);
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}")]
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);
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName * System.Reflection.Emit.CustomAttributeBuilder[] * string -> unit
[<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
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 屬性值為空字串或 null 字串。

-或-

regexinfos 中一個或多個物件的規則運算式模式包含無效的語法。

assemblynameregexinfosnull

resourceFile 參數指定不正確的 Win32 資源檔。

找不到 resourceFile 參數所指定的檔案。

僅限 .NET Core 和 .NET 5+:不支援建立已編譯正則運算式的元件。

備註

方法 CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) 會產生.NET Framework元件,其中陣列中 regexinfos 定義的每一個正則運算式是由類別表示。 一般而言,方法 CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) 會從產生已編譯正則運算式元件的個別應用程式呼叫。 元件中包含的每個正則運算式都有下列特性:

  • 它衍生自 Regex 類別。

  • 它會指派其對應 RegexCompilationInfo 物件的 和 name 參數所 fullnamespace 定義的完整名稱。

  • 它有預設 (或無參數) 建構函式。

一般而言,具現化和使用已編譯正則運算式的程式碼,可在與建立元件的程式碼分開的元件或應用程式中找到。

CompileToAssembly由於方法會從方法呼叫產生.NET Framework元件,而不是使用特定語言的類別定義關鍵字 (,例如 class C# 或 Class Visual Basic) ... End Class 中的類別定義關鍵字,因此不允許使用開發語言的標準屬性語法將.NET Framework屬性指派給元件。 參數 attributes 提供替代方法來定義套用至元件的屬性。 針對您想要套用至元件的每個屬性,請執行下列動作:

  1. 建立 物件的陣列,此陣列 Type 代表您要呼叫之屬性建構函式的參數類型。

  2. Type擷取物件,此物件代表您要套用至新元件的屬性類別。

  3. GetConstructor呼叫屬性 Type 物件的 方法,以擷取 ConstructorInfo 代表您要呼叫之屬性建構函式的物件。 GetConstructor傳遞 物件陣列,此陣列 Type 表示建構函式的參數類型

  4. 建立 Object 陣列,定義要傳遞至屬性建構函式的參數。

  5. 藉由傳遞物件建構函式, ConstructorInfo 傳遞步驟 3 中擷取的物件,以及步驟 4 中建立的 Object 陣列來具現化 CustomAttributeBuilder 物件。

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

給呼叫者的注意事項

如果您要在已安裝 .NET Framework 4.5 或其點版本的系統上進行開發,則以 .NET Framework 4 為目標,並使用 CompileToAssembly(RegexCompilationInfo[], AssemblyName) 方法來建立包含已編譯正則運算式的元件。 嘗試在該元件中使用其中一個具有 .NET Framework 4 的正則運算式,會擲回例外狀況。 若要解決這個問題,您可以執行下列任何一項操作:

另請參閱

適用於