Skip to content

Instantly share code, notes, and snippets.

@vladd
Last active Nov 4, 2017
Embed
What would you like to do?
Генерация классов при помощи T4-шаблона
// Generated code! Do not edit!
using System;
using System.Xml.Serialization;
namespace CodegenTest
{
public class Document
{
public int Tag1 { get; set; }
public string Tag2 { get; set; }
public DateTime Tag3 { get; set; }
public int Tag4 { get; set; }
public DateTime Tag5 { get; set; }
public string Tag6 { get; set; }
public double Tag7 { get; set; }
public int InnerTag11 { get; set; }
public string InnerTag12 { get; set; }
public string InnerTag13 { get; set; }
public int InnerTag14 { get; set; }
public int InnerTag15 { get; set; }
public int InnerTag16 { get; set; }
public string InnerInnerTag111 { get; set; }
public DateTime InnerInnerTag112 { get; set; }
public string InnerTag21 { get; set; }
public string InnerTag22 { get; set; }
public string InnerTag23 { get; set; }
public string InnerTag24 { get; set; }
public string InnerTag25 { get; set; }
public string InnerTag26 { get; set; }
private void AssignFromSerialized(Serialization.Document that)
{
this.Tag1 = that.Tag1.Value;
this.Tag2 = that.Tag2.Value;
this.Tag3 = that.Tag3.Value;
this.Tag4 = that.Tag4.Value;
this.Tag5 = that.Tag5.Value;
this.Tag6 = that.Tag6.Value;
this.Tag7 = that.Tag7.Value;
this.InnerTag11 = that.OuterTag1.InnerTag11.Value;
this.InnerTag12 = that.OuterTag1.InnerTag12.Value;
this.InnerTag13 = that.OuterTag1.InnerTag13.Value;
this.InnerTag14 = that.OuterTag1.InnerTag14.Value;
this.InnerTag15 = that.OuterTag1.InnerTag15.Value;
this.InnerTag16 = that.OuterTag1.InnerTag16.Value;
this.InnerInnerTag111 = that.OuterTag1.InnerOuterTag11.InnerInnerTag111.Value;
this.InnerInnerTag112 = that.OuterTag1.InnerOuterTag11.InnerInnerTag112.Value;
this.InnerTag21 = that.OuterTag2.InnerTag21.Value;
this.InnerTag22 = that.OuterTag2.InnerTag22.Value;
this.InnerTag23 = that.OuterTag2.InnerTag23.Value;
this.InnerTag24 = that.OuterTag2.InnerTag24.Value;
this.InnerTag25 = that.OuterTag2.InnerTag25.Value;
this.InnerTag26 = that.OuterTag2.InnerTag26.Value;
}
internal static Document FromSerialized(Serialization.Document sdoc)
{
var doc = new Document();
doc.AssignFromSerialized(sdoc);
return doc;
}
}
namespace Serialization
{
[XmlRoot(ElementName="document")]
class Document
{
[XmlElement(ElementName="tag1")]
public Tag1 Tag1 { get; set; }
[XmlElement(ElementName="tag2")]
public Tag2 Tag2 { get; set; }
[XmlElement(ElementName="tag3")]
public Tag3 Tag3 { get; set; }
[XmlElement(ElementName="tag4")]
public Tag4 Tag4 { get; set; }
[XmlElement(ElementName="tag5")]
public Tag5 Tag5 { get; set; }
[XmlElement(ElementName="tag6")]
public Tag6 Tag6 { get; set; }
[XmlElement(ElementName="tag7")]
public Tag7 Tag7 { get; set; }
[XmlElement(ElementName="outerTag1")]
public OuterTag1 OuterTag1 { get; set; }
[XmlElement(ElementName="outerTag2")]
public OuterTag2 OuterTag2 { get; set; }
}
class Tag1
{
[XmlAttribute]
public int Value { get; set; }
}
class Tag2
{
[XmlAttribute]
public string Value { get; set; }
}
class Tag3
{
[XmlAttribute]
public DateTime Value { get; set; }
}
class Tag4
{
[XmlAttribute]
public int Value { get; set; }
}
class Tag5
{
[XmlAttribute]
public DateTime Value { get; set; }
}
class Tag6
{
[XmlAttribute]
public string Value { get; set; }
}
class Tag7
{
[XmlAttribute]
public double Value { get; set; }
}
[XmlRoot(ElementName="outerTag1")]
class OuterTag1
{
[XmlElement(ElementName="innerTag11")]
public InnerTag11 InnerTag11 { get; set; }
[XmlElement(ElementName="innerTag12")]
public InnerTag12 InnerTag12 { get; set; }
[XmlElement(ElementName="innerTag13")]
public InnerTag13 InnerTag13 { get; set; }
[XmlElement(ElementName="innerTag14")]
public InnerTag14 InnerTag14 { get; set; }
[XmlElement(ElementName="innerTag15")]
public InnerTag15 InnerTag15 { get; set; }
[XmlElement(ElementName="innerTag16")]
public InnerTag16 InnerTag16 { get; set; }
[XmlElement(ElementName="innerOuterTag11")]
public InnerOuterTag11 InnerOuterTag11 { get; set; }
}
class InnerTag11
{
[XmlAttribute]
public int Value { get; set; }
}
class InnerTag12
{
[XmlAttribute]
public string Value { get; set; }
}
class InnerTag13
{
[XmlAttribute]
public string Value { get; set; }
}
class InnerTag14
{
[XmlAttribute]
public int Value { get; set; }
}
class InnerTag15
{
[XmlAttribute]
public int Value { get; set; }
}
class InnerTag16
{
[XmlAttribute]
public int Value { get; set; }
}
[XmlRoot(ElementName="innerOuterTag11")]
class InnerOuterTag11
{
[XmlElement(ElementName="innerInnerTag111")]
public InnerInnerTag111 InnerInnerTag111 { get; set; }
[XmlElement(ElementName="innerInnerTag112")]
public InnerInnerTag112 InnerInnerTag112 { get; set; }
}
class InnerInnerTag111
{
[XmlAttribute]
public string Value { get; set; }
}
class InnerInnerTag112
{
[XmlAttribute]
public DateTime Value { get; set; }
}
[XmlRoot(ElementName="outerTag2")]
class OuterTag2
{
[XmlElement(ElementName="innerTag21")]
public InnerTag21 InnerTag21 { get; set; }
[XmlElement(ElementName="innerTag22")]
public InnerTag22 InnerTag22 { get; set; }
[XmlElement(ElementName="innerTag23")]
public InnerTag23 InnerTag23 { get; set; }
[XmlElement(ElementName="innerTag24")]
public InnerTag24 InnerTag24 { get; set; }
[XmlElement(ElementName="innerTag25")]
public InnerTag25 InnerTag25 { get; set; }
[XmlElement(ElementName="innerTag26")]
public InnerTag26 InnerTag26 { get; set; }
}
class InnerTag21
{
[XmlAttribute]
public string Value { get; set; }
}
class InnerTag22
{
[XmlAttribute]
public string Value { get; set; }
}
class InnerTag23
{
[XmlAttribute]
public string Value { get; set; }
}
class InnerTag24
{
[XmlAttribute]
public string Value { get; set; }
}
class InnerTag25
{
[XmlAttribute]
public string Value { get; set; }
}
class InnerTag26
{
[XmlAttribute]
public string Value { get; set; }
}
}
}
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ output extension=".cs" #>
<#
var xmlpath = Host.ResolvePath("DocumentProto.xml");
XDocument xd = XDocument.Load(xmlpath);
#>
// Generated code! Do not edit!
using System;
using System.Xml.Serialization;
namespace CodegenTest
{
public class Document
{
<#
PushIndent(" ");
GenerateFlatClassProps(xd.Root);
ClearIndent();
#>
private void AssignFromSerialized(Serialization.Document that)
{
<#
PushIndent(" ");
GenerateFlatteningBody(xd.Root, "");
ClearIndent();
#>
}
internal static Document FromSerialized(Serialization.Document sdoc)
{
var doc = new Document();
doc.AssignFromSerialized(sdoc);
return doc;
}
}
namespace Serialization
{
<#
PushIndent(" ");
GenerateNestedClasses(xd.Root);
ClearIndent();
#>
}
}
<#+
void GenerateNestedClasses(XElement element)
{
var childClasses = new Queue<XElement>();
var leafClasses = new Queue<XElement>();
string className = element.Name.LocalName;
string capitalizedClassName = char.ToUpper(className[0]) + className.Substring(1);
WriteLine($"[XmlRoot(ElementName=\"{className}\")]");
WriteLine($"class {capitalizedClassName}");
WriteLine("{");
foreach (var sub in element.Elements())
{
string name = sub.Name.LocalName;
string capitalizedName = char.ToUpper(name[0]) + name.Substring(1);
if (!sub.HasAttributes) // nested class
childClasses.Enqueue(sub);
else
leafClasses.Enqueue(sub);
WriteLine($" [XmlElement(ElementName=\"{name}\")]");
WriteLine($" public {capitalizedName} {capitalizedName} {{ get; set; }}");
}
WriteLine("}");
WriteLine("");
foreach (var leaf in leafClasses)
GenerateLeafClass(leaf);
foreach (var child in childClasses)
GenerateNestedClasses(child);
}
void GenerateLeafClass(XElement element)
{
string className = element.Name.LocalName;
string capitalizedClassName = char.ToUpper(className[0]) + className.Substring(1);
string type = (string)element.Attribute("type");
WriteLine($"class {capitalizedClassName}");
WriteLine("{");
WriteLine($" [XmlAttribute]");
WriteLine($" public {type} Value {{ get; set; }}");
WriteLine("}");
WriteLine("");
}
void GenerateFlatClassProps(XElement element)
{
foreach (var sub in element.Elements())
{
if (!sub.HasAttributes) // nested
GenerateFlatClassProps(sub);
else
{
var type = (string)sub.Attribute("type");
string name = sub.Name.LocalName;
string capitalizedName = char.ToUpper(name[0]) + name.Substring(1);
WriteLine($"public {type} {capitalizedName} {{ get; set; }}");
}
}
}
void GenerateFlatteningBody(XElement element, string path)
{
string name = element.Name.LocalName;
string capitalizedName = char.ToUpper(name[0]) + name.Substring(1);
foreach (var sub in element.Elements())
{
string subName = sub.Name.LocalName;
string capitalizedSubName = char.ToUpper(subName[0]) + subName.Substring(1);
if (!sub.HasAttributes) // nested
GenerateFlatteningBody(sub, path + "." + capitalizedSubName);
else
WriteLine($"this.{capitalizedSubName} = that{path}.{capitalizedSubName}.Value;");
}
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment