T4默认模板参数

时间:2017-09-12 05:08:17

标签: templates t4 visual-studio-extensions scaffolding

我想从CodeGenerator函数传递上下文控制器T4模板的默认模板参数,以获取新的扩展名:

<#@ parameter type="System.String" name="ControllerName" #>
<#@ parameter type="System.String" name="ControllerRootName" #>
<#@ parameter type="System.String" name="Namespace" #>
<#@ parameter type="System.String" name="AreaName" #>
<#@ parameter type="System.String" name="ContextTypeName" #>
<#@ parameter type="System.String" name="ModelTypeName" #>
<#@ parameter type="System.String" name="ModelVariable" #>
<#@ parameter type="Microsoft.AspNet.Scaffolding.Core.Metadata.ModelMetadata" name="ModelMetadata" #>
<#@ parameter type="System.String" name="EntitySetVariable" #>
<#@ parameter type="System.Boolean" name="UseAsync" #>
<#@ parameter type="System.Boolean" name="IsOverpostingProtectionRequired" #>
<#@ parameter type="System.String" name="BindAttributeIncludeText" #>
<#@ parameter type="System.String" name ="OverpostingWarningMessage" #>
<#@ parameter type="System.Collections.Generic.HashSet<System.String>" name="RequiredNamespaces" #>

这些是通过微软MVC dll的脚手架流程自动传递的,但由于我用我自己的脚手架过程,我想在这里传递它们:

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);
    }

有一种简单的方法吗?

2 个答案:

答案 0 :(得分:0)

您可以使用文本模板服务处理模板。示例代码,请参阅:

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

如果要从T4模板获取参数,则需要使用ITextTemplatingEngineHost.ResolveParameterValue方法。在使用此方法之前,还需要将hostspecific =“true”属性添加到模板元素。

示例代码,您的代码参考:

Get argument value from TextTransform.exe into the template

答案 1 :(得分: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;
}

我已经通过调用上面值所需的类来根据我的解决方案填充正确的值,从而深入了解逻辑。