Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
1 out of 1 rated this helpful - Rate this topic

Regex.CompileToAssembly Method (RegexCompilationInfo[], AssemblyName)

Compiles one or more specified Regex objects to a named assembly.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public static void CompileToAssembly(
	RegexCompilationInfo[] regexinfos,
	AssemblyName assemblyname
)

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.

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.

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. Typically, the CompileToAssembly(RegexCompilationInfo[], AssemblyName) 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.

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 following example creates an assembly named RegexLib.dll. 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.

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);                                                 
   }
}

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 jumped 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 jumped over the lazy dog dog.'

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.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, 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.

Show:
© 2014 Microsoft. All rights reserved.