Export (0) Print
Expand All

Regex.CompileToAssembly Method (RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String)

Updated: May 2009

Compiles one or more specified Regex objects and a specified resource file to a named assembly with specified attributes.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public static void CompileToAssembly(
	RegexCompilationInfo[] regexinfos,
	AssemblyName assemblyname,
	CustomAttributeBuilder[] attributes,
	string resourceFile
)

Parameters

regexinfos
Type: System.Text.RegularExpressions.RegexCompilationInfo[]

An array that describes the regular expressions to compile.

assemblyname
Type: System.Reflection.AssemblyName

The file name of the assembly.

attributes
Type: System.Reflection.Emit.CustomAttributeBuilder[]

An array that defines the attributes to apply to the assembly.

resourceFile
Type: System.String

The name of the Win32 resource file to include in the assembly.

ExceptionCondition
ArgumentException

The value of the assemblyname parameter's AssemblyName.Name property is an empty or null string.

-or-

The regular expression pattern of one or more objects in regexinfos contains invalid syntax.

ArgumentNullException

assemblyname or regexinfos is null.

COMException

The resourceFile parameter designates an invalid Win32 resource file.

FileNotFoundException

The file designated by the resourceFile parameter cannot be found.

NoteNote:

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.

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. Typically, the CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) 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 ClassEnd Class in Visual Basic), it does not allow .NET Framework attributes to be assigned to the assembly by using the development language's normal attribute syntax. The attributes parameter provides an alternate method for defining the attributes that apply to the assembly. For each attribute that you wish to apply to the assembly, do the following:

  1. Create an array of Type objects representing the parameter types of the attribute constructor that you want to call.

  2. Retrieve a Type object representing the attribute class that you want to apply to the new assembly.

  3. 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

  4. Create a Object array that defines the parameters to pass to the attribute's constructor.

  5. Instantiate a CustomAttributeBuilder object by passing its constructor the ConstructorInfo object retrieved in step 3 and the Object array created in step 4.

You can then pass an array of these CustomAttributeBuilder objects in place of the attributes parameter to the CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) method.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Date

History

Reason

May 2009

Added a Remarks section and expanded the list of exceptions.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft