Regex.CompileToAssembly Method (RegexCompilationInfo, AssemblyName, CustomAttributeBuilder)
Compiles one or more specified Regex objects to a named assembly with the specified attributes.
Assembly: System (in System.dll)
[<HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)>] static member CompileToAssembly : regexinfos:RegexCompilationInfo * assemblyname:AssemblyName * attributes:CustomAttributeBuilder -> unit
- Type: System.Text.RegularExpressions.RegexCompilationInfo
An array that describes the regular expressions to compile.
- Type: System.Reflection.AssemblyName
The file name of the assembly.
- Type: System.Reflection.Emit.CustomAttributeBuilder
An array that defines the attributes to apply to the assembly.
The value of the assemblyname parameter's AssemblyName.Name property is an empty or null string.
The regular expression pattern of one or more objects in regexinfos contains invalid syntax.
assemblyname or regexinfos is a null reference (Nothing in Visual Basic).
The method generates a .NET Framework assembly in which each regular expression defined in the regexinfos array is represented by a class. Typically, the method is called from a separate application that generates an assembly of compiled regular expressions. Each regular expression included in the assembly has the following characteristics:
It is derived from the Regex class.
It is assigned the fully qualified name that is defined by the fullnamespace and name parameters of its corresponding RegexCompilationInfo object.
It has a default (or parameterless) constructor.
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.
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. The attributes parameter provides an alternative method for defining the attributes that apply to the assembly. For each attribute that you want to apply to the assembly, do the following:
Create an array of Type objects representing the parameter types of the attribute constructor that you want to call.
Retrieve a Type object representing the attribute class that you want to apply to the new assembly.
Call the GetConstructor method of the attribute Type object to retrieve a ConstructorInfo object representing the attribute constructor that you want to call. Pass the GetConstructor method the array of Type objects that represents the constructor's parameter types.
Create a Object array that defines the parameters to pass to the attribute's constructor.
You can then pass an array of these CustomAttributeBuilder objects instead of the attributes parameter to the method.
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
If you are developing on a system that has .NET Framework 4.5 or its point releases installed, you target .NET Framework 4, and you use the CompileToAssembly method to create an assembly that contains compiled regular expressions. Trying to use one of the regular expressions in that assembly on a system that has .NET Framework 4 throws an exception. To work around this problem, you can do either of the following:
Build the assembly that contains the compiled regular expressions on a system that has .NET Framework 4 instead of later versions installed.
Instead of calling CompileToAssembly and retrieving the compiled regular expression from an assembly, use either static or instance Regex methods with the RegexOptions.Compiled option when you instantiate a Regex object or call a regular expression pattern matching method.
The following example creates an assembly named RegexLib.dll and applies the AssemblyTitleAttribute attribute to it. The assembly includes two compiled regular expressions. The first, Utilities.RegularExpressions.DuplicatedString, matches two identical contiguous words. The second, Utilities.RegularExpressions.EmailAddress, checks whether a string has the correct format to be an e-mail address.
You can verify that the AssemblyTitleAttribute attribute has been applied to the assembly by examining its manifest with a reflection utility such as ILDasm.
The regular expression that checks a string for duplicate words is then instantiated and used by the following example.
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.
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.