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

Text template custom host: how to implement ResolveDirectiveProcessor

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

CustomCmdLineHostclass实现了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" 到 return 的类型是什么?

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

看来唯一的办法就是创建那个类型。如何 ?通过创建一个继承自 DirectiveProcessor 抽象 class 的 class(我们称之为 FallbackT4VSHostProcessor),它存在于 Microsoft.VisualStudio.TextTemplating 命名空间中(我在互联网 Here)。然后我们需要 return ResolveDirectiveProcessorFallbackT4VSHostProcessor 的类型,如下所示:

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

我希望有一天这会对某人有所帮助。