CodeRegionDirective 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
코드 영역의 이름과 모드를 지정합니다.
public ref class CodeRegionDirective : System::CodeDom::CodeDirective
public class CodeRegionDirective : System.CodeDom.CodeDirective
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeRegionDirective : System.CodeDom.CodeDirective
type CodeRegionDirective = class
inherit CodeDirective
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type CodeRegionDirective = class
inherit CodeDirective
Public Class CodeRegionDirective
Inherits CodeDirective
- 상속
- 특성
예제
다음 코드 예제에서는 를 사용하여 CodeRegionDirective 컴파일된 코드를 생성하는 데 사용되는 그래프를 만드는 방법을 보여 줍니다.
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);
}
// This example uses the SHA1 and MD5 algorithms.
// Due to collision problems with SHA1 and MD5, Microsoft recommends SHA256 or better.
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);
}
}
}
}
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Collections
Imports System.Collections.Specialized
Imports System.IO
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports System.Globalization
Class CodeDirectiveDemo
Shared Sub Main()
Try
DemonstrateCodeDirectives("vb", "ChecksumPragma.vb", "ChecksumPragmaVB.exe")
Catch e As Exception
Console.WriteLine(("Unexpected exception:" + e.ToString()))
End Try
End Sub
' Create and compile code containing code directives.
Shared Sub DemonstrateCodeDirectives(ByVal providerName As String, ByVal sourceFileName As String, ByVal assemblyName As String)
Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider(providerName)
Console.WriteLine("Building the CodeDOM graph...")
Dim cu As New CodeCompileUnit()
CreateGraph(cu)
Dim sw As New StringWriter()
Console.WriteLine("Generating code...")
provider.GenerateCodeFromCompileUnit(cu, sw, Nothing)
Dim output As String = sw.ToString()
output = Regex.Replace(output, "Runtime version:[^" + vbCr + vbLf + "]*", "Runtime version omitted for this demo")
Console.WriteLine("Dumping source code...")
Console.WriteLine(output)
Console.WriteLine("Writing source code to file...")
Dim s As Stream = File.Open(sourceFileName, FileMode.Create)
Dim t As New StreamWriter(s)
t.Write(output)
t.Close()
s.Close()
Dim opt As 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
Dim results As CompilerResults
Console.WriteLine(("Compiling with " + providerName))
results = provider.CompileAssemblyFromFile(opt, sourceFileName)
OutputResults(results)
If results.NativeCompilerReturnValue <> 0 Then
Console.WriteLine("")
Console.WriteLine("Compilation failed.")
Else
Console.WriteLine("")
Console.WriteLine("Demo completed successfully.")
End If
File.Delete(sourceFileName)
End Sub
' This example uses the SHA1 and MD5 algorithms.
' Due to collision problems with SHA1 and MD5, Microsoft recommends SHA256 or better.
Private Shared HashMD5 As New Guid(&H406EA660, &H64CF, &H4C82, &HB6, &HF0, &H42, &HD4, &H81, &H72, &HA7, &H99)
Private Shared HashSHA1 As New Guid(&HFF1816EC, &H65FF, &H4D10, &H87, &HF7, &H6F, &H49, &H63, &H83, &H34, &H60)
' Create a CodeDOM graph.
Shared Sub CreateGraph(ByVal cu As CodeCompileUnit) 'ICodeGenerator generator,
cu.StartDirectives.Add(New CodeRegionDirective(CodeRegionMode.Start, "Compile Unit Region"))
cu.EndDirectives.Add(New CodeRegionDirective(CodeRegionMode.End, String.Empty))
Dim pragma1 As New CodeChecksumPragma()
pragma1.FileName = "c:\temp\test\OuterLinePragma.txt"
pragma1.ChecksumAlgorithmId = HashMD5
pragma1.ChecksumData = New Byte() {&HAA, &HAA}
cu.StartDirectives.Add(pragma1)
Dim pragma2 As New CodeChecksumPragma("test.txt", HashSHA1, New Byte() {&HBB, &HBB, &HBB})
cu.StartDirectives.Add(pragma2)
Dim ns As 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"))
Dim cd As 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)
Dim method1 As New CodeMemberMethod()
method1.Name = "Method1"
method1.Attributes = method1.Attributes And Not MemberAttributes.AccessMask Or MemberAttributes.Public
Dim method2 As New CodeMemberMethod()
method2.Name = "Method2"
method2.Attributes = method2.Attributes And Not MemberAttributes.AccessMask Or MemberAttributes.Public
method2.Comments.Add(New CodeCommentStatement("Method2 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))
Dim field1 As New CodeMemberField(GetType(String), "field1")
cd.Members.Add(field1)
field1.Comments.Add(New CodeCommentStatement("Field1 Comment"))
Dim codeRegionDirective1 As New CodeRegionDirective(CodeRegionMode.Start, "Field Region")
field1.StartDirectives.Add(codeRegionDirective1)
Dim codeRegionDirective2 As New CodeRegionDirective(CodeRegionMode.End, "")
codeRegionDirective2.RegionMode = CodeRegionMode.End
codeRegionDirective2.RegionText = String.Empty
field1.EndDirectives.Add(codeRegionDirective2)
Dim snippet1 As New CodeSnippetStatement()
snippet1.Value = " Console.WriteLine(field1)"
Dim regionStart As 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
Dim constructor1 As New CodeConstructor()
constructor1.Attributes = constructor1.Attributes And Not MemberAttributes.AccessMask _
Or MemberAttributes.Public
Dim codeAssignStatement1 As 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)
End Sub
Shared Sub OutputResults(ByVal results As CompilerResults)
Console.WriteLine(("NativeCompilerReturnValue=" + results.NativeCompilerReturnValue.ToString()))
Dim s As String
For Each s In results.Output
Console.WriteLine(s)
Next s
End Sub
End Class
설명
속성은 RegionMode instance 영역의 시작 또는 끝을 나타내는지 여부를 지정합니다.
참고
모든 컴파일러가 코드 영역 지시문을 지원하는 것은 아닙니다. 컴파일러 오류를 방지하기 위해 코드 공급자는 일반적으로 이를 지원하지 않는 컴파일러에 대한 공급자 출력에 코드 영역 지시문을 포함하지 않습니다.
생성자
CodeRegionDirective() |
기본값을 사용하여 CodeRegionDirective 클래스의 새 인스턴스를 초기화합니다. |
CodeRegionDirective(CodeRegionMode, String) |
모드와 이름을 지정하여 CodeRegionDirective 클래스의 새 인스턴스를 초기화합니다. |
속성
RegionMode |
영역 지시문의 모드를 가져오거나 설정합니다. |
RegionText |
영역의 이름을 가져오거나 설정합니다. |
UserData |
현재 개체에 대해 사용자 정의 가능한 데이터를 가져옵니다. (다음에서 상속됨 CodeObject) |
메서드
Equals(Object) |
지정된 개체가 현재 개체와 같은지 확인합니다. (다음에서 상속됨 Object) |
GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
GetType() |
현재 인스턴스의 Type을 가져옵니다. (다음에서 상속됨 Object) |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
적용 대상
.NET