2017-09-12 7 views
0

は、私のような新しい拡張のためCodeGenerator機能からコンテキスト・コントローラのT4テンプレートのデフォルトのテンプレートパラメータに渡すパラメータMicrosoftのMVCは自動的けど以来、私はここで自分自身でそれらを渡したい自分で足場プロセスをオーバーライドしていますDLL:T4のデフォルトテンプレートが

public override void GenerateCode() 
    { 
     // Get the selected code type 
     var codeType = _viewModel.SelectedModelType.CodeType; 

     // Add the custom scaffolding item from T4 template. 
     this.AddFileFromTemplate(Context.ActiveProject, 
      "MVCBootstrapServerTable", 
      "CustomTextTemplate", 
      GetParameters(),   //to provide the parameters here! 
      skipIfExists: false); 
    } 

はこれを行う簡単な方法はありますか?

答えて

0
protected virtual IDictionary<string, object> AddTemplateParameters(CodeType dbContextType, ModelMetadata modelMetadata) 
{ 
    if (dbContextType == null) 
    throw new ArgumentNullException(nameof (dbContextType)); 
    if (modelMetadata == null) 
    throw new ArgumentNullException(nameof (modelMetadata)); 
    if (string.IsNullOrEmpty(this.Model.ControllerName)) 
    throw new InvalidOperationException(Microsoft.AspNet.Scaffolding.Mvc.Properties.Resources.InvalidControllerName); 
    IDictionary<string, object> dictionary = (IDictionary<string, object>) new Dictionary<string, object>((IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase); 
    CodeType codeType = this.Model.ModelType.CodeType; 
    dictionary.Add("ModelMetadata", (object) modelMetadata); 
    string str = codeType.Namespace != null ? codeType.Namespace.FullName : string.Empty; 
    dictionary.Add("ModelTypeNamespace", (object) str); 
    HashSet<string> requiredNamespaces = this.GetRequiredNamespaces((IEnumerable<CodeType>) new List<CodeType>() 
    { 
    codeType, 
    dbContextType 
    }); 
    dictionary.Add("RequiredNamespaces", (object) requiredNamespaces); 
    dictionary.Add("ModelTypeName", (object) codeType.Name); 
    dictionary.Add("ContextTypeName", (object) dbContextType.Name); 
    dictionary.Add("UseAsync", (object) this.Model.IsAsyncSelected); 
    string escapedIdentifier = ValidationUtil.GenerateCodeDomProvider(this.Model.ActiveProject.GetCodeLanguage()).CreateEscapedIdentifier(this.Model.ModelType.ShortTypeName.ToLowerInvariantFirstChar()); 
    dictionary.Add("ModelVariable", (object) escapedIdentifier); 
    dictionary.Add("EntitySetVariable", (object) modelMetadata.EntitySetName.ToLowerInvariantFirstChar()); 
    if (this.Model.IsViewGenerationSupported) 
    { 
    bool flag = OverpostingProtection.IsOverpostingProtectionRequired(codeType); 
    dictionary.Add("IsOverpostingProtectionRequired", (object) flag); 
    if (flag) 
    { 
     dictionary.Add("OverpostingWarningMessage", (object) OverpostingProtection.WarningMessage); 
     dictionary.Add("BindAttributeIncludeText", (object) OverpostingProtection.GetBindAttributeIncludeText(modelMetadata)); 
    } 
    } 
    return dictionary; 
} 

私の解決策に。

0

テキストテンプレートサービスを使用してテンプレートを処理できます。あなたはT4テンプレートからパラメータを取得したい場合は、あなたがITextTemplatingEngineHost.ResolveParameterValueメソッドを使用する必要が

https://msdn.microsoft.com/en-us/library/gg586944.aspx#Anchor_1

:サンプルコードでは、を参照してください。このメソッドを使用する前に、テンプレート要素にhostspecific = "true"属性を追加する必要もあります。

サンプルコード、あなたはを参照してくださいコード:私は従った正しい値を移入するために上記の値のために必要なクラスを呼び出すことによって、ロジックにはるかに深くなったので、持っている

Get argument value from TextTransform.exe into the template

+0

ありがとうウェンディ、私が欲しいのは、ControllerNameの文字列、ControllerRootName、名前空間、AreaName、ContextTypeName、ModelTypeName ....などのそれぞれの値を設定することです。 –

+0

サンプルコードでは、ITextTemplatingSessionHost.CreateSession()メソッドを使用しています。これは、キーと値のペアで値を渡すことができます。キー名をControllerName、ControllerRootName ...などに設定し、それぞれの値を設定することができます。 –

+0

私は自分自身をよく説明していないと思う。私は値を渡す方法を知っています。私が望むのは、ControllerName、ControllerRootNameなどの値を取得することです(元の投稿のパラメータのリスト全体)。 –

関連する問題