简体   繁体   中英

Translation TT file from Vb.net to C#

I have the following code in VB.NET

<#@ template debug="false" hostspecific="true" language="VB" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ output extension=".vb" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Xml.Linq" #>
<#

Dim inputFile= Path.ChangeExtension(Host.TemplateFile, "resx")
Dim xml As XElement = XElement.Load(Host.ResolvePath(inputFile))
Dim dataElements = xml.Elements().Where(Function(x) x.Name = "data")
Dim resourceTypeName = Path.GetFileNameWithoutExtension(inputFile)
#>

' This file is autogenerated
Public Class <#= resourceTypeName #>Constants

<# For Each d as XElement in dataElements #>
 Public Const <#= d.@name #> As String = "<#= d.@name #>"
<# Next #>

End Class

I made a rewrite to C#. But i forgot something i think. Because i get an error ( ErrorGeneratingOutput )

<#@ template language="C#" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Xml.Linq" #>
<#
dynamic inputFile = Path.ChangeExtension(Host.TemplateFile, "resx");
XElement xml = XElement.Load(Host.ResolvePath(inputFile));
dynamic dataElements = xml.Elements().Where(x => x.Name == "data");
dynamic resourceTypeName = Path.GetFileNameWithoutExtension(inputFile);
#>

'This file is autogenerated

public class <# resourceTypeName #>Contstants
{
foreach (XElement d in dataElements) {
    public Const String  <#= d.@name #> = "<#= d.@name #>";
}
}

Somebody who can help me?

Update

I get some error on line 21, this is the line

public class <# resourceTypeName #>Contstants 

He give as error Compiling transformation: ; expected

Change 'This file is autogenerated to //This file is autogenerated .

Additionally, the foreach in the first version is a TT foreach not a VB one. Try this:

<# foreach (XElement d in dataElements) {#>
    public const string  <#= d.@name #> = "<#= d.@name #>";
<# } #>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM