Regex.CompileToAssembly メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
正規表現をコンパイルし、1 つのアセンブリ内のディスクに保存します。
オーバーロード
| 名前 | 説明 |
|---|---|
| CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) |
古い.
指定した 1 つ以上の Regex オブジェクトと指定したリソース ファイルを、指定した属性を持つ名前付きアセンブリにコンパイルします。 |
| CompileToAssembly(RegexCompilationInfo[], AssemblyName) |
古い.
指定した 1 つ以上の Regex オブジェクトを名前付きアセンブリにコンパイルします。 |
| CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) |
古い.
指定した 1 つ以上の 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.
指定した 1 つ以上の 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 プロパティの値が空または null 文字列です。
-又は-
regexinfos内の 1 つ以上のオブジェクトの正規表現パターンに無効な構文が含まれています。
assemblyname または regexinfos が null。
resourceFile パラメーターは、無効な Win32 リソース ファイルを指定します。
resourceFile パラメーターによって指定されたファイルが見つかりません。
コンパイルされた正規表現のアセンブリの作成はサポートされていません。
注釈
CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) メソッドは、regexinfos 配列で定義された各正規表現がクラスによって表される.NET Framework アセンブリを生成します。 通常、 CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) メソッドは、コンパイルされた正規表現のアセンブリを生成する別のアプリケーションから呼び出されます。 アセンブリに含まれる各正規表現には、次の特性があります。
これは、 Regex クラスから派生します。
fullnamespaceおよび対応するnameオブジェクトのRegexCompilationInfoパラメーターによって定義される完全修飾名が割り当てられます。これには、既定の (またはパラメーターなしの) コンストラクターがあります。
通常、コンパイルされた正規表現をインスタンス化して使用するコードは、アセンブリを作成するコードとは別のアセンブリまたはアプリケーションにあります。
CompileToAssembly メソッドは、特定の言語のクラス定義キーワード (C# の class や Class など) を使用する代わりに、メソッド呼び出しから.NET Framework アセンブリを生成するためです。End Class in Visual Basic) では、.NET Framework 属性を開発言語の標準属性構文を使用してアセンブリに割り当てることは許可されません。
attributes パラメーターは、アセンブリに適用される属性を定義するための代替メソッドを提供します。 アセンブリに適用する属性ごとに、次の操作を行います。
呼び出す属性コンストラクターのパラメーター型を表す Type オブジェクトの配列を作成します。
新しいアセンブリに適用する属性クラスを表す Type オブジェクトを取得します。
属性GetConstructorオブジェクトのType メソッドを呼び出して、呼び出す属性コンストラクターを表すConstructorInfo オブジェクトを取得します。 コンストラクターのパラメーター型を表すGetConstructor オブジェクトの配列をType メソッドに渡します
属性のコンストラクターに渡すパラメーターを定義する Object 配列を作成します。
手順 3 で取得したCustomAttributeBuilder オブジェクトと手順 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 メソッドを使用します。
こちらもご覧ください
適用対象
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.
指定した 1 つ以上の 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 プロパティの値が空または null 文字列です。
-又は-
regexinfos内の 1 つ以上のオブジェクトの正規表現パターンに無効な構文が含まれています。
assemblyname または regexinfos が null。
コンパイルされた正規表現のアセンブリの作成はサポートされていません。
例
次の例では、RegexLib.dllという名前のアセンブリを作成します。 アセンブリには、2 つのコンパイル済み正規表現が含まれています。 1 つ目の Utilities.RegularExpressions.DuplicatedStringは、2 つの同一の連続した単語と一致します。 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 クラスから派生します。
fullnamespaceおよび対応するnameオブジェクトのRegexCompilationInfoパラメーターによって定義される完全修飾名が割り当てられます。これには、既定の (またはパラメーターなしの) コンストラクターがあります。
通常、コンパイルされた正規表現をインスタンス化して使用するコードは、アセンブリを作成するコードとは別のアセンブリまたはアプリケーションにあります。
注意 (呼び出し元)
.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.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.
指定した 1 つ以上の 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 プロパティの値が空または null 文字列です。
-又は-
regexinfos内の 1 つ以上のオブジェクトの正規表現パターンに無効な構文が含まれています。
assemblyname または regexinfos が null。
コンパイルされた正規表現のアセンブリの作成はサポートされていません。
例
次の例では、RegexLib.dll という名前のアセンブリを作成し、 AssemblyTitleAttribute 属性を適用します。 アセンブリには、2 つのコンパイル済み正規表現が含まれています。 1 つ目の Utilities.RegularExpressions.DuplicatedStringは、2 つの同一の連続した単語と一致します。 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 クラスから派生します。
fullnamespaceおよび対応するnameオブジェクトのRegexCompilationInfoパラメーターによって定義される完全修飾名が割り当てられます。これには、既定の (またはパラメーターなしの) コンストラクターがあります。
通常、コンパイルされた正規表現をインスタンス化して使用するコードは、アセンブリを作成するコードとは別のアセンブリまたはアプリケーションにあります。
CompileToAssembly メソッドは、特定の言語のクラス定義キーワード (C# の class や Class など) を使用する代わりに、メソッド呼び出しから.NET Framework アセンブリを生成するためです。End Class in Visual Basic) では、.NET Framework 属性を開発言語の標準属性構文を使用してアセンブリに割り当てることは許可されません。
attributes パラメーターは、アセンブリに適用される属性を定義するための代替メソッドを提供します。 アセンブリに適用する属性ごとに、次の操作を行います。
呼び出す属性コンストラクターのパラメーター型を表す Type オブジェクトの配列を作成します。
新しいアセンブリに適用する属性クラスを表す Type オブジェクトを取得します。
属性GetConstructorオブジェクトのType メソッドを呼び出して、呼び出す属性コンストラクターを表すConstructorInfo オブジェクトを取得します。 コンストラクターのパラメーター型を表すGetConstructorオブジェクトの配列をTypeメソッドに渡します。
属性のコンストラクターに渡すパラメーターを定義する Object 配列を作成します。
手順 3 で取得したCustomAttributeBuilder オブジェクトと手順 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 メソッドを使用します。