2015-04-13 41 views
0

我試圖從msdn使用此示例瞭解如何爲文本模板生成創建自定義主機。文本模板自定義主機:如何實現ResolveDirectiveProcessor

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」,但它似乎並不存在於任何名稱空間中。

+0

你是否像命令行一樣從命令行測試了這個? – Frank

+0

@Frank否它被應用程序中的其他代碼調用 – dafriskymonkey

回答

0

看來唯一的方法就是創建該類型。怎麼樣 ?通過創建一個繼承自DirectiveProcessor抽象類(位於Microsoft.VisualStudio.TextTemplating命名空間(我在互聯網中找到的唯一示例是Here))的類(讓它稱爲FallbackT4VSHostProcessor)。那麼我們需要在ResolveDirectiveProcessor這樣返回FallbackT4VSHostProcessor類型:

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

我希望這會幫助別人一天。

相關問題