【问题标题】:T4 text template with IList带有 IList 的 T4 文本模板
【发布时间】:2011-05-31 02:24:38
【问题描述】:

我正在使用 T4 template 为我的每个数据实体创建 DTO,但是使用 IList 时它失败了。

http://pastebin.com/QxtsDJdt

Func<PropertyInfo, bool> scalarProperties = p => !p.PropertyType.GetInterfaces().Any(t => t == typeof(System.Collections.IList) || t == typeof(System.Collections.ICollection));
Func<PropertyInfo, bool> collectionProperties = p => !scalarProperties.Invoke(p);

private bool ExportProperty(PropertyInfo p)
{
    return true;
}

我认为它所涉及的部分是这个,即使 IList 是一个 ICollection,以下内容也没有被评估为 true

if (ExportProperty(property) && collectionProperties(property))

我不确定如何在 VS 2010 中调试 .tt (T4) 文件。

当属性是 IList 时生成的类是:

public System.Collections.Generic.IList`1[[Namespace.Inspection, Entities, Version=1.0.4168.906, Culture=neutral, PublicKeyToken=null]] Inspections
{
    get; set;
}

什么时候应该:

    public System.Collections.Generic.IList<Namespace.Inspection> Inspections
    {
        get; set;
    }

【问题讨论】:

    标签: c# visual-studio-2010 t4 dto


    【解决方案1】:

    PastBin 的链接已损坏,但我从问题中了解到;该问题类似于以下问题“How can I get the correct text definition of a generic type using reflection?”。但是,如果您使用的是实体框架,那么您可能会更好地使用从模型生成的 POCO 或自我跟踪实体,而不是向 DTO 进行转换。

    为了调试 T4,我只是从在一个单独的类中编写我的逻辑开始,我将其移至 T4 文件或从中调用。我还安装了免费的T4 toolbox 扩展,乍一看它很有帮助,但我才刚刚开始学习/使用代码生成。因此,也许存在更好的解决方案或实践。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-11
      • 2014-11-10
      • 1970-01-01
      • 2011-04-07
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多