Upgrade to Pro — share decks privately, control downloads, hide ads and more …

T4使ってみた

 T4使ってみた

2015/02/24(火) Unity部主催、日本Microsoft様で開催されたVisual Studio Tools for Unityの勉強会で行ったLTの資料です。

※もともとはSlideShareで公開していた資料です

RyotaMurohoshi

February 24, 2015
Tweet

More Decks by RyotaMurohoshi

Other Decks in Technology

Transcript

  1. DRY

  2. ࣍ͷΑ͏ͳΫϥεͷ৘ใΛParseͰอଘ͠·͢ public class Person { public string FirstName { get;

    set; } public string LastName { get; set; } public int Age { get; set; } public int Sex { get; set; } }
  3. ී௨ʹσʔλΛอଘ͢Δͱ͖ ParseObject gameScore = new ParseObject("Person") { {"firstName", "Ryota"}, {"lastName",

    "Murohoshi"}, {"age", 26}, {"sex", 0}, // உੑ }; Task saveTask = gameScore.SaveAsync(); // อଘ // ࡉ͔͍ͱ͜Ζ͸֤ࣗ(ུ)
  4. ͜Μͳײ͡ͷαϒΫϥεΛ࡞ͬͯ࢖͏ [ParseClassName("Person")] public class Person : ParseObject { [ParseFieldName("firstName")] public

    string FirstName { get { return GetProperty<string>("FirstName"); } set { SetProperty<string>(value, "FirstName"); } } /*ɹུɺଞ3ݸͷϓϩύςΟ */ } // ࡉ͔͍ͱ͜Ζ͸(ུ)
  5. T4Λ৽ن࡞੒͢Δͱ͜Μͳײ͡ <#@ template language="C#" #> <#@ assembly name="System.Core" #> <#@

    import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #>
  6. ෇͚Ճ͑Δͱɺ <#@ template language="C#" #> <#@ assembly name="System.Core" #> <#@

    import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> public class HelloT4 { }
  7. ͜Μͳײ͡Ͱॻ͘ͱɺ <#@ template language="C#" #> <#@ assembly name="System.Core" #> <#@

    import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> public class HelloT4 { <# foreach(int num in Enumerable.Range(0, 3)) { #> ͜͜ʹϑΟʔϧυΛఆ͍ٛͨ͠ʂ <# } #> }
  8. ͪΐͬͱվ଄ͯ͠ʂ <#@ template language="C#" #> <#@ assembly name="System.Core" #> <#@

    import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> public class HelloT4 { <# foreach(int num in Enumerable.Range(0, 3)) { #> public const int Num<#= num #> = <#= num #>; <# } #> }
  9. ܁Γฦ͠Λ༻͍ͨίʔυੜ੒ public class HelloT4 { public const int Num0 =

    0; public const int Num1 = 1; public const int Num2 = 2; }
  10. ͜ΕΛT4Ͱͭ͘ΔͰʂ [ParseClassName("Person")] public class Person : ParseObject { [ParseFieldName("firstName")] public

    string FirstName { get { return GetProperty<string>("FirstName"); } set { SetProperty<string>(value, "FirstName"); } } /*ɹུɺଞ3ݸͷϓϩύςΟ */ }
  11. ͜Μͳײ͡ͷςϯϓϨʔτΛ༻ҙͯ͠ʂ namespace ParseObjects { using Parse; <# foreach (var definedClass

    in definedClasses) { #> [ParseClassName("<#= definedClass.ClassName #>")] public class <#= definedClass.ClassName #> : ParseObject { <# foreach (var property in definedClass.Propeties) { string typeName = property.Type.ToString (); string fieldName = ToLowerCamel (property.Name); #> [ParseFieldName("<#= fieldName#>")] public <#= typeName #> <#= property.Name #> { get { return GetProperty<<#= typeName #> > ("<#= property.Name #>"); } set { SetProperty<<#= typeName #> > (value, "<#= property.Name #>"); } } <# } #> } <# } #> }
  12. ͜Μͳײ͡ͷݩσʔλΛఆٛͯ͠ʂ(ෳ਺ΫϥεରԠʂ) var definedClasses = new [] { new { ClassName

    = "Person", Propeties = new [] { new { Type = typeof(string), Name = "FirstName" }, new { Type = typeof(string), Name = "LastName" }, new { Type = typeof(int), Name = "Age" }, new { Type = typeof(int), Name = "Sex" }, } }, new { ClassName = "PlayLog", Propeties = new [] { new { Type = typeof(int), Name = "StageId" }, new { Type = typeof(int), Name = "Score" }, new { Type = typeof(DateTime), Name = "PlayedAt" }, } }, };
  13. Ͱ͖ͨʂ [ParseClassName("Person")] public class Person : ParseObject { [ParseFieldName("firstName")] public

    string FirstName { get { return GetProperty<string>("FirstName"); } set { SetProperty<string>(value, "FirstName"); } } /*ɹུɺଞ3ݸͷϓϩύςΟ */ }