PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/NRAPI/Source/Widgetsphere.Core/Util/LINQDynamicCompile.cs

#
C# | 96 lines | 86 code | 9 blank | 1 comment | 5 complexity | 36089230814a43e381e6610877461565 MD5 | raw file
Possible License(s): JSON, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using Widgetsphere.Core.Util;
  7. using System.CodeDom.Compiler;
  8. using System.IO;
  9. namespace Widgetsphere.Core.Util
  10. {
  11. public static class LINQDynamicCompile
  12. {
  13. private static Dictionary<string, MethodInfo> queryDictionary = new Dictionary<string, MethodInfo>();
  14. public static MethodInfo GetMethod(string linq, string businessNamespace, string objectName)
  15. {
  16. string hash = HashHelper.ComputeHash(linq);
  17. MethodInfo method = null;
  18. if (!queryDictionary.ContainsKey(hash))
  19. {
  20. method = LINQDynamicCompile.CreateScriptClass(Guid.NewGuid(), businessNamespace, linq, objectName);
  21. queryDictionary.Add(hash, method);
  22. }
  23. else
  24. method = queryDictionary[hash];
  25. return method;
  26. }
  27. private static MethodInfo CreateScriptClass(System.Guid currentGuid, string businessNamespace, string linq, string ObjectName)
  28. {
  29. string guid = currentGuid.ToString("N");
  30. StringBuilder sb = new StringBuilder();
  31. sb.AppendLine("using System;");
  32. sb.AppendLine("using System.Linq;");
  33. sb.AppendLine("using System.Linq.Expressions;");
  34. sb.AppendLine("using " + businessNamespace + ".Objects;");
  35. sb.AppendLine("using " + businessNamespace + ".LINQ;");
  36. sb.AppendLine("namespace Widgetpshere.MemoryGeneration");
  37. sb.AppendLine("{");
  38. sb.AppendLine("public static class " + ObjectName + "Queries" + guid);
  39. sb.AppendLine("{");
  40. sb.AppendLine(" public static " + ObjectName + "Collection ZZ" + guid + "(" + ObjectName + "Paging paging)");
  41. sb.AppendLine(" {");
  42. sb.AppendLine(" return " + ObjectName + "Collection.RunSelect(" + linq + ", paging);");
  43. sb.AppendLine(" }");
  44. sb.AppendLine("}");
  45. sb.AppendLine("}");
  46. try
  47. {
  48. CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("C#");
  49. CompilerParameters cp = new CompilerParameters();
  50. //TODO: Set Uri Through Different Variable
  51. Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
  52. FileInfo fi = new FileInfo(uri.AbsolutePath);
  53. string appServerBinDirectory = fi.DirectoryName;
  54. if (!string.IsNullOrEmpty(appServerBinDirectory))
  55. {
  56. cp.CompilerOptions = string.Format(@"/lib:""{0}""", appServerBinDirectory);
  57. }
  58. cp.ReferencedAssemblies.Add("System.dll");
  59. cp.ReferencedAssemblies.Add("System.Core.dll");
  60. cp.ReferencedAssemblies.Add(businessNamespace.Remove(businessNamespace.Length - 9) + ".dll");
  61. cp.ReferencedAssemblies.Add("Widgetsphere.Core.dll");
  62. cp.GenerateExecutable = false;
  63. cp.GenerateInMemory = true;
  64. CompilerResults cr = codeDomProvider.CompileAssemblyFromSource(cp, sb.ToString());
  65. if (cr.Errors.HasErrors)
  66. {
  67. StringBuilder error = new StringBuilder();
  68. error.Append("Error Compiling Expression: ");
  69. foreach (CompilerError err in cr.Errors)
  70. {
  71. error.AppendFormat("{0}\n", err.ErrorText);
  72. }
  73. throw new Exception("Error Compiling Expression: " + error.ToString());
  74. }
  75. Assembly assembly = cr.CompiledAssembly;
  76. Type[] types = assembly.GetTypes();
  77. if (types.Length == 1)
  78. return types[0].GetMethod("ZZ" + guid);
  79. }
  80. catch (Exception ex)
  81. {
  82. throw ex;
  83. }
  84. return null;
  85. }
  86. }
  87. }