UnitOfWorkCodeScript.tt 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <#@ template language="C#" debug="True" #>
  2. <#@ output extension="cs" #>
  3. <#@ Assembly Name="System.Core" #>
  4. <#@ Assembly Name="$(SolutionDir)\T4\bin\Debug\T4.dll" #>
  5. <#@ import Namespace="System.IO" #>
  6. <#@ Import Namespace="System.Linq" #>
  7. <#@ Import Namespace="System.Text" #>
  8. <#@ import Namespace="System.Reflection" #>
  9. <#@ Import Namespace="System.Collections.Generic" #>
  10. <#@ Import Namespace="T4" #>
  11. <#@ include file="T4Toolbox.tt" #>
  12. <#@ include file="..\..\T4\Include\UnitOfWorkTemplate.ttinclude" #>
  13. <#
  14. // <copyright file="BllCodeScript.tt" company="Yue@China">
  15. // Copyright © . All Rights Reserved.
  16. // </copyright>
  17. //System.Diagnostics.Debugger.Launch(); //模版调试第一个断点
  18. //System.Diagnostics.Debugger.Break(); //模版调试断点
  19. string currentPath=Path.GetDirectoryName(Host.TemplateFile);
  20. string projectPath =currentPath.Substring(0, currentPath.IndexOf(@"\T4"));
  21. string solutionPath = currentPath.Substring(0, currentPath.IndexOf(@"\YZXYH2017"));
  22. string modelFile= Path.Combine(solutionPath, @"YZXYH2017\YZXYH.Repository\bin\Debug\YZXYH.Repository.dll");
  23. byte[] fileData= File.ReadAllBytes(modelFile);
  24. Assembly assembly=Assembly.Load(fileData);
  25. Type[] allModelTypes=assembly.GetTypes();
  26. Func<System.Attribute[], bool> IsSerial = o =>
  27. {
  28. foreach (System.Attribute a in o)
  29. {
  30. if (a is SerializableAttribute)
  31. return true;
  32. }
  33. return false;
  34. };
  35. Type[] modelTypes=allModelTypes.Where(o=>
  36. {
  37. return IsSerial(System.Attribute.GetCustomAttributes(o, true));
  38. }).ToArray();
  39. List<T4ModelInfo> modelList= new List<T4ModelInfo>();
  40. foreach(Type modelType in modelTypes)
  41. {
  42. T4ModelInfo model = new T4ModelInfo(modelType);
  43. modelList.Add(model);
  44. }
  45. T4ModelInfo[] models=modelList.ToArray();;
  46. //实体映射类
  47. UnitOfWorkTemplate config = new UnitOfWorkTemplate(models);
  48. string path = string.Format(@"{0}", projectPath);
  49. config.Output.Encoding = Encoding.UTF8;
  50. config.RenderToFile(Path.Combine(path, config.FileName));
  51. #>