文本模板自定义主机:如何实现ResolveDirectiveProcessor

时间:2015-04-13 01:00:47

标签: c# t4

我尝试使用msdn中的这个示例来介绍如何为文本模板生成创建自定义主机。

CustomCmdLineHost类实现ITextTemplatingEngineHost接口但不完全,ResolveDirectiveProcessor未实现,并且每次正常发生异常时抛出它。这是ResolveDirectiveProcessor方法:

public Type ResolveDirectiveProcessor(string processorName)
    {
        //This host will not resolve any specific processors.
        //Check the processor name, and if it is the name of a processor the 
        //host wants to support, return the type of the processor.
        //---------------------------------------------------------------------
        if (string.Compare(processorName, "XYZ", StringComparison.OrdinalIgnoreCase) == 0)
        {
            //return typeof();
        }
        //This can be customized to search specific paths for the file
        //or to search the GAC
        //If the directive processor cannot be found, throw an error.
        throw new Exception("Directive Processor not found");
    }
传递给此函数的

processorName是“T4VSHost”,

现在的问题: 在这种方法中返回“T4VSHost”的类型是什么?

P.S。:我试过“Microsoft.Data.Entity.Design.VisualStudio.Directives.FallbackT4VSHostProcessor”,但它似乎并不存在于任何命名空间中。

1 个答案:

答案 0 :(得分:0)

似乎唯一的方法是创建该类型。怎么样 ?通过创建一个继承自FallbackT4VSHostProcessor命名空间的DirectiveProcessor抽象类的类(让我们称之为Microsoft.VisualStudio.TextTemplating)(我在互联网上找到的唯一例子是Here )。那么我们需要在FallbackT4VSHostProcessor中返回ResolveDirectiveProcessor的类型,如下所示:

Type ITextTemplatingEngineHost.ResolveDirectiveProcessor(string processorName)
    {
        if (string.Compare(processorName, "T4VSHost", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return typeof(FallbackT4VSHostProcessor);
        }
        throw new Exception("Directive Processor not found");
    }

我希望有一天能帮到某人。