开发者

T4 text template with IList

I am using a T4 template to create DTO's for each of my Data Entities, however it is falling over on using 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);

and

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

I think the section it is falling over on is this, even though IList is an ICollection, the following is not being evaluated to true:

if (ExportProperty(property) && collectionProperties(property开发者_StackOverflow社区))

I am not sure how to debug .tt (T4) files in VS 2010.

The class being generated when a property is an IList is:

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

When it should be:

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


The link to PastBin is broken, but what I understand from the question; the problem is similar to the following question "How can I get the correct text definition of a generic type using reflection?". But if you are using Entity Framework, then you can maybe better use POCO or self tracking entities generated from the model, instead of making a translation towards DTOs.

For debugging T4 I just start with coding my logic in a separate class, which I move to the T4 file or call from it. I also installed the free T4 toolbox extension, and at first sight it is helpful, but I just started learning/using code generation. So maybe better solutions or practices exist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜