IRepositoryCodeScript.tt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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\IRepositoryTemplate.ttinclude" #>
  13. <#
  14. //System.Diagnostics.Debugger.Launch(); //模版调试第一个断点
  15. //System.Diagnostics.Debugger.Break(); //模版调试断点
  16. string currentPath=Path.GetDirectoryName(Host.TemplateFile);
  17. string projectPath =currentPath.Substring(0, currentPath.IndexOf(@"\T4"));
  18. string solutionPath = currentPath.Substring(0, currentPath.IndexOf(@"\YZXYH2017"));
  19. string modelFile= Path.Combine(solutionPath, @"YZXYH2017\YZXYH.Repository\bin\Debug\YZXYH.Repository.dll");
  20. byte[] fileData= File.ReadAllBytes(modelFile);
  21. Assembly assembly=Assembly.Load(fileData);
  22. Type[] allModelTypes=assembly.GetTypes();
  23. Func<System.Attribute[], bool> IsSerial = o =>
  24. {
  25. foreach (System.Attribute a in o)
  26. {
  27. if (a is SerializableAttribute)
  28. return true;
  29. }
  30. return false;
  31. };
  32. Type[] modelTypes=allModelTypes.Where(o=>
  33. {
  34. return IsSerial(System.Attribute.GetCustomAttributes(o, true));
  35. }).ToArray();
  36. foreach(Type modelType in modelTypes)
  37. {
  38. T4ModelInfo model = new T4ModelInfo(modelType);
  39. //实体映射类
  40. IRepositoryTemplate config = new IRepositoryTemplate(model);
  41. string path = string.Format(@"{0}/Interface", projectPath);
  42. config.Output.Encoding = Encoding.UTF8;
  43. config.RenderToFile(Path.Combine(path, config.FileName));
  44. }
  45. #>