Can anonymous types be used in text templates?

I am writing a text template and have the following line of code:

Tuple<string, int, bool>[] tupleArray = new[]
    {
        new Tuple<string, int, bool>("apple", 4, true),
        new Tuple<string, int, bool>("grape", 1, false)
    };

I would like to convert this to an array of anonymous types :

var anonArray = new[]
    {
        new {Name = "apple", Diam = 4, Tasty = true},
        new {Name = "grape", Diam = 1, Tasty = false}
    };

The text template, however, although it is the only continuous function, does not allow the use of implicitly typed local variables.

Is there an easy way to get around this limitation and use anonymous types in a text template?

+3
source share
2 answers
Dictionary<string, int> set = 
  {
      { "apple", 4 },
      { "grape", 1 }
  }

This is probably about as brief as you can get.

EDIT: if you really want to use anonymous objects, you can always go with an array of bread and butter dynamic:

dynamic[] array = new dynamic[] { new { Name = "Apple", Diam = 4 }, ... }

. , T4- - intellisense.

+3

. Visual Studio 2010 anonArray , foreach, , . .


<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
<#
    var anonArray = new[] {
        new {Name = "apple", Diam = 4, Tasty = true},
        new {Name = "grape", Diam = 1, Tasty = false},
    };
#>
<# foreach ( var foo in anonArray) { #>
Hello <#= foo.Name #> of type <#= foo.GetType() #>
<# } #> 

T4 , # CodeDOM, , , , #, T4. , , ( ), .

+1

All Articles