다음을 통해 공유


CodeMemberField.InitExpression 속성

정의

필드의 초기화 식을 가져오거나 설정합니다.

public:
 property System::CodeDom::CodeExpression ^ InitExpression { System::CodeDom::CodeExpression ^ get(); void set(System::CodeDom::CodeExpression ^ value); };
public System.CodeDom.CodeExpression InitExpression { get; set; }
member this.InitExpression : System.CodeDom.CodeExpression with get, set
Public Property InitExpression As CodeExpression

속성 값

필드의 초기화 식입니다.

예제

다음 예제에서는 사용 된 InitExpression 속성입니다.

using System;
using System.Reflection;
using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace SampleCodeDom
{
    /// <summary>
    /// This code example creates a graph using a CodeCompileUnit and
    /// generates source code for the graph using the CSharpCodeProvider.
    /// </summary>
    class Sample
    {
        /// <summary>
        /// Define the compile unit to use for code generation.
        /// </summary>
        CodeCompileUnit targetUnit;

        /// <summary>
        /// The only class in the compile unit.
        /// </summary>
        CodeTypeDeclaration targetClass;

        /// <summary>
        /// The name of the file to contain the source code.
        /// </summary>
        private const string outputFileName = "SampleCode.cs";

        /// <summary>
        /// Define the class.
        /// </summary>
        public Sample()
        {
            targetUnit = new CodeCompileUnit();
            CodeNamespace samples = new CodeNamespace("CodeDOMSample");
            samples.Imports.Add(new CodeNamespaceImport("System"));
            targetClass = new CodeTypeDeclaration("CodeDOMCreatedClass");
            targetClass.IsClass = true;
            targetClass.TypeAttributes =
                TypeAttributes.Public | TypeAttributes.Sealed;
            samples.Types.Add(targetClass);
            targetUnit.Namespaces.Add(samples);
        }

        /// <summary>
        /// Adds a field to the class.
        /// </summary>
        public void AddField()
        {

            CodeMemberField testField = new CodeMemberField();
            testField.Name = "today";
            testField.Type = new CodeTypeReference(typeof(System.DateTime));
            testField.Attributes = MemberAttributes.Private | MemberAttributes.Static;
            testField.InitExpression =
                new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.DateTime"), "Today");

            targetClass.Members.Add(testField);
        }

        /// <summary>
        /// Add a constructor to the class.
        /// </summary>
        public void AddConstructor()
        {
            // Declare the constructor
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes =
                MemberAttributes.Public | MemberAttributes.Final;

            targetClass.Members.Add(constructor);
        }

        /// <summary>
        /// Add an entry point to the class.
        /// </summary>
        public void AddEntryPoint()
        {
            CodeEntryPointMethod start = new CodeEntryPointMethod();
            CodeObjectCreateExpression objectCreate =
                new CodeObjectCreateExpression(
                new CodeTypeReference("CodeDOMCreatedClass"));
            // Add the statement:
            // "CodeDOMCreatedClass testClass =
            //     new CodeDOMCreatedClass();"
            start.Statements.Add(new CodeVariableDeclarationStatement(
                new CodeTypeReference("CodeDOMCreatedClass"), "testClass",
                objectCreate));

            // Creat the expression:
            // "testClass.ToString()"
            CodeMethodInvokeExpression toStringInvoke =
                new CodeMethodInvokeExpression(
                new CodeVariableReferenceExpression("today"), "ToString");

            // Add a System.Console.WriteLine statement with the previous
            // expression as a parameter.
            start.Statements.Add(new CodeMethodInvokeExpression(
                new CodeTypeReferenceExpression("System.Console"),
                "WriteLine", toStringInvoke));
            targetClass.Members.Add(start);
        }
        /// <summary>
        /// Generate CSharp source code from the compile unit.
        /// </summary>
        /// <param name="filename">Output file name</param>
        public void GenerateCSharpCode(string fileName)
        {
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BracingStyle = "C";
            using (StreamWriter sourceWriter = new StreamWriter(fileName))
            {
                provider.GenerateCodeFromCompileUnit(
                    targetUnit, sourceWriter, options);
            }
        }

        /// <summary>
        /// Create the CodeDOM graph and generate the code.
        /// </summary>
        static void Main()
        {
            Sample sample = new Sample();
            sample.AddField();
            sample.AddConstructor();
            sample.AddEntryPoint();
            sample.GenerateCSharpCode(outputFileName);
        }
    }
}
// A C# code generator produces the following source code for the preceeding example code:
//namespace CodeDOMSample
//{
//    using System;


//    public sealed class CodeDOMCreatedClass
//    {

//        private static System.DateTime today = System.DateTime.Today;

//        public CodeDOMCreatedClass()
//        {
//        }

//        public static void Main()
//        {
//            CodeDOMCreatedClass testClass = new CodeDOMCreatedClass();
//            System.Console.WriteLine(today.ToString());
//        }
//    }
//}
Imports System.Reflection
Imports System.IO
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp


'/ <summary>
'/ This code example creates a graph using a CodeCompileUnit and  
'/ generates source code for the graph using the CSharpCodeProvider.
'/ </summary>

Class Sample
    '/ <summary>
    '/ Define the compile unit to use for code generation. 
    '/ </summary>
    Private targetUnit As CodeCompileUnit
    
    '/ <summary>
    '/ The only class in the compile unit. 
    '/ </summary>
    Private targetClass As CodeTypeDeclaration
    
    '/ <summary>
    '/ The name of the file to contain the source code.
    '/ </summary>
    Private Const outputFileName As String = "SampleCode.vb"
    
    
    '/ <summary>
    '/ Define the class.
    '/ </summary>
    Public Sub New() 
        targetUnit = New CodeCompileUnit()
        Dim samples As New CodeNamespace("CodeDOMSample")
        samples.Imports.Add(New CodeNamespaceImport("System"))
        targetClass = New CodeTypeDeclaration("CodeDOMCreatedClass")
        targetClass.IsClass = True
        targetClass.TypeAttributes = TypeAttributes.Public Or TypeAttributes.Sealed
        samples.Types.Add(targetClass)
        targetUnit.Namespaces.Add(samples)
    
    End Sub
    
    
    '/ <summary>
    '/ Adds a field to the class.
    '/ </summary>
    Public Sub AddField()

        Dim testField As New CodeMemberField()
        testField.Name = "today"
        testField.Type = New CodeTypeReference(GetType(System.DateTime))
        testField.Attributes = MemberAttributes.Private Or MemberAttributes.Static
        testField.InitExpression = New CodeFieldReferenceExpression(New CodeTypeReferenceExpression("System.DateTime"), "Today")

        targetClass.Members.Add(testField)

    End Sub
     
    
    '/ <summary>
    '/ Add a constructor to the class.
    '/ </summary>
    Public Sub AddConstructor() 
        ' Declare the constructor
        Dim constructor As New CodeConstructor()
        constructor.Attributes = MemberAttributes.Public Or MemberAttributes.Final
        
        targetClass.Members.Add(constructor)
    
    End Sub
    
    
    '/ <summary>
    '/ Add an entry point to the class.
    '/ </summary>
    Public Sub AddEntryPoint() 
        Dim start As New CodeEntryPointMethod()
        Dim objectCreate As New CodeObjectCreateExpression(New CodeTypeReference("CodeDOMCreatedClass"))
        ' Add the statement:
        ' "CodeDOMCreatedClass testClass = 
        '     new CodeDOMCreatedClass();"
        start.Statements.Add(New CodeVariableDeclarationStatement(New CodeTypeReference("CodeDOMCreatedClass"), "testClass", objectCreate))
        
        ' Creat the expression:
        ' "testClass.ToString()"
        Dim toStringInvoke As New CodeMethodInvokeExpression(New CodeVariableReferenceExpression("today"), "ToString")
        
        ' Add a System.Console.WriteLine statement with the previous 
        ' expression as a parameter.
        start.Statements.Add(New CodeMethodInvokeExpression(New CodeTypeReferenceExpression("System.Console"), "WriteLine", toStringInvoke))
        targetClass.Members.Add(start)
    
    End Sub
    
    '/ <summary>
    '/ Generate CSharp source code from the compile unit.
    '/ </summary>
    '/ <param name="filename">Output file name</param>
    Public Sub GenerateCSharpCode(ByVal fileName As String) 
        Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VB")
        Dim options As New CodeGeneratorOptions()
        Dim sourceWriter As New StreamWriter(fileName)
        Try
            provider.GenerateCodeFromCompileUnit(targetUnit, sourceWriter, options)
        Finally
            sourceWriter.Dispose()
        End Try
    
    End Sub
    
    
    '/ <summary>
    '/ Create the CodeDOM graph and generate the code.
    '/ </summary>
    Shared Sub Main() 
        Dim sample As New Sample()
        sample.AddField()
        sample.AddConstructor()
        sample.AddEntryPoint()
        sample.GenerateCSharpCode(outputFileName)
    
    End Sub
End Class
' A Visual Basic code generator produces the following source code for the preceeding example code:
'Option Strict Off
'Option Explicit On

''Namespace CodeDOMSample

'    Public NotInheritable Class CodeDOMCreatedClass

'        Private Shared today As Date = Date.Today

'        Public Sub New()
'            MyBase.New()
'        End Sub

'        Public Shared Sub Main()
'            Dim testClass As CodeDOMCreatedClass = New CodeDOMCreatedClass
'            System.Console.WriteLine(today.ToString)
'        End Sub
'    End Class
'End Namespace

적용 대상

추가 정보