PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Tools/LinqPad/LINQPad/Extensions.cs

https://github.com/vishalsh-spec/TestProject
C# | 330 lines | 305 code | 25 blank | 0 comment | 74 complexity | cc04a44ffdfa3ffe6267eaee7f15cafb MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.0, Apache-2.0
  1. namespace LINQPad
  2. {
  3. using LINQPad.ExecutionModel;
  4. using LINQPad.Expressions;
  5. using LINQPad.Extensibility.DataContext;
  6. using LINQPad.ObjectGraph;
  7. using LINQPad.ObjectGraph.Formatters;
  8. using LINQPad.UI;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Data.Common;
  13. using System.Data.Linq;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Linq.Expressions;
  17. using System.Reflection;
  18. using System.Runtime.CompilerServices;
  19. using System.Windows.Forms;
  20. using System.Xml;
  21. using System.Xml.Linq;
  22. public static class Extensions
  23. {
  24. public static T[] Cache<T>(this IEnumerable<T> query)
  25. {
  26. return UserCache.CacheSequence<T>(query, null);
  27. }
  28. public static T[] Cache<T>(this IEnumerable<T> query, string key)
  29. {
  30. return UserCache.CacheSequence<T>(query, key);
  31. }
  32. public static string Disassemble(this MethodBase method)
  33. {
  34. return Disassembler.Disassemble(method);
  35. }
  36. public static T Dump<T>(this T o)
  37. {
  38. return o.Dump<T>(null, ((int?) null));
  39. }
  40. public static T Dump<T>(this T o, bool toDataGrid)
  41. {
  42. return o.Dump<T>(null, null, toDataGrid, null);
  43. }
  44. public static T Dump<T>(this T o, int depth)
  45. {
  46. return o.Dump<T>(null, new int?(depth));
  47. }
  48. public static T Dump<T>(this T o, string description)
  49. {
  50. return o.Dump<T>(description, ((int?) null));
  51. }
  52. public static T Dump<T>(this T o, string description, int? depth)
  53. {
  54. return o.Dump<T>(description, depth, false, null);
  55. }
  56. public static T Dump<T>(this T o, string description, bool toDataGrid)
  57. {
  58. return o.Dump<T>(description, null, toDataGrid, null);
  59. }
  60. private static T Dump<T>(this T o, string description, int? depth, bool toDataGrid, Action<ClickContext> onClick)
  61. {
  62. if (o != null)
  63. {
  64. Type type = o.GetType();
  65. if ((((!type.IsValueType && !type.IsPrimitive) && (type.Namespace != null)) && (type.Namespace.StartsWith("System.Threading.Tasks.Dataflow") && (type.GetInterface("System.Threading.Tasks.Dataflow.IDataflowBlock") != null))) && DataflowBridge.Dump(o, description))
  66. {
  67. return o;
  68. }
  69. }
  70. if (depth < 0)
  71. {
  72. depth = null;
  73. }
  74. if (depth > 20)
  75. {
  76. depth = 20;
  77. }
  78. if (((depth.HasValue || toDataGrid) || !AppDomain.CurrentDomain.GetAssemblies().Any<Assembly>(a => a.FullName.StartsWith("PresentationCore,", StringComparison.InvariantCultureIgnoreCase))) || !WpfBridge.DumpWpfElement(o, description))
  79. {
  80. bool flag2;
  81. if ((!depth.HasValue && !toDataGrid) && (o is Control))
  82. {
  83. if (o is Form)
  84. {
  85. ((Form) o).Show();
  86. return o;
  87. }
  88. PanelManager.DisplayControl((Control) o, description ?? "WinForms");
  89. return o;
  90. }
  91. if ((o is XContainer) || (o is XmlDocument))
  92. {
  93. PanelManager.DisplaySyntaxColoredText(o.ToString(), SyntaxLanguageStyle.XML, description ?? "XML");
  94. return o;
  95. }
  96. Server currentServer = Server.CurrentServer;
  97. TextWriter writer = (currentServer == null) ? null : currentServer.LambdaFormatter;
  98. Expression expr = null;
  99. if (writer != null)
  100. {
  101. if (o is IQueryable)
  102. {
  103. expr = ((IQueryable) o).Expression;
  104. }
  105. else if (o is Expression)
  106. {
  107. expr = (Expression) o;
  108. }
  109. }
  110. if (expr != null)
  111. {
  112. string content = "";
  113. try
  114. {
  115. ExpressionToken token = ExpressionToken.Visit(expr);
  116. if (token != null)
  117. {
  118. content = token.ToString();
  119. }
  120. }
  121. catch (Exception exception)
  122. {
  123. Log.Write(exception, "Dump ExpressionToken Visit");
  124. }
  125. if (content.Length > 0)
  126. {
  127. lock (writer)
  128. {
  129. if (!string.IsNullOrEmpty(description))
  130. {
  131. writer.WriteLine(new HeadingPresenter(description, content));
  132. }
  133. else
  134. {
  135. writer.WriteLine(content + "\r\n");
  136. }
  137. }
  138. }
  139. }
  140. if ((currentServer != null) && currentServer.WriteResultsToGrids)
  141. {
  142. toDataGrid = true;
  143. }
  144. if (toDataGrid && (o != null))
  145. {
  146. Type t = o.GetType();
  147. if (((!ExplorerGrid.IsAtomic(t) && (!t.IsArray || (t.GetArrayRank() == 1))) && (t.GetInterface("System.IObservable`1") == null)) && (t.GetCustomAttributes(typeof(MetaGraphNodeAttribute), false).Length == 0))
  148. {
  149. return o.Explore<T>(description);
  150. }
  151. }
  152. XhtmlWriter writer3 = (currentServer == null) ? null : currentServer.ResultsWriter;
  153. if (flag2 = o is Type)
  154. {
  155. ObjectNode.ExpandTypes = true;
  156. }
  157. try
  158. {
  159. if (!string.IsNullOrEmpty(description))
  160. {
  161. HeadingPresenter presenter = new HeadingPresenter(description, o);
  162. if (writer3 != null)
  163. {
  164. writer3.WriteDepth(presenter, depth, onClick);
  165. }
  166. else
  167. {
  168. Console.Write(presenter);
  169. }
  170. return o;
  171. }
  172. if (writer3 != null)
  173. {
  174. writer3.WriteLineDepth(o, depth, onClick);
  175. }
  176. else
  177. {
  178. Console.WriteLine(o);
  179. }
  180. }
  181. finally
  182. {
  183. if (flag2)
  184. {
  185. ObjectNode.ExpandTypes = false;
  186. }
  187. }
  188. }
  189. return o;
  190. }
  191. public static XDocument DumpFormatted(this XDocument xml)
  192. {
  193. return xml.DumpFormatted(null);
  194. }
  195. public static XElement DumpFormatted(this XElement xml)
  196. {
  197. return xml.DumpFormatted(null);
  198. }
  199. public static XmlDocument DumpFormatted(this XmlDocument xml)
  200. {
  201. return xml.DumpFormatted(null);
  202. }
  203. public static XmlElement DumpFormatted(this XmlElement xml)
  204. {
  205. return xml.DumpFormatted(null);
  206. }
  207. public static XDocument DumpFormatted(this XDocument xml, string heading)
  208. {
  209. if (string.IsNullOrEmpty(heading))
  210. {
  211. heading = "XML";
  212. }
  213. PanelManager.DisplaySyntaxColoredText(xml.ToString(), SyntaxLanguageStyle.XML, heading);
  214. return xml;
  215. }
  216. public static XElement DumpFormatted(this XElement xml, string heading)
  217. {
  218. if (string.IsNullOrEmpty(heading))
  219. {
  220. heading = "XML";
  221. }
  222. PanelManager.DisplaySyntaxColoredText(xml.ToString(), SyntaxLanguageStyle.XML, heading);
  223. return xml;
  224. }
  225. public static XmlDocument DumpFormatted(this XmlDocument xml, string heading)
  226. {
  227. if (string.IsNullOrEmpty(heading))
  228. {
  229. heading = "XML";
  230. }
  231. PanelManager.DisplaySyntaxColoredText(XmlHelper.ToFormattedString(xml), SyntaxLanguageStyle.XML, heading);
  232. return xml;
  233. }
  234. public static XmlElement DumpFormatted(this XmlElement xml, string heading)
  235. {
  236. if (string.IsNullOrEmpty(heading))
  237. {
  238. heading = "XML";
  239. }
  240. PanelManager.DisplaySyntaxColoredText(XmlHelper.ToFormattedString(xml), SyntaxLanguageStyle.XML, heading);
  241. return xml;
  242. }
  243. public static IObservable<T> DumpLive<T>(this IObservable<T> obs)
  244. {
  245. return obs.DumpLive<T>(null);
  246. }
  247. public static IObservable<T> DumpLive<T>(this IObservable<T> obs, string heading)
  248. {
  249. LiveDumper.DumpLive<T>(obs, heading);
  250. return obs;
  251. }
  252. internal static DataSet ExecuteDataSet(this DbCommand cmd)
  253. {
  254. if ((cmd == null) || (cmd.Connection == null))
  255. {
  256. return null;
  257. }
  258. DbConnection connection = cmd.Connection;
  259. using (DbDataAdapter adapter = cmd.Connection.GetFactory().CreateDataAdapter())
  260. {
  261. adapter.SelectCommand = cmd;
  262. DataSet dataSet = new DataSet();
  263. adapter.Fill(dataSet);
  264. return dataSet;
  265. }
  266. }
  267. internal static T Explore<T>(this T o, string panelTitle)
  268. {
  269. if (o != null)
  270. {
  271. GridOptions options2 = new GridOptions {
  272. MembersToExclude = null,
  273. PanelTitle = panelTitle
  274. };
  275. Server currentServer = Server.CurrentServer;
  276. if ((currentServer != null) && (currentServer.DataContextDriver != null))
  277. {
  278. currentServer.DataContextDriver.DisplayObjectInGrid(o, options2);
  279. }
  280. else
  281. {
  282. ExplorerGrid.Display(o, options2);
  283. }
  284. }
  285. return o;
  286. }
  287. internal static DbProviderFactory GetFactory(this DbConnection cx)
  288. {
  289. if (cx == null)
  290. {
  291. return null;
  292. }
  293. PropertyInfo property = cx.GetType().GetProperty("DbProviderFactory", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  294. if (property == null)
  295. {
  296. return null;
  297. }
  298. return (property.GetValue(cx, null) as DbProviderFactory);
  299. }
  300. public static object ToImage(this Binary imageData)
  301. {
  302. return Util.Image(imageData);
  303. }
  304. }
  305. }