daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest Apr 21st, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#@ template debug="true" language="C#" hostSpecific="true" #>
  2. <#@ output extension=".cs" #>
  3. <#@ assembly name="System.Core.dll" #>
  4. <#@ import namespace="System.Reflection" #>
  5. <#@ import namespace="System.IO" #>
  6. <#@ import namespace="System.Linq" #>
  7. <#
  8.   string assemblyFile = @"bin\Debug\CloningGenerator.exe";
  9.   Assembly assembly = LoadProjectAssembly(assemblyFile);
  10.     var deepCloneables = assembly.GetTypes()
  11.         .Where(x => !x.IsInterface
  12.                     && x.Name != "DeepCloneable"
  13.                     && x.GetInterfaces().Any(y => y.Name == "IDeepCloneable"));
  14. #>
  15. namespace CloningGenerator {
  16. <#
  17.     foreach (var t in deepCloneables)
  18.     {
  19. #>
  20.     public partial class <#= t.Name #>
  21.     {
  22.     }    
  23. <#
  24.     }
  25. #>
  26. }
  27.  
  28. <# Cleanup(); #>
  29. <#+
  30. private string outputDir;
  31.  
  32. private Assembly LoadProjectAssembly(string assemblyFile) {
  33.   if (!Path.IsPathRooted(assemblyFile))
  34.     assemblyFile = Path.Combine(Path.GetDirectoryName(Host.TemplateFile), assemblyFile);
  35.   outputDir = Path.GetDirectoryName(assemblyFile);
  36.   if (!File.Exists(assemblyFile))
  37.     throw new ArgumentException(string.Format("Project assembly file could not be located at {0}.", assemblyFile));
  38.   AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
  39.   return Assembly.Load(File.ReadAllBytes(assemblyFile));
  40. }
  41.  
  42. private void Cleanup() {
  43.   AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly;
  44. }
  45.  
  46. private Assembly ResolveAssembly(object sender, ResolveEventArgs args) {
  47.   string dependency = Path.Combine(outputDir, args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll");
  48.   if (File.Exists(dependency))
  49.     return Assembly.Load(File.ReadAllBytes(dependency));
  50.   return null;
  51. }
  52. #>
RAW Paste Data
Top