Export (0) Print
Expand All

CodeRegionDirective Class

Specifies the name and mode for a code region.

Namespace:  System.CodeDom
Assembly:  System (in System.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class CodeRegionDirective : CodeDirective

The CodeRegionDirective type exposes the following members.

  NameDescription
Public methodCodeRegionDirective()Initializes a new instance of the CodeRegionDirective class with default values.
Public methodCodeRegionDirective(CodeRegionMode, String)Initializes a new instance of the CodeRegionDirective class, specifying its mode and name.
Top

  NameDescription
Public propertyRegionModeGets or sets the mode for the region directive.
Public propertyRegionTextGets or sets the name of the region.
Public propertyUserDataGets the user-definable data for the current object. (Inherited from CodeObject.)
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The RegionMode property specifies whether an instance represents the start or end of the region.

NoteNote

Not all compilers support code region directives. To prevent compiler errors, code providers normally do not include code region directives in the provider output for compilers that do not support them.

The following code example shows the use of the CodeRegionDirective in the creation of a graph that is used to produce code that is also compiled.

using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Globalization;
namespace System.CodeDom
{
    class CodeDirectiveDemo
    {
        static void Main()
        {
            try
            {
                DemonstrateCodeDirectives("cs", "ChecksumPragma.cs", "ChecksumPragmaCS.exe");
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected Exception:" + e.ToString());
            }
        }

        // Create and compile code containing code directives. 
        static void DemonstrateCodeDirectives(string providerName, string sourceFileName, string assemblyName)
        {

            CodeDomProvider provider = CodeDomProvider.CreateProvider(providerName);

            Console.WriteLine("Building the CodeDOM graph...");

            CodeCompileUnit cu = new CodeCompileUnit();

            CreateGraph(cu);

            StringWriter sw = new StringWriter();

            Console.WriteLine("Generating code...");
            provider.GenerateCodeFromCompileUnit(cu, sw, null);

            string output = sw.ToString();
            output = Regex.Replace(output, "Runtime Version:[^\r\n]*",
                "Runtime Version omitted for demo");

            Console.WriteLine("Dumping source code...");
            Console.WriteLine(output);

            Console.WriteLine("Writing source code to file...");
            Stream s = File.Open(sourceFileName, FileMode.Create);
            StreamWriter t = new StreamWriter(s);
            t.Write(output);
            t.Close();
            s.Close();

            CompilerParameters opt = new CompilerParameters(new string[]{
                                      "System.dll", 
                                      "System.Xml.dll",
                                      "System.Windows.Forms.dll",
                                      "System.Data.dll",
                                      "System.Drawing.dll"});
            opt.GenerateExecutable = false;
            opt.TreatWarningsAsErrors = true;
            opt.IncludeDebugInformation = true;
            opt.GenerateInMemory = true;

            CompilerResults results;

            Console.WriteLine("Compiling with " + providerName);
            results = provider.CompileAssemblyFromFile(opt, sourceFileName);

            OutputResults(results);
            if (results.NativeCompilerReturnValue != 0)
            {
                Console.WriteLine("");
                Console.WriteLine("Compilation failed.");
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine("Demo complete.");
            }
            File.Delete(sourceFileName);
        }

        private static Guid HashMD5 = new Guid(0x406ea660, 0x64cf, 0x4c82, 0xb6, 0xf0, 0x42, 0xd4, 0x81, 0x72, 0xa7, 0x99);
        private static Guid HashSHA1 = new Guid(0xff1816ec, 0xaa5e, 0x4d10, 0x87, 0xf7, 0x6f, 0x49, 0x63, 0x83, 0x34, 0x60);

        // Create a CodeDOM graph. 
        static void CreateGraph( CodeCompileUnit cu)
        {
            cu.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start,
                "Compile Unit Region"));
            cu.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End,
                string.Empty));
            CodeChecksumPragma pragma1 = new CodeChecksumPragma();
            pragma1.FileName = "c:\\temp\\test\\OuterLinePragma.txt";
            pragma1.ChecksumAlgorithmId = HashMD5;
            pragma1.ChecksumData = new byte[] { 0xAA, 0xAA };
            cu.StartDirectives.Add(pragma1);
            CodeChecksumPragma pragma2 = new CodeChecksumPragma("test.txt", HashSHA1, new byte[] { 0xBB, 0xBB, 0xBB });
            cu.StartDirectives.Add(pragma2);

            CodeNamespace ns = new CodeNamespace("Namespace1");
            ns.Imports.Add(new CodeNamespaceImport("System"));
            ns.Imports.Add(new CodeNamespaceImport("System.IO"));
            cu.Namespaces.Add(ns);
            ns.Comments.Add(new CodeCommentStatement("Namespace Comment"));
            CodeTypeDeclaration cd = new CodeTypeDeclaration("Class1");
            ns.Types.Add(cd);

            cd.Comments.Add(new CodeCommentStatement("Outer Type Comment"));
            cd.LinePragma = new CodeLinePragma("c:\\temp\\test\\OuterLinePragma.txt", 300);

            CodeMemberMethod method1 = new CodeMemberMethod();
            method1.Name = "Method1";
            method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;


            CodeMemberMethod method2 = new CodeMemberMethod();
            method2.Name = "Method2";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method2.Comments.Add(new CodeCommentStatement("Method 2 Comment"));

            cd.Members.Add(method1);
            cd.Members.Add(method2);

            cd.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start,
                "Outer Type Region"));

            cd.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End,
                string.Empty));

            CodeMemberField field1 = new CodeMemberField(typeof(String), "field1");
            cd.Members.Add(field1);
            field1.Comments.Add(new CodeCommentStatement("Field 1 Comment"));

            CodeRegionDirective codeRegionDirective1 = new CodeRegionDirective(CodeRegionMode.Start,
                "Field Region");
            field1.StartDirectives.Add(codeRegionDirective1);
            CodeRegionDirective codeRegionDirective2 = new CodeRegionDirective(CodeRegionMode.End,
                "");
            codeRegionDirective2.RegionMode = CodeRegionMode.End;
            codeRegionDirective2.RegionText = string.Empty;
            field1.EndDirectives.Add(codeRegionDirective2);

            CodeSnippetStatement snippet1 = new CodeSnippetStatement();
            snippet1.Value = "            Console.WriteLine(field1);";

            CodeRegionDirective regionStart = new CodeRegionDirective(CodeRegionMode.End, "");
            regionStart.RegionText = "Snippet Region";
            regionStart.RegionMode = CodeRegionMode.Start;
            snippet1.StartDirectives.Add(regionStart);
            snippet1.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            // CodeStatement example
            CodeConstructor constructor1 = new CodeConstructor();
            constructor1.Attributes = (constructor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            CodeStatement codeAssignStatement1 = new CodeAssignStatement(
                                        new CodeFieldReferenceExpression(
                                            new CodeThisReferenceExpression(),
                                            "field1"),
                                        new CodePrimitiveExpression("value1"));
            codeAssignStatement1.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Statements Region"));
            cd.Members.Add(constructor1);
            codeAssignStatement1.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));
            method2.Statements.Add(codeAssignStatement1);
            method2.Statements.Add(snippet1);

        }

        static void OutputResults(CompilerResults results)
        {
            Console.WriteLine("NativeCompilerReturnValue=" +
                results.NativeCompilerReturnValue.ToString());
            foreach (string s in results.Output)
            {
                Console.WriteLine(s);
            }
        }

    }
}

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft