Regex.CompileToAssembly Metodo
Definizione
Compila espressioni regolari e le salva su disco in un singolo assembly.Compiles regular expressions and saves them to disk in a single assembly.
Overload
CompileToAssembly(RegexCompilationInfo[], AssemblyName) |
Compila uno o più oggetti Regex specificati in un assembly denominato.Compiles one or more specified Regex objects to a named assembly. |
CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) |
Compila uno o più oggetti Regex specificati in un assembly denominato con gli attributi specificati.Compiles one or more specified Regex objects to a named assembly with the specified attributes. |
CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) |
Compila uno o più oggetti Regex specificati e un file di risorse specificato in un assembly denominato con gli attributi specificati.Compiles one or more specified Regex objects and a specified resource file to a named assembly with the specified attributes. |
Commenti
In .NET Core le chiamate al Regex.CompileToAssembly
metodo generano PlatformNotSupportedException ; la scrittura di un assembly non è supportata.On .NET Core, calls to the Regex.CompileToAssembly
method throw a PlatformNotSupportedException; writing out an assembly is not supported.
CompileToAssembly(RegexCompilationInfo[], AssemblyName)
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);
static member CompileToAssembly : System.Text.RegularExpressions.RegexCompilationInfo[] * System.Reflection.AssemblyName -> unit
Public Shared Sub CompileToAssembly (regexinfos As RegexCompilationInfo(), assemblyname As AssemblyName)
Parametri
- regexinfos
- RegexCompilationInfo[]
Matrice che descrive le espressioni regolari da compilare.An array that describes the regular expressions to compile.
- assemblyname
- AssemblyName
Nome file dell'assembly.The file name of the assembly.
Eccezioni
Il valore della proprietà Name del parametro assemblyname
è una stringa vuota o null.The value of the assemblyname
parameter's Name property is an empty or null string.
-oppure--or-
Il criterio di ricerca di espressioni regolari di uno o più oggetti in regexinfos
contiene una sintassi non valida.The regular expression pattern of one or more objects in regexinfos
contains invalid syntax.
assemblyname
o regexinfos
è null
.assemblyname
or regexinfos
is null
.
Solo .NET 5+ e .NET Core: la creazione di un assembly di espressioni regolari compilate non è supportata..NET 5+ and .NET Core only: Creating an assembly of compiled regular expressions is not supported.
Esempio
Nell'esempio seguente viene creato un assembly denominato RegexLib.dll.The following example creates an assembly named RegexLib.dll. L'assembly include due espressioni regolari compilate.The assembly includes two compiled regular expressions. Il primo, Utilities.RegularExpressions.DuplicatedString
, corrisponde a due parole contigue identiche.The first, Utilities.RegularExpressions.DuplicatedString
, matches two identical contiguous words. Il secondo, Utilities.RegularExpressions.EmailAddress
, controlla se una stringa ha il formato corretto come indirizzo di posta elettronica.The second, Utilities.RegularExpressions.EmailAddress
, checks whether a string has the correct format to be an email address.
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
Viene quindi creata un'istanza dell'espressione regolare che controlla una stringa per le parole duplicate e usata nell'esempio seguente.The regular expression that checks a string for duplicate words is then instantiated and used by the following example.
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.'
Per la corretta compilazione di questo secondo esempio è necessario aggiungere al progetto un riferimento a RegexLib.dll (l'assembly creato dal primo esempio).Successful compilation of this second example requires a reference to RegexLib.dll (the assembly created by the first example) to be added to the project.
Commenti
Il CompileToAssembly(RegexCompilationInfo[], AssemblyName) metodo genera un assembly .NET Framework in cui ogni espressione regolare definita nella regexinfos
matrice è rappresentata da una classe.The CompileToAssembly(RegexCompilationInfo[], AssemblyName) method generates a .NET Framework assembly in which each regular expression defined in the regexinfos
array is represented by a class. Il CompileToAssembly(RegexCompilationInfo[], AssemblyName) metodo viene in genere chiamato da un'applicazione separata che genera un assembly di espressioni regolari compilate.Typically, the CompileToAssembly(RegexCompilationInfo[], AssemblyName) method is called from a separate application that generates an assembly of compiled regular expressions. Ogni espressione regolare inclusa nell'assembly presenta le caratteristiche seguenti:Each regular expression included in the assembly has the following characteristics:
Viene derivato dalla Regex classe.It is derived from the Regex class.
Viene assegnato il nome completo definito dai
fullnamespace
name
parametri e dell' RegexCompilationInfo oggetto corrispondente.It is assigned the fully qualified name that is defined by thefullnamespace
andname
parameters of its corresponding RegexCompilationInfo object.Ha un costruttore predefinito o senza parametri.It has a default (or parameterless) constructor.
In genere, il codice che crea un'istanza e usa l'espressione regolare compilata si trova in un assembly o in un'applicazione distinta dal codice che crea l'assembly.Ordinarily, the code that instantiates and uses the compiled regular expression is found in an assembly or application that is separate from the code that creates the assembly.
Note per i chiamanti
Se si sta sviluppando in un sistema in cui sono .NET Framework 4.5.NET Framework 4.5 installati o le versioni intermedie, è necessario .NET Framework 4.NET Framework 4 utilizzare il CompileToAssembly(RegexCompilationInfo[], AssemblyName) metodo per creare un assembly contenente espressioni regolari compilate.If you are developing on a system that has .NET Framework 4.5.NET Framework 4.5 or its point releases installed, you target .NET Framework 4.NET Framework 4, and you use the CompileToAssembly(RegexCompilationInfo[], AssemblyName) method to create an assembly that contains compiled regular expressions. Il tentativo di usare una delle espressioni regolari in tale assembly in un sistema che ha .NET Framework 4.NET Framework 4 generato un'eccezione.Trying to use one of the regular expressions in that assembly on a system that has .NET Framework 4.NET Framework 4 throws an exception. Per risolvere il problema, è possibile eseguire una delle operazioni seguenti:To work around this problem, you can do either of the following: -Compila l'assembly che contiene le espressioni regolari compilate in un sistema in cui è .NET Framework 4.NET Framework 4 installato anziché versioni successive.- Build the assembly that contains the compiled regular expressions on a system that has .NET Framework 4.NET Framework 4 instead of later versions installed.
-Anziché chiamare CompileToAssembly(RegexCompilationInfo[], AssemblyName) e recuperare l'espressione regolare compilata da un assembly, usare i metodi statici o Regex di istanza con l' Compiled opzione quando si crea un'istanza di un oggetto o si chiama un metodo di criteri di ricerca di Regex espressioni regolari.- Instead of calling CompileToAssembly(RegexCompilationInfo[], AssemblyName) and retrieving the compiled regular expression from an assembly, use either static or instance Regex methods with the Compiled option when you instantiate a Regex object or call a regular expression pattern matching method.
Vedi anche
Si applica a
CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[])
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);
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
Public Shared Sub CompileToAssembly (regexinfos As RegexCompilationInfo(), assemblyname As AssemblyName, attributes As CustomAttributeBuilder())
Parametri
- regexinfos
- RegexCompilationInfo[]
Matrice che descrive le espressioni regolari da compilare.An array that describes the regular expressions to compile.
- assemblyname
- AssemblyName
Nome file dell'assembly.The file name of the assembly.
- attributes
- CustomAttributeBuilder[]
Matrice che definisce gli attributi da applicare all'assembly.An array that defines the attributes to apply to the assembly.
Eccezioni
Il valore della proprietà Name del parametro assemblyname
è una stringa vuota o null.The value of the assemblyname
parameter's Name property is an empty or null string.
-oppure--or-
Il criterio di ricerca di espressioni regolari di uno o più oggetti in regexinfos
contiene una sintassi non valida.The regular expression pattern of one or more objects in regexinfos
contains invalid syntax.
assemblyname
o regexinfos
è null
.assemblyname
or regexinfos
is null
.
Solo .NET 5+ e .NET Core: la creazione di un assembly di espressioni regolari compilate non è supportata..NET 5+ and .NET Core only: Creating an assembly of compiled regular expressions is not supported.
Esempio
Nell'esempio seguente viene creato un assembly denominato RegexLib.dll a cui viene applicato l' AssemblyTitleAttribute attributo.The following example creates an assembly named RegexLib.dll and applies the AssemblyTitleAttribute attribute to it. L'assembly include due espressioni regolari compilate.The assembly includes two compiled regular expressions. Il primo, Utilities.RegularExpressions.DuplicatedString
, corrisponde a due parole contigue identiche.The first, Utilities.RegularExpressions.DuplicatedString
, matches two identical contiguous words. Il secondo, Utilities.RegularExpressions.EmailAddress
, controlla se una stringa ha il formato corretto come indirizzo di posta elettronica.The second, Utilities.RegularExpressions.EmailAddress
, checks whether a string has the correct format to be an email address.
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
È possibile verificare che l' AssemblyTitleAttribute attributo sia stato applicato all'assembly esaminando il manifesto con un'utilità di reflection, ad esempio Ildasm.You can verify that the AssemblyTitleAttribute attribute has been applied to the assembly by examining its manifest with a reflection utility such as ILDasm.
Viene quindi creata un'istanza dell'espressione regolare che controlla una stringa per le parole duplicate e usata nell'esempio seguente.The regular expression that checks a string for duplicate words is then instantiated and used by the following example.
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.'
Per la corretta compilazione di questo secondo esempio è necessario aggiungere al progetto un riferimento a RegexLib.dll (l'assembly creato dal primo esempio).Successful compilation of this second example requires a reference to RegexLib.dll (the assembly created by the first example) to be added to the project.
Commenti
Il CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) metodo genera un assembly .NET Framework in cui ogni espressione regolare definita nella regexinfos
matrice è rappresentata da una classe.The CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) method generates a .NET Framework assembly in which each regular expression defined in the regexinfos
array is represented by a class. Il CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) metodo viene in genere chiamato da un'applicazione separata che genera un assembly di espressioni regolari compilate.Typically, the CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) method is called from a separate application that generates an assembly of compiled regular expressions. Ogni espressione regolare inclusa nell'assembly presenta le caratteristiche seguenti:Each regular expression included in the assembly has the following characteristics:
Viene derivato dalla Regex classe.It is derived from the Regex class.
Viene assegnato il nome completo definito dai
fullnamespace
name
parametri e dell' RegexCompilationInfo oggetto corrispondente.It is assigned the fully qualified name that is defined by thefullnamespace
andname
parameters of its corresponding RegexCompilationInfo object.Ha un costruttore predefinito o senza parametri.It has a default (or parameterless) constructor.
In genere, il codice che crea un'istanza e usa l'espressione regolare compilata si trova in un assembly o in un'applicazione distinta dal codice che crea l'assembly.Ordinarily, the code that instantiates and uses the compiled regular expression is found in an assembly or application that is separate from the code that creates the assembly.
Poiché il CompileToAssembly metodo genera un assembly .NET Framework da una chiamata al metodo anziché usare la parola chiave della definizione di classe di un particolare linguaggio, ad esempio class
in C# o Class
... End Class
in Visual Basic, non consente l'assegnazione di .NET Framework attributi all'assembly usando la sintassi dell'attributo standard del linguaggio di sviluppo.Because the CompileToAssembly method generates a .NET Framework assembly from a method call instead of using a particular language's class definition keyword (such as class
in C# or Class
...End Class
in Visual Basic), it does not allow .NET Framework attributes to be assigned to the assembly by using the development language's standard attribute syntax. Il attributes
parametro fornisce un metodo alternativo per la definizione degli attributi applicabili all'assembly.The attributes
parameter provides an alternative method for defining the attributes that apply to the assembly. Per ogni attributo che si desidera applicare all'assembly, eseguire le operazioni seguenti:For each attribute that you want to apply to the assembly, do the following:
Creare una matrice di Type oggetti che rappresentano i tipi di parametro del costruttore di attributo che si desidera chiamare.Create an array of Type objects representing the parameter types of the attribute constructor that you want to call.
Recuperare un Type oggetto che rappresenta la classe dell'attributo che si desidera applicare al nuovo assembly.Retrieve a Type object representing the attribute class that you want to apply to the new assembly.
Chiamare il GetConstructor metodo dell'oggetto attributo Type per recuperare un ConstructorInfo oggetto che rappresenta il costruttore dell'attributo che si desidera chiamare.Call the GetConstructor method of the attribute Type object to retrieve a ConstructorInfo object representing the attribute constructor that you want to call. Passare al GetConstructor Metodo la matrice di Type oggetti che rappresenta i tipi di parametro del costruttore.Pass the GetConstructor method the array of Type objects that represents the constructor's parameter types.
Creare una Object matrice che definisce i parametri da passare al costruttore dell'attributo.Create a Object array that defines the parameters to pass to the attribute's constructor.
Creare un'istanza CustomAttributeBuilder di un oggetto passando il relativo costruttore l' ConstructorInfo oggetto recuperato nel passaggio 3 e la Object matrice creata nel passaggio 4.Instantiate a CustomAttributeBuilder object by passing its constructor the ConstructorInfo object retrieved in step 3 and the Object array created in step 4.
È quindi possibile passare una matrice di questi CustomAttributeBuilder oggetti anziché il attributes
parametro al Regex.CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) metodo.You can then pass an array of these CustomAttributeBuilder objects instead of the attributes
parameter to the Regex.CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) method.
Note per i chiamanti
Se si sta sviluppando in un sistema in cui sono .NET Framework 4.5.NET Framework 4.5 installati o le versioni intermedie, è necessario .NET Framework 4.NET Framework 4 utilizzare il CompileToAssembly(RegexCompilationInfo[], AssemblyName) metodo per creare un assembly contenente espressioni regolari compilate.If you are developing on a system that has .NET Framework 4.5.NET Framework 4.5 or its point releases installed, you target .NET Framework 4.NET Framework 4, and you use the CompileToAssembly(RegexCompilationInfo[], AssemblyName) method to create an assembly that contains compiled regular expressions. Il tentativo di usare una delle espressioni regolari in tale assembly in un sistema che ha .NET Framework 4.NET Framework 4 generato un'eccezione.Trying to use one of the regular expressions in that assembly on a system that has .NET Framework 4.NET Framework 4 throws an exception. Per risolvere il problema, è possibile eseguire una delle operazioni seguenti:To work around this problem, you can do either of the following: -Compila l'assembly che contiene le espressioni regolari compilate in un sistema in cui è .NET Framework 4.NET Framework 4 installato anziché versioni successive.- Build the assembly that contains the compiled regular expressions on a system that has .NET Framework 4.NET Framework 4 instead of later versions installed.
-Anziché chiamare CompileToAssembly(RegexCompilationInfo[], AssemblyName) e recuperare l'espressione regolare compilata da un assembly, usare i metodi statici o Regex di istanza con l' Compiled opzione quando si crea un'istanza di un oggetto o si chiama un metodo di criteri di ricerca di Regex espressioni regolari.- Instead of calling CompileToAssembly(RegexCompilationInfo[], AssemblyName) and retrieving the compiled regular expression from an assembly, use either static or instance Regex methods with the Compiled option when you instantiate a Regex object or call a regular expression pattern matching method.
Vedi anche
Si applica a
CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)
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);
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
Public Shared Sub CompileToAssembly (regexinfos As RegexCompilationInfo(), assemblyname As AssemblyName, attributes As CustomAttributeBuilder(), resourceFile As String)
Parametri
- regexinfos
- RegexCompilationInfo[]
Matrice che descrive le espressioni regolari da compilare.An array that describes the regular expressions to compile.
- assemblyname
- AssemblyName
Nome file dell'assembly.The file name of the assembly.
- attributes
- CustomAttributeBuilder[]
Matrice che definisce gli attributi da applicare all'assembly.An array that defines the attributes to apply to the assembly.
- resourceFile
- String
Nome del file di risorse Win32 da includere nell'assembly.The name of the Win32 resource file to include in the assembly.
Eccezioni
Il valore della proprietà Name del parametro assemblyname
è una stringa vuota o null.The value of the assemblyname
parameter's Name property is an empty or null string.
-oppure--or-
Il criterio di ricerca di espressioni regolari di uno o più oggetti in regexinfos
contiene una sintassi non valida.The regular expression pattern of one or more objects in regexinfos
contains invalid syntax.
assemblyname
o regexinfos
è null
.assemblyname
or regexinfos
is null
.
Il parametro resourceFile
definisce un file di risorse Win32 non valido.The resourceFile
parameter designates an invalid Win32 resource file.
Impossibile trovare il file designato dal parametro resourceFile
.The file designated by the resourceFile
parameter cannot be found.
Solo .NET 5+ e .NET Core: la creazione di un assembly di espressioni regolari compilate non è supportata..NET 5+ and .NET Core only: Creating an assembly of compiled regular expressions is not supported.
Commenti
Il CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) metodo genera un assembly .NET Framework in cui ogni espressione regolare definita nella regexinfos
matrice è rappresentata da una classe.The CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) method generates a .NET Framework assembly in which each regular expression defined in the regexinfos
array is represented by a class. Il CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) metodo viene in genere chiamato da un'applicazione separata che genera un assembly di espressioni regolari compilate.Typically, the CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) method is called from a separate application that generates an assembly of compiled regular expressions. Ogni espressione regolare inclusa nell'assembly presenta le caratteristiche seguenti:Each regular expression included in the assembly has the following characteristics:
Viene derivato dalla Regex classe.It is derived from the Regex class.
Viene assegnato il nome completo definito dai
fullnamespace
name
parametri e dell' RegexCompilationInfo oggetto corrispondente.It is assigned the fully qualified name that is defined by thefullnamespace
andname
parameters of its corresponding RegexCompilationInfo object.Ha un costruttore predefinito o senza parametri.It has a default (or parameterless) constructor.
In genere, il codice che crea un'istanza e usa l'espressione regolare compilata si trova in un assembly o in un'applicazione distinta dal codice che crea l'assembly.Ordinarily, the code that instantiates and uses the compiled regular expression is found in an assembly or application that is separate from the code that creates the assembly.
Poiché il CompileToAssembly metodo genera un assembly .NET Framework da una chiamata al metodo anziché usare la parola chiave della definizione di classe di un particolare linguaggio, ad esempio class
in C# o Class
... End Class
in Visual Basic, non consente l'assegnazione di .NET Framework attributi all'assembly usando la sintassi dell'attributo standard del linguaggio di sviluppo.Because the CompileToAssembly method generates a .NET Framework assembly from a method call instead of using a particular language's class definition keyword (such as class
in C# or Class
...End Class
in Visual Basic), it does not allow .NET Framework attributes to be assigned to the assembly by using the development language's standard attribute syntax. Il attributes
parametro fornisce un metodo alternativo per la definizione degli attributi applicabili all'assembly.The attributes
parameter provides an alternative method for defining the attributes that apply to the assembly. Per ogni attributo che si desidera applicare all'assembly, eseguire le operazioni seguenti:For each attribute that you want to apply to the assembly, do the following:
Creare una matrice di Type oggetti che rappresentano i tipi di parametro del costruttore di attributo che si desidera chiamare.Create an array of Type objects representing the parameter types of the attribute constructor that you want to call.
Recuperare un Type oggetto che rappresenta la classe dell'attributo che si desidera applicare al nuovo assembly.Retrieve a Type object representing the attribute class that you want to apply to the new assembly.
Chiamare il GetConstructor metodo dell'oggetto attributo Type per recuperare un ConstructorInfo oggetto che rappresenta il costruttore dell'attributo che si desidera chiamare.Call the GetConstructor method of the attribute Type object to retrieve a ConstructorInfo object representing the attribute constructor that you want to call. Passare il GetConstructor metodo alla matrice di Type oggetti che rappresenta i tipi di parametro del costruttorePass the GetConstructor method the array of Type objects that represents the constructor's parameter types
Creare una Object matrice che definisce i parametri da passare al costruttore dell'attributo.Create a Object array that defines the parameters to pass to the attribute's constructor.
Creare un'istanza CustomAttributeBuilder di un oggetto passando il relativo costruttore l' ConstructorInfo oggetto recuperato nel passaggio 3 e la Object matrice creata nel passaggio 4.Instantiate a CustomAttributeBuilder object by passing its constructor the ConstructorInfo object retrieved in step 3 and the Object array created in step 4.
È quindi possibile passare una matrice di questi CustomAttributeBuilder oggetti anziché il attributes
parametro al CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) metodo.You can then pass an array of these CustomAttributeBuilder objects instead of the attributes
parameter to the CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) method.
Note per i chiamanti
Se si sta sviluppando in un sistema in cui sono .NET Framework 4.5.NET Framework 4.5 installati o le versioni intermedie, è necessario .NET Framework 4.NET Framework 4 utilizzare il CompileToAssembly(RegexCompilationInfo[], AssemblyName) metodo per creare un assembly contenente espressioni regolari compilate.If you are developing on a system that has .NET Framework 4.5.NET Framework 4.5 or its point releases installed, you target .NET Framework 4.NET Framework 4, and you use the CompileToAssembly(RegexCompilationInfo[], AssemblyName) method to create an assembly that contains compiled regular expressions. Il tentativo di usare una delle espressioni regolari in tale assembly in un sistema che ha .NET Framework 4.NET Framework 4 generato un'eccezione.Trying to use one of the regular expressions in that assembly on a system that has .NET Framework 4.NET Framework 4 throws an exception. Per risolvere il problema, è possibile eseguire una delle operazioni seguenti:To work around this problem, you can do either of the following: -Compila l'assembly che contiene le espressioni regolari compilate in un sistema in cui è .NET Framework 4.NET Framework 4 installato anziché versioni successive.- Build the assembly that contains the compiled regular expressions on a system that has .NET Framework 4.NET Framework 4 instead of later versions installed.
-Anziché chiamare CompileToAssembly(RegexCompilationInfo[], AssemblyName) e recuperare l'espressione regolare compilata da un assembly, usare i metodi statici o Regex di istanza con l' Compiled opzione quando si crea un'istanza di un oggetto o si chiama un metodo di criteri di ricerca di Regex espressioni regolari.- Instead of calling CompileToAssembly(RegexCompilationInfo[], AssemblyName) and retrieving the compiled regular expression from an assembly, use either static or instance Regex methods with the Compiled option when you instantiate a Regex object or call a regular expression pattern matching method.