如何更改 T4 模板以将可序列化添加到生成的 类?

How do I change a T4 Template to add Serializable to generated classes?

如问题所述,我想将 [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;”的方法和每个 class 中的“[Serializable]”,方法是向 T4 模板中的 UsingDirectives 方法添加一些行。 通过改变这个:

    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]")
            : "";
    }