Regex.CompileToAssembly メソッド

定義

正規表現をコンパイルして、ディスク上の単一のアセンブリに保存します。

オーバーロード

CompileToAssembly(RegexCompilationInfo[], AssemblyName)
互換性のために残されています。

1 つ以上の指定した Regex オブジェクトをコンパイルして、名前付きアセンブリに保存します。

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[])
互換性のために残されています。

指定した 1 つ以上の Regex オブジェクトをコンパイルして、指定した属性を持つ名前付きアセンブリに保存します。

CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)
互換性のために残されています。

指定した 1 つ以上の Regex オブジェクトと、指定した 1 つのリソース ファイルをコンパイルして、指定した属性を持つ名前付きアセンブリに保存します。

注釈

注意

.NET Core と .NET 5 以降では、メソッドの Regex.CompileToAssembly 呼び出しで PlatformNotSupportedException. アセンブリの書き込みはサポートされていません。

CompileToAssembly(RegexCompilationInfo[], AssemblyName)

注意事項

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

1 つ以上の指定した 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 RegexGeneratorAttribute 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 RegexGeneratorAttribute 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 の 1 つ以上のオブジェクトの正規表現パターンに、無効な構文が含まれています。

assemblyname または regexinfosnull です。

.NET Core と .NET 5 以降のみ: コンパイルされた正規表現のアセンブリの作成はサポートされていません。

次の例では、RegexLib.dllという名前のアセンブリを作成します。 アセンブリには、コンパイルされた 2 つの正規表現が含まれています。 1 つ目の単語は、 Utilities.RegularExpressions.DuplicatedString2 つの同一の連続した単語と一致します。 2 つ目は、 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.'

この 2 番目の例を正常にコンパイルするには、RegexLib.dll (最初の例で作成されたアセンブリ) への参照をプロジェクトに追加する必要があります。

注釈

このメソッドはCompileToAssembly(RegexCompilationInfo[], AssemblyName)、配列で定義regexinfosされた各正規表現がクラスによって表される.NET Frameworkアセンブリを生成します。 通常、この CompileToAssembly(RegexCompilationInfo[], AssemblyName) メソッドは、コンパイルされた正規表現のアセンブリを生成する別のアプリケーションから呼び出されます。 アセンブリに含まれる各正規表現には、次の特性があります。

  • これはクラスから Regex 派生します。

  • 対応するRegexCompilationInfoオブジェクトのパラメーターとnameによってfullnamespace定義される完全修飾名が割り当てられます。

  • これには、既定の (またはパラメーターなしの) コンストラクターがあります。

通常、コンパイルされた正規表現をインスタンス化して使用するコードは、アセンブリを作成するコードとは別のアセンブリまたはアプリケーションにあります。

注意 (呼び出し元)

.NET Framework 4.5 またはそのポイント リリースがインストールされているシステムで開発している場合は、.NET Framework 4 をターゲットとし、このメソッドを使用CompileToAssembly(RegexCompilationInfo[], AssemblyName)してコンパイルされた正規表現を含むアセンブリを作成します。 .NET Framework 4 のシステムで、そのアセンブリ内のいずれかの正規表現を使用しようとすると、例外がスローされます。 この問題を回避するには、次のいずれかの方法を実行します。

  • コンパイルされた正規表現を含むアセンブリを、.NET Framework 4 以降のバージョンがインストールされているシステムでビルドします。

  • アセンブリからコンパイルされた正規表現を呼び出CompileToAssembly(RegexCompilationInfo[], AssemblyName)して取得する代わりに、オブジェクトをインスタンス化Regexするとき、または正規表現パターン マッチング メソッドを呼び出すときに、オプションでCompiled静的メソッドまたはインスタンス Regex メソッドを使用します。

こちらもご覧ください

適用対象

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

注意事項

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

指定した 1 つ以上の 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 RegexGeneratorAttribute 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 RegexGeneratorAttribute 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 の 1 つ以上のオブジェクトの正規表現パターンに、無効な構文が含まれています。

assemblyname または regexinfosnull です。

.NET Core と .NET 5 以降のみ: コンパイルされた正規表現のアセンブリの作成はサポートされていません。

次の例では、RegexLib.dllという名前のアセンブリを作成し、そのアセンブリに属性を AssemblyTitleAttribute 適用します。 アセンブリには、コンパイルされた 2 つの正規表現が含まれています。 1 つ目の単語は、 Utilities.RegularExpressions.DuplicatedString2 つの同一の連続した単語と一致します。 2 つ目は、 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.'

この 2 番目の例を正常にコンパイルするには、RegexLib.dll (最初の例で作成されたアセンブリ) への参照をプロジェクトに追加する必要があります。

注釈

このメソッドはCompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[])、配列で定義regexinfosされた各正規表現がクラスによって表される.NET Frameworkアセンブリを生成します。 通常、この CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) メソッドは、コンパイルされた正規表現のアセンブリを生成する別のアプリケーションから呼び出されます。 アセンブリに含まれる各正規表現には、次の特性があります。

  • これはクラスから Regex 派生します。

  • 対応するRegexCompilationInfoオブジェクトのパラメーターとnameによってfullnamespace定義される完全修飾名が割り当てられます。

  • これには、既定の (またはパラメーターなしの) コンストラクターがあります。

通常、コンパイルされた正規表現をインスタンス化して使用するコードは、アセンブリを作成するコードとは別のアセンブリまたはアプリケーションにあります。

メソッドはCompileToAssembly、特定の言語のクラス定義キーワード (Visual Basicの C# や Class...End Class などclass) を使用する代わりに、メソッド呼び出しから.NET Framework アセンブリを生成するため、開発言語の標準属性構文を使用してアセンブリに.NET Framework属性を割り当てられません。 このパラメーターは attributes 、アセンブリに適用される属性を定義するための別の方法を提供します。 アセンブリに適用する属性ごとに、次の操作を行います。

  1. 呼び出す属性コンストラクターの Type パラメーター型を表すオブジェクトの配列を作成します。

  2. 新しいアセンブリに Type 適用する属性クラスを表すオブジェクトを取得します。

  3. 属性オブジェクトのGetConstructorメソッドを呼び出して、ConstructorInfo呼び出す属性コンストラクターを表すオブジェクトを取得Typeします。 コンストラクターの GetConstructor パラメーター型を表すオブジェクトの Type 配列をメソッドに渡します。

  4. 属性の Object コンストラクターに渡すパラメーターを定義する配列を作成します。

  5. CustomAttributeBuilder手順 3 で取得したオブジェクトと手順 4 で作成した配列をコンストラクターConstructorInfoに渡して、オブジェクトをObjectインスタンス化します。

その後、パラメーターの代わりに、これらの CustomAttributeBuilder オブジェクトの配列を attributes メソッドに Regex.CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) 渡すことができます。

注意 (呼び出し元)

.NET Framework 4.5 またはそのポイント リリースがインストールされているシステムで開発している場合は、.NET Framework 4 をターゲットとし、このメソッドを使用CompileToAssembly(RegexCompilationInfo[], AssemblyName)してコンパイルされた正規表現を含むアセンブリを作成します。 .NET Framework 4 のシステムで、そのアセンブリ内のいずれかの正規表現を使用しようとすると、例外がスローされます。 この問題を回避するには、次のいずれかの方法を実行します。

  • コンパイルされた正規表現を含むアセンブリを、.NET Framework 4 以降のバージョンがインストールされているシステムでビルドします。

  • アセンブリからコンパイルされた正規表現を呼び出CompileToAssembly(RegexCompilationInfo[], AssemblyName)して取得する代わりに、オブジェクトをインスタンス化Regexするとき、または正規表現パターン マッチング メソッドを呼び出すときに、オプションでCompiled静的メソッドまたはインスタンス Regex メソッドを使用します。

こちらもご覧ください

適用対象

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

注意事項

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

指定した 1 つ以上の Regex オブジェクトと、指定した 1 つのリソース ファイルをコンパイルして、指定した属性を持つ名前付きアセンブリに保存します。

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 RegexGeneratorAttribute 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 RegexGeneratorAttribute 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 の 1 つ以上のオブジェクトの正規表現パターンに、無効な構文が含まれています。

assemblyname または regexinfosnull です。

resourceFile パラメーターは無効な Win32 リソース ファイルを指定します。

resourceFile パラメーターで指定されているファイルが見つかりません。

.NET Core と .NET 5 以降のみ: コンパイルされた正規表現のアセンブリの作成はサポートされていません。

注釈

このメソッドはCompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)、配列で定義regexinfosされた各正規表現がクラスによって表される.NET Frameworkアセンブリを生成します。 通常、この CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) メソッドは、コンパイルされた正規表現のアセンブリを生成する別のアプリケーションから呼び出されます。 アセンブリに含まれる各正規表現には、次の特性があります。

  • これはクラスから Regex 派生します。

  • 対応するRegexCompilationInfoオブジェクトのパラメーターとnameによってfullnamespace定義される完全修飾名が割り当てられます。

  • これには、既定の (またはパラメーターなしの) コンストラクターがあります。

通常、コンパイルされた正規表現をインスタンス化して使用するコードは、アセンブリを作成するコードとは別のアセンブリまたはアプリケーションにあります。

メソッドはCompileToAssembly、特定の言語のクラス定義キーワード (Visual Basicの C# や Class...End Class などclass) を使用する代わりに、メソッド呼び出しから.NET Framework アセンブリを生成するため、開発言語の標準属性構文を使用してアセンブリに.NET Framework属性を割り当てられません。 このパラメーターは attributes 、アセンブリに適用される属性を定義するための別の方法を提供します。 アセンブリに適用する属性ごとに、次の操作を行います。

  1. 呼び出す属性コンストラクターの Type パラメーター型を表すオブジェクトの配列を作成します。

  2. 新しいアセンブリに Type 適用する属性クラスを表すオブジェクトを取得します。

  3. 属性オブジェクトのGetConstructorメソッドを呼び出して、ConstructorInfo呼び出す属性コンストラクターを表すオブジェクトを取得Typeします。 コンストラクターの GetConstructor パラメーター型を表すオブジェクトの Type 配列をメソッドに渡します

  4. 属性の Object コンストラクターに渡すパラメーターを定義する配列を作成します。

  5. CustomAttributeBuilder手順 3 で取得したオブジェクトと手順 4 で作成した配列をコンストラクターConstructorInfoに渡して、オブジェクトをObjectインスタンス化します。

その後、パラメーターの代わりに、これらの CustomAttributeBuilder オブジェクトの配列を attributes メソッドに CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) 渡すことができます。

注意 (呼び出し元)

.NET Framework 4.5 またはそのポイント リリースがインストールされているシステムで開発している場合は、.NET Framework 4 をターゲットとし、このメソッドを使用CompileToAssembly(RegexCompilationInfo[], AssemblyName)してコンパイルされた正規表現を含むアセンブリを作成します。 .NET Framework 4 のシステムで、そのアセンブリ内のいずれかの正規表現を使用しようとすると、例外がスローされます。 この問題を回避するには、次のいずれかの方法を実行します。

  • コンパイルされた正規表現を含むアセンブリを、.NET Framework 4 以降のバージョンがインストールされているシステムでビルドします。

  • アセンブリからコンパイルされた正規表現を呼び出CompileToAssembly(RegexCompilationInfo[], AssemblyName)して取得する代わりに、オブジェクトをインスタンス化Regexするとき、または正規表現パターン マッチング メソッドを呼び出すときに、オプションでCompiled静的メソッドまたはインスタンス Regex メソッドを使用します。

こちらもご覧ください

適用対象