次の方法で共有


DirectiveProcessor.ProcessDirective メソッド

派生クラスでオーバーライドされると、テンプレート ファイルから 1 つのディレクティブを処理します。

名前空間:  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
    処理するディレクティブの名前。

解説

1 つのディレクティブ プロセッサで、多くの異なるディレクティブをサポートできます。 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

その他の技術情報

カスタム テキスト テンプレート ディレクティブ プロセッサの作成

チュートリアル: カスタム ディレクティブ プロセッサの作成