DirectiveProcessor.ProcessDirective 메서드

파생 클래스에서 재정의된 경우 템플릿 파일로부터 단일 지시문을 처리합니다.

네임스페이스:  Microsoft.VisualStudio.TextTemplating
어셈블리:  Microsoft.VisualStudio.TextTemplating.10.0(Microsoft.VisualStudio.TextTemplating.10.0.dll)

구문

‘선언
Public MustOverride Sub ProcessDirective ( _
    directiveName As String, _
    arguments As IDictionary(Of String, String) _
)
public abstract void ProcessDirective(
    string directiveName,
    IDictionary<string, string> arguments
)
public:
virtual void ProcessDirective(
    String^ directiveName, 
    IDictionary<String^, String^>^ arguments
) abstract
abstract ProcessDirective : 
        directiveName:string * 
        arguments:IDictionary<string, string> -> unit 
public abstract function ProcessDirective(
    directiveName : String, 
    arguments : IDictionary<String, String>
)

매개 변수

  • directiveName
    형식: System.String
    처리할 지시문의 이름입니다.

설명

한 지시문 프로세서가 많은 다른 지시문을 지원할 수 있습니다. ProcessDirective가 호출될 때 조건문은 호출되는 특정 지시문만 실행합니다.

지시문은 인수를 처리하고 생성된 변환 클래스에 추가할 코드를 생성합니다.

예제

다음 코드 예제에서는 사용자 지정 지시문 처리기에 대한 구현 방법을 보여 줍니다. 이 코드 예제는 DirectiveProcessor 클래스에 대해 제공되는 보다 큰 예제의 일부입니다.

public override void ProcessDirective(string directiveName, IDictionary<string, string> arguments)
{
    if (string.Compare(directiveName, "CoolDirective", StringComparison.OrdinalIgnoreCase) == 0)
    {
        string fileName;

        if (!arguments.TryGetValue("FileName", out fileName))
        {
            throw new DirectiveProcessorException("Required argument 'FileName' not specified.");
        }

        if (string.IsNullOrEmpty(fileName))
        {
            throw new DirectiveProcessorException("Argument 'FileName' is null or empty.");
        }

        //Now we add code to the generated transformation class.
        //This directive supports either Visual Basic or C#, so we must use the
        //System.CodeDom to create the code.
        //If a directive supports only one language, you can hard code the code.
        //--------------------------------------------------------------------------
        
        CodeMemberField documentField = new CodeMemberField();

        documentField.Name = "document" + directiveCount + "Value";
        documentField.Type = new CodeTypeReference(typeof(XmlDocument));
        documentField.Attributes = MemberAttributes.Private;

        CodeMemberProperty documentProperty = new CodeMemberProperty();

        documentProperty.Name = "Document" + directiveCount;
        documentProperty.Type = new CodeTypeReference(typeof(XmlDocument));
        documentProperty.Attributes = MemberAttributes.Public;
        documentProperty.HasSet = false;
        documentProperty.HasGet = true;

        CodeExpression fieldName = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), documentField.Name);
        CodeExpression booleanTest = new CodeBinaryOperatorExpression(fieldName, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null));
        CodeExpression rightSide = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("XmlReaderHelper"), "ReadXml", new CodePrimitiveExpression(fileName));
        CodeStatement[] thenSteps = new CodeStatement[] { new CodeAssignStatement(fieldName, rightSide) };

        CodeConditionStatement ifThen = new CodeConditionStatement(booleanTest, thenSteps);
        documentProperty.GetStatements.Add(ifThen);

        CodeStatement s = new CodeMethodReturnStatement(fieldName);
        documentProperty.GetStatements.Add(s);

        CodeGeneratorOptions options = new CodeGeneratorOptions();
        options.BlankLinesBetweenMembers = true;
        options.IndentString = "    ";
        options.VerbatimOrder = true;
        options.BracingStyle = "C";

        using (StringWriter writer = new StringWriter(codeBuffer, CultureInfo.InvariantCulture))
        {
            codeDomProvider.GenerateCodeFromMember(documentField, writer, options);
            codeDomProvider.GenerateCodeFromMember(documentProperty, writer, options);
        }

    }//end CoolDirective


    //One directive processor can contain many directives.
    //If you want to support more directives, the code goes here...
    //-----------------------------------------------------------------
    if (string.Compare(directiveName, "supercooldirective", StringComparison.OrdinalIgnoreCase) == 0)
    {
        //code for SuperCoolDirective goes here...
    }//end SuperCoolDirective


    //Track how many times the processor has been called.
    //-----------------------------------------------------------------
    directiveCount++;

}//end ProcessDirective
Public Overrides Sub ProcessDirective(ByVal directiveName As String, ByVal arguments As IDictionary(Of String, String))

    If String.Compare(directiveName, "CoolDirective", StringComparison.OrdinalIgnoreCase) = 0 Then

        Dim fileName As String

        If Not (arguments.TryGetValue("FileName", fileName)) Then
            Throw New DirectiveProcessorException("Required argument 'FileName' not specified.")
        End If

        If String.IsNullOrEmpty(fileName) Then
            Throw New DirectiveProcessorException("Argument 'FileName' is null or empty.")
        End If

        'Now we add code to the generated transformation class.
        'This directive supports either Visual Basic or C#, so we must use the
        'System.CodeDom to create the code.
        'If a directive supports only one language, you can hard code the code.
        '--------------------------------------------------------------------------

        Dim documentField As CodeMemberField = New CodeMemberField()

        documentField.Name = "document" & directiveCount & "Value"
        documentField.Type = New CodeTypeReference(GetType(XmlDocument))
        documentField.Attributes = MemberAttributes.Private

        Dim documentProperty As CodeMemberProperty = New CodeMemberProperty()

        documentProperty.Name = "Document" & directiveCount
        documentProperty.Type = New CodeTypeReference(GetType(XmlDocument))
        documentProperty.Attributes = MemberAttributes.Public
        documentProperty.HasSet = False
        documentProperty.HasGet = True

        Dim fieldName As CodeExpression = New CodeFieldReferenceExpression(New CodeThisReferenceExpression(), documentField.Name)
        Dim booleanTest As CodeExpression = New CodeBinaryOperatorExpression(fieldName, CodeBinaryOperatorType.IdentityEquality, New CodePrimitiveExpression(Nothing))
        Dim rightSide As CodeExpression = New CodeMethodInvokeExpression(New CodeTypeReferenceExpression("XmlReaderHelper"), "ReadXml", New CodePrimitiveExpression(fileName))
        Dim thenSteps As CodeStatement() = New CodeStatement() {New CodeAssignStatement(fieldName, rightSide)}

        Dim ifThen As CodeConditionStatement = New CodeConditionStatement(booleanTest, thenSteps)
        documentProperty.GetStatements.Add(ifThen)

        Dim s As CodeStatement = New CodeMethodReturnStatement(fieldName)
        documentProperty.GetStatements.Add(s)

        Dim options As CodeGeneratorOptions = New CodeGeneratorOptions()
        options.BlankLinesBetweenMembers = True
        options.IndentString = "    "
        options.VerbatimOrder = True
        options.BracingStyle = "VB"

        Using writer As StringWriter = New StringWriter(codeBuffer, CultureInfo.InvariantCulture)

            codeDomProvider.GenerateCodeFromMember(documentField, writer, options)
            codeDomProvider.GenerateCodeFromMember(documentProperty, writer, options)
        End Using

    End If  'CoolDirective


    'One directive processor can contain many directives.
    'If you want to support more directives, the code goes here...
    '-----------------------------------------------------------------
    If String.Compare(directiveName, "supercooldirective", StringComparison.OrdinalIgnoreCase) = 0 Then

        'code for SuperCoolDirective goes here
    End If 'SuperCoolDirective

    'Track how many times the processor has been called.
    '-----------------------------------------------------------------
    directiveCount += 1
End Sub 'ProcessDirective

.NET Framework 보안

  • 직접 실행 호출자의 경우 완전히 신뢰합니다. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다. 자세한 내용은 부분 신뢰 코드에서 라이브러리 사용을 참조하십시오.

참고 항목

참조

DirectiveProcessor 클래스

Microsoft.VisualStudio.TextTemplating 네임스페이스

IsDirectiveSupported

ProcessDirective

기타 리소스

사용자 지정 텍스트 템플릿 지시문 프로세서 만들기

연습: 사용자 지정 지시문 프로세서 만들기