c#-4.0 - 如何更改 T4 模板以将 Serializable 添加到生成的类?
问题描述
正如问题所说,我想将 [Serializable] 添加到从 T4 模板 .tt 文件生成的类中。就我发现的信息而言,我可以使用 System.Runtime.Serialization;和 [Serializable] 作为 .tt 文件中的纯文本作为“文本块”。除了它不会在输出文件中生成这些。
<#
EndNamespace(code);
}
foreach (var complex in typeMapper.GetItemsToGenerate<ComplexType>(itemCollection))
{
fileManager.StartNewFile(complex.Name + ".cs");
BeginNamespace(code);
#>
<#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#>
using System.Runtime.Serialization;
[Serializable]
<#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>
{
<#
var complexProperties = typeMapper.GetComplexProperties(complex);
var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(complex);
if (propertiesWithDefaultValues.Any() || complexProperties.Any())
T4 模板代码
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Projectnamespace.Models
{
using System;
using System.Collections.Generic;
public partial class Customer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
生成的代码
我很想知道这是否有特殊的语法,或者我必须改变什么才能让它工作。
我正在为数据库使用 Visual Studio 2019、.NET Framework 4.7.2、一个 MVC 5 项目和一个 .edmx 文件。
解决方案
好吧..最终我找到了一种方法来添加“使用 System.Runtime.Serialization;” 通过在 T4 模板中的 UsingDirectives 方法中添加一些行,在每个类中添加“[Serializable]”。通过改变这个:
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"{2}",
inHeader ? Environment.NewLine : "",
includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
inHeader ? "" : Environment.NewLine)
: "";
}
通过将最后一行编辑为:
public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"{2}",
inHeader ? Environment.NewLine : "",
includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
inHeader ? "" : Environment.NewLine + "using System.Runtime.Serialization;" + Environment.NewLine + "[Serializable]")
: "";
}
推荐阅读
- python - 如何查找句子中是否提到了主题?-nlp
- terraform - 读取包含 Terraform 中变量的外部文件
- mysql - 如何检查 JSON 数组值是否包含数字列表和返回长度
- arrays - 从一列值构建一个数组
- apache-spark - Pyspark - AWS EMR:作业因阶段失败而中止:ShuffleMapStage - 无法连接到 ip-XXX-XX-XX-XX.ec2.internal/XXX.XX.XX.XX:XXXXX
- c - 如何在 2 个不同的函数中使用 fopen()
- r - 细分面板数据以应用功能
- node.js - 如何覆盖 Heroku 上的 nodemobules 文件?
- python - 如何将作为函数列表的输出传递到可以在其他函数中使用的变量中?
- python - 除非我停止并重新启动程序,否则我无法选择最近的 INSERT 到我的数据库