PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Tools/LinqPad/LINQPad/ObjectGraph/ObjectNode.cs

https://github.com/vishalsh-spec/TestProject
C# | 662 lines | 632 code | 30 blank | 0 comment | 208 complexity | 726bbcf02dd2d51185753590737e0769 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.0, Apache-2.0
  1. namespace LINQPad.ObjectGraph
  2. {
  3. using LINQPad;
  4. using LINQPad.ExecutionModel;
  5. using LINQPad.Extensibility.DataContext;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.Data.Linq;
  11. using System.Data.SqlTypes;
  12. using System.Diagnostics;
  13. using System.Drawing;
  14. using System.Drawing.Imaging;
  15. using System.Dynamic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Reflection;
  19. using System.Runtime.CompilerServices;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using System.Xml;
  24. using System.Xml.Linq;
  25. internal abstract class ObjectNode
  26. {
  27. private bool _containsProcess;
  28. public readonly ObjectNode CyclicReference;
  29. public readonly DataContextDriver DCDriver;
  30. public static bool ExpandTypes = false;
  31. internal bool HasTypeReferences;
  32. internal readonly int MaxDepth = 5;
  33. public readonly int NestingDepth;
  34. public readonly object ObjectValue;
  35. public Action<ClickContext> OnClick;
  36. protected ObjectNode(ObjectNode parent, object value, int maxDepth, DataContextDriver dcDriver)
  37. {
  38. this.Parent = parent;
  39. this.ObjectValue = value;
  40. this.MaxDepth = maxDepth;
  41. this.DCDriver = dcDriver;
  42. if (Util.IsMetaGraphNode(value))
  43. {
  44. this.NestingDepth--;
  45. }
  46. while (parent != null)
  47. {
  48. if (!Util.IsMetaGraphNode(parent.ObjectValue))
  49. {
  50. this.NestingDepth++;
  51. }
  52. if (IsSame(value, parent.ObjectValue))
  53. {
  54. this.CyclicReference = parent;
  55. }
  56. if (parent.ObjectValue is Process)
  57. {
  58. this._containsProcess = true;
  59. }
  60. parent = parent.Parent;
  61. }
  62. }
  63. public abstract object Accept(IObjectGraphVisitor visitor);
  64. public static ObjectNode Create(object item, int? maxDepth, DataContextDriver dcDriver)
  65. {
  66. int? nullable = maxDepth;
  67. return Create(null, item, nullable.HasValue ? nullable.GetValueOrDefault() : 5, dcDriver);
  68. }
  69. internal static ObjectNode Create(ObjectNode parent, object item, int maxDepth, DataContextDriver dcDriver)
  70. {
  71. if (item is ObjectNode)
  72. {
  73. ObjectNode node = (ObjectNode) item;
  74. node.Parent = parent;
  75. return node;
  76. }
  77. if (item == null)
  78. {
  79. return new SimpleNode(parent, null);
  80. }
  81. if (((!(item is string) && !(item is int)) && !(item is decimal)) && !(item is DateTime))
  82. {
  83. Type type = item.GetType();
  84. if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>)))
  85. {
  86. type = type.GetGenericArguments()[0];
  87. }
  88. if (Type.GetTypeCode(type) == TypeCode.Object)
  89. {
  90. HandleObjectProxies(ref item, parent, dcDriver);
  91. }
  92. if (item == ObjectGraphInfo.GetDisplayNothingToken())
  93. {
  94. return new EmptyNode();
  95. }
  96. if (item == null)
  97. {
  98. return new SimpleNode(parent, null);
  99. }
  100. }
  101. Type type2 = item.GetType();
  102. if ((((item is byte[]) || (item is Binary)) && (parent != null)) && !(parent.ObjectValue is HeadingPresenter))
  103. {
  104. string str;
  105. byte[] buffer = item as byte[];
  106. if (buffer == null)
  107. {
  108. buffer = ((Binary) item).ToArray();
  109. }
  110. if (buffer.Length <= 20)
  111. {
  112. StringBuilder builder = new StringBuilder();
  113. foreach (byte num2 in buffer)
  114. {
  115. if (builder.Length > 0)
  116. {
  117. builder.Append(" ");
  118. }
  119. builder.AppendFormat("{0:X2}", num2);
  120. }
  121. str = builder.ToString();
  122. }
  123. else
  124. {
  125. str = "byte[]";
  126. }
  127. return new SimpleNode(parent, str, "(binary data)", SimpleNodeKind.Metadata);
  128. }
  129. if (((((((item is string) || (item is bool)) || ((item is char) || (item is TimeSpan))) || (((item is IFormattable) || (item is XObject)) || ((item is XName) || (item is XNamespace)))) || ((((item is SqlBoolean) || (item is SqlByte)) || ((item is SqlDateTime) || (item is SqlDecimal))) || (((item is SqlDouble) || (item is SqlGuid)) || ((item is SqlInt16) || (item is SqlInt32))))) || (((item is SqlInt64) || (item is SqlMoney)) || (item is SqlSingle))) || (item is SqlString))
  130. {
  131. return new SimpleNode(parent, item.ToString());
  132. }
  133. if (item is XmlNode)
  134. {
  135. return new SimpleNode(parent, XmlHelper.ToFormattedString((XmlNode) item));
  136. }
  137. Type type3 = type2.IsGenericType ? type2.GetGenericTypeDefinition() : null;
  138. if (!(((!(type3 == typeof(EntitySet<>)) || (parent == null)) || (parent.IsAnonType || (parent is ListNode))) || (parent.ObjectValue is HeadingPresenter)))
  139. {
  140. return new SimpleNode(parent, "EntitySet", type2.FormatTypeName(), SimpleNodeKind.Metadata);
  141. }
  142. for (ObjectNode node3 = parent; node3 == null; node3 = node3.Parent)
  143. {
  144. Label_0305:
  145. if (0 == 0)
  146. {
  147. if ((type3 == typeof(Table<>)) && (node3 != null))
  148. {
  149. return new SimpleNode(parent, "Table", type2.FormatTypeName(), SimpleNodeKind.Metadata);
  150. }
  151. if (item is DBNull)
  152. {
  153. return new SimpleNode(parent, "null", "DbNull", SimpleNodeKind.Metadata);
  154. }
  155. string text = null;
  156. ObjectNode payload = null;
  157. ReturnDataSet set = item as ReturnDataSet;
  158. if (set != null)
  159. {
  160. text = set.ReturnValue.ToString();
  161. if (set.OutputParameters != null)
  162. {
  163. foreach (KeyValuePair<string, object> pair in set.OutputParameters)
  164. {
  165. if (pair.Value != null)
  166. {
  167. string str3 = text;
  168. text = str3 + ", " + pair.Key + "=" + pair.Value.ToString();
  169. }
  170. }
  171. }
  172. payload = new SimpleNode(null, text);
  173. }
  174. if ((item is DataSet) && (((DataSet) item).Tables.Count == 1))
  175. {
  176. item = ((DataSet) item).Tables[0];
  177. }
  178. if (item is DataSet)
  179. {
  180. return new ListPayloadNode(parent, ((DataSet) item).Tables, maxDepth, dcDriver, "Result Sets", payload, "ReturnValue");
  181. }
  182. if (item is DataTable)
  183. {
  184. return new ListPayloadNode(parent, ((DataTable) item).Rows, maxDepth, dcDriver, "Result Set", payload, "ReturnValue");
  185. }
  186. if (item is DataRow)
  187. {
  188. return new DataRowNode(parent, (DataRow) item, maxDepth, dcDriver);
  189. }
  190. if (item is IDataReader)
  191. {
  192. DataReaderNode node5 = new DataReaderNode(parent, (IDataReader) item, maxDepth, dcDriver);
  193. if (node5.Items.Count == 1)
  194. {
  195. return node5.Items[0];
  196. }
  197. return node5;
  198. }
  199. if (item is IDataRecord)
  200. {
  201. return new DataRecordMemberNode(parent, null, (IDataRecord) item, maxDepth, dcDriver);
  202. }
  203. if (type2.GetInterfaces().Any<Type>(t => t.FullName == typeof(ICustomMemberProvider).FullName))
  204. {
  205. try
  206. {
  207. return new CustomMemberProviderNode(parent, item, maxDepth, dcDriver, false);
  208. }
  209. catch (Exception exception)
  210. {
  211. Log.Write(exception, "CustomMemberProvider");
  212. }
  213. }
  214. if ((dcDriver != null) && (dcDriver.GetCustomDisplayMemberProvider(item) != null))
  215. {
  216. return new CustomMemberProviderNode(parent, item, maxDepth, dcDriver, true);
  217. }
  218. if (item is Type)
  219. {
  220. for (ObjectNode node6 = parent; node6 != null; node6 = node6.Parent)
  221. {
  222. node6.HasTypeReferences = true;
  223. }
  224. }
  225. if (!(ExpandTypes || !(item is Type)))
  226. {
  227. return new SimpleNode(parent, "typeof (" + ((Type) item).FormatTypeName() + ")", ((Type) item).FormatTypeName(true), SimpleNodeKind.Metadata) { HasTypeReferences = true };
  228. }
  229. if (item is Image)
  230. {
  231. Image image = (Image) item;
  232. MemoryStream stream = new MemoryStream();
  233. image.Save(stream, ImageFormat.Png);
  234. item = Util.Image(stream.ToArray());
  235. }
  236. if (((!type2.IsArray && (item is IList)) && (((IList) item).Count == 1)) && (((IList) item)[0] == item))
  237. {
  238. return new ClrMemberNode(parent, item, maxDepth, dcDriver);
  239. }
  240. if (type2.IsArray && (type2.GetArrayRank() == 2))
  241. {
  242. Type elementType = type2.GetElementType();
  243. if (elementType.IsGenericType && (elementType.GetGenericTypeDefinition() == typeof(Nullable<>)))
  244. {
  245. elementType = elementType.GetGenericArguments()[0];
  246. }
  247. if (((typeof(IFormattable).IsAssignableFrom(elementType) || (elementType == typeof(bool))) || ((elementType == typeof(char)) || (elementType == typeof(TimeSpan)))) || (elementType == typeof(string)))
  248. {
  249. return new MultiDimArrayNode(parent, (Array) item);
  250. }
  251. }
  252. if (item is IEnumerable)
  253. {
  254. return new ListNode(parent, (IEnumerable) item, maxDepth, dcDriver);
  255. }
  256. if (item is Exception)
  257. {
  258. return new ExceptionNode(parent, (Exception) item, maxDepth);
  259. }
  260. if (item is DynamicObject)
  261. {
  262. return new DynamicObjectMemberNode(parent, (DynamicObject) item, maxDepth, dcDriver);
  263. }
  264. return new ClrMemberNode(parent, item, maxDepth, dcDriver);
  265. }
  266. }
  267. goto Label_0305;
  268. }
  269. internal static ObjectNode Create(ObjectNode parent, object item, bool foldExceptions, int maxDepth, DataContextDriver dcDriver)
  270. {
  271. if (!foldExceptions)
  272. {
  273. return Create(parent, item, maxDepth, dcDriver);
  274. }
  275. try
  276. {
  277. return Create(parent, item, maxDepth, dcDriver);
  278. }
  279. catch (Exception exception)
  280. {
  281. return new SimpleNode(parent, "(" + exception.GetType().Name + ": " + exception.Message + ")", "An exception was thrown when querying this value", SimpleNodeKind.Warning);
  282. }
  283. }
  284. private static IEnumerable<object> GetParentHierarchy(ObjectNode parent)
  285. {
  286. return new <GetParentHierarchy>d__2(-2) { <>3__parent = parent };
  287. }
  288. private static bool HandleAsyncEnumerable(ref object item, ObjectGraphInfo info)
  289. {
  290. Server currentServer = Server.CurrentServer;
  291. if (currentServer == null)
  292. {
  293. return false;
  294. }
  295. Type type = item.GetType().GetInterface("System.Collections.Generic.IAsyncEnumerable`1");
  296. if (type == null)
  297. {
  298. return false;
  299. }
  300. MethodInfo method = type.GetMethod("GetEnumerator", new Type[0]);
  301. if (method == null)
  302. {
  303. return false;
  304. }
  305. object rator = method.Invoke(item, new object[0]);
  306. if (rator == null)
  307. {
  308. return false;
  309. }
  310. List<IDisposable> disposables = currentServer.Disposables;
  311. if (rator is IDisposable)
  312. {
  313. disposables.Add((IDisposable) rator);
  314. }
  315. MethodInfo moveNextMethod = rator.GetType().GetMethod("MoveNext", new Type[0]);
  316. if (moveNextMethod == null)
  317. {
  318. moveNextMethod = rator.GetType().GetMethod("MoveNext", new Type[] { typeof(CancellationToken) });
  319. }
  320. if (moveNextMethod == null)
  321. {
  322. return false;
  323. }
  324. if (moveNextMethod.ReturnType != typeof(Task<bool>))
  325. {
  326. return false;
  327. }
  328. PropertyInfo property = rator.GetType().GetProperty("Current");
  329. if (property == null)
  330. {
  331. return false;
  332. }
  333. Server.CurrentServer.QueryCompletionCountdown.Increment();
  334. HandleAsyncEnumerator(rator, moveNextMethod, property, info.Heading);
  335. item = info.DisplayNothingToken;
  336. return true;
  337. }
  338. private static void HandleAsyncEnumerator(object rator, MethodInfo moveNextMethod, PropertyInfo currentProp, string heading)
  339. {
  340. object[] objArray2;
  341. if (moveNextMethod.GetParameters().Length == 1)
  342. {
  343. objArray2 = new object[] { CancellationToken.None };
  344. }
  345. else
  346. {
  347. objArray2 = new object[0];
  348. }
  349. Task<bool> task = (Task<bool>) moveNextMethod.Invoke(rator, objArray2);
  350. if (task != null)
  351. {
  352. task.ContinueWith(delegate (Task<bool> ant) {
  353. if (ant.Exception != null)
  354. {
  355. if (string.IsNullOrEmpty(heading))
  356. {
  357. ant.Exception.Dump<AggregateException>();
  358. }
  359. else
  360. {
  361. Util.HorizontalRun(true, new object[] { Util.Metatext(heading + " →"), ant.Exception }).Dump<object>();
  362. }
  363. }
  364. if (!((ant.Exception != null) ? false : ant.Result))
  365. {
  366. Server.CurrentServer.QueryCompletionCountdown.Decrement();
  367. }
  368. else
  369. {
  370. object o = currentProp.GetValue(rator, new object[0]);
  371. if (string.IsNullOrEmpty(heading))
  372. {
  373. o.Dump<object>();
  374. }
  375. else
  376. {
  377. Util.HorizontalRun(true, new object[] { Util.Metatext(heading + " →"), o }).Dump<object>();
  378. }
  379. HandleAsyncEnumerator(rator, moveNextMethod, currentProp, heading);
  380. }
  381. });
  382. }
  383. }
  384. private static void HandleObjectProxies(ref object item, ObjectNode parent, DataContextDriver dcDriver)
  385. {
  386. HeadingPresenter objectValue = null;
  387. if (parent != null)
  388. {
  389. objectValue = parent.ObjectValue as HeadingPresenter;
  390. }
  391. string heading = (objectValue == null) ? null : (objectValue.Heading as string);
  392. ObjectGraphInfo info = new ObjectGraphInfo(heading, GetParentHierarchy(parent));
  393. if (dcDriver != null)
  394. {
  395. dcDriver.PreprocessObjectToWrite(ref item, info);
  396. }
  397. HandleObservable(ref item, info);
  398. HandleAsyncEnumerable(ref item, info);
  399. if ((item == info.DisplayNothingToken) && (objectValue != null))
  400. {
  401. objectValue.HidePresenter = true;
  402. }
  403. }
  404. private static bool HandleObservable(ref object item, ObjectGraphInfo info)
  405. {
  406. Server currentServer = Server.CurrentServer;
  407. if (currentServer == null)
  408. {
  409. return false;
  410. }
  411. Type type = item.GetType().GetInterface("System.IObservable`1");
  412. if (type == null)
  413. {
  414. return false;
  415. }
  416. MethodInfo method = typeof(ObservableHelper).GetMethod("Subscribe", BindingFlags.Public | BindingFlags.Static);
  417. if (method == null)
  418. {
  419. return false;
  420. }
  421. string heading = info.Heading;
  422. Type type2 = type.GetGenericArguments()[0];
  423. MethodInfo info3 = typeof(NextActionClosure).GetMethod("NextAction").MakeGenericMethod(new Type[] { type2 });
  424. Delegate delegate2 = Delegate.CreateDelegate(typeof(Action<>).MakeGenericType(new Type[] { type2 }), new NextActionClosure(heading), info3);
  425. Countdown countdown = currentServer.QueryCompletionCountdown;
  426. List<IDisposable> disposables = currentServer.Disposables;
  427. Action<Exception> action = delegate (Exception ex) {
  428. if (!string.IsNullOrEmpty(heading))
  429. {
  430. Util.HorizontalRun(true, new object[] { Util.Metatext(heading + " →"), ex }).Dump<object>();
  431. }
  432. else
  433. {
  434. ex.Dump<Exception>();
  435. }
  436. try
  437. {
  438. countdown.Decrement();
  439. }
  440. catch
  441. {
  442. }
  443. };
  444. Action action2 = new Action(countdown.Decrement);
  445. object obj2 = method.MakeGenericMethod(new Type[] { type2 }).Invoke(null, new object[] { item, delegate2, action, action2 });
  446. countdown.Increment();
  447. if (obj2 is IDisposable)
  448. {
  449. lock (disposables)
  450. {
  451. disposables.Add((IDisposable) obj2);
  452. }
  453. }
  454. item = info.DisplayNothingToken;
  455. return true;
  456. }
  457. protected bool IsAtNestingLimit()
  458. {
  459. int num = this.MaxDepth + ((this.ObjectValue is Exception) ? 1 : 0);
  460. if ((this.Parent != null) && (this.Parent.ObjectValue is Exception))
  461. {
  462. num++;
  463. }
  464. if (this._containsProcess)
  465. {
  466. num -= 2;
  467. }
  468. return ((this.NestingDepth >= num) || (((this.Parent != null) && (this.Parent.Parent != null)) && (this.Parent.Parent.ObjectValue is Type)));
  469. }
  470. internal static bool IsKey(string name, Type type)
  471. {
  472. if (((type == typeof(decimal)) || (type == typeof(double))) || (type == typeof(float)))
  473. {
  474. return false;
  475. }
  476. if (name.ToLowerInvariant() == "id")
  477. {
  478. return true;
  479. }
  480. if (name.Length < 3)
  481. {
  482. return false;
  483. }
  484. return (name.EndsWith("Id", StringComparison.Ordinal) || (name.EndsWith("_ID", StringComparison.OrdinalIgnoreCase) || ((name.EndsWith("ID", StringComparison.Ordinal) && char.IsLower(name[name.Length - 3])) || (name.EndsWith("Key", StringComparison.Ordinal) || name.EndsWith("_KEY", StringComparison.OrdinalIgnoreCase)))));
  485. }
  486. private static bool IsSame(object o1, object o2)
  487. {
  488. return (object.ReferenceEquals(o1, o2) || (((o1 is FileSystemInfo) && (o2 is FileSystemInfo)) && (((FileSystemInfo) o1).FullName == ((FileSystemInfo) o2).FullName)));
  489. }
  490. public bool GraphTruncated { get; protected set; }
  491. public bool InitiallyHidden { get; protected set; }
  492. public bool IsAnonType
  493. {
  494. get
  495. {
  496. return ((this.ObjectValue != null) && this.ObjectValue.GetType().IsAnonymous());
  497. }
  498. }
  499. public ObjectNode Parent { get; private set; }
  500. [CompilerGenerated]
  501. private sealed class <GetParentHierarchy>d__2 : IEnumerable<object>, IEnumerable, IEnumerator<object>, IEnumerator, IDisposable
  502. {
  503. private bool $__disposing;
  504. private int <>1__state;
  505. private object <>2__current;
  506. public ObjectNode <>3__parent;
  507. private int <>l__initialThreadId;
  508. public ObjectNode parent;
  509. [DebuggerHidden]
  510. public <GetParentHierarchy>d__2(int <>1__state)
  511. {
  512. this.<>1__state = <>1__state;
  513. this.<>l__initialThreadId = Thread.CurrentThread.ManagedThreadId;
  514. }
  515. private bool MoveNext()
  516. {
  517. try
  518. {
  519. if (this.<>1__state == 1)
  520. {
  521. if (this.$__disposing)
  522. {
  523. return false;
  524. }
  525. this.<>1__state = 0;
  526. goto Label_0067;
  527. }
  528. if (this.<>1__state == -1)
  529. {
  530. return false;
  531. }
  532. if (this.$__disposing)
  533. {
  534. return false;
  535. }
  536. Label_0047:
  537. if (this.parent == null)
  538. {
  539. goto Label_00A2;
  540. }
  541. if (!Util.IsMetaGraphNode(this.parent.ObjectValue))
  542. {
  543. goto Label_007C;
  544. }
  545. Label_0067:
  546. this.parent = this.parent.Parent;
  547. goto Label_0047;
  548. Label_007C:
  549. this.<>2__current = this.parent.ObjectValue;
  550. this.<>1__state = 1;
  551. return true;
  552. }
  553. catch (Exception)
  554. {
  555. this.<>1__state = -1;
  556. throw;
  557. }
  558. Label_00A2:
  559. this.<>1__state = -1;
  560. return false;
  561. }
  562. [DebuggerHidden]
  563. IEnumerator<object> IEnumerable<object>.GetEnumerator()
  564. {
  565. ObjectNode.<GetParentHierarchy>d__2 d__;
  566. if ((Thread.CurrentThread.ManagedThreadId == this.<>l__initialThreadId) && (this.<>1__state == -2))
  567. {
  568. this.<>1__state = 0;
  569. d__ = this;
  570. }
  571. else
  572. {
  573. d__ = new ObjectNode.<GetParentHierarchy>d__2(0);
  574. }
  575. d__.parent = this.<>3__parent;
  576. return d__;
  577. }
  578. [DebuggerHidden]
  579. IEnumerator IEnumerable.GetEnumerator()
  580. {
  581. return this.System.Collections.Generic.IEnumerable<System.Object>.GetEnumerator();
  582. }
  583. [DebuggerHidden]
  584. void IEnumerator.Reset()
  585. {
  586. throw new NotSupportedException();
  587. }
  588. [DebuggerHidden]
  589. void IDisposable.Dispose()
  590. {
  591. this.$__disposing = true;
  592. this.MoveNext();
  593. this.<>1__state = -1;
  594. }
  595. object IEnumerator<object>.Current
  596. {
  597. [DebuggerHidden]
  598. get
  599. {
  600. return this.<>2__current;
  601. }
  602. }
  603. object IEnumerator.Current
  604. {
  605. [DebuggerHidden]
  606. get
  607. {
  608. return this.<>2__current;
  609. }
  610. }
  611. }
  612. private class NextActionClosure
  613. {
  614. public readonly string Heading;
  615. public NextActionClosure(string heading)
  616. {
  617. this.Heading = heading;
  618. }
  619. public void NextAction<T>(T data)
  620. {
  621. if (string.IsNullOrEmpty(this.Heading))
  622. {
  623. data.Dump<T>();
  624. }
  625. else
  626. {
  627. Util.HorizontalRun(true, new object[] { Util.Metatext(this.Heading + " →"), data }).Dump<object>();
  628. }
  629. }
  630. }
  631. }
  632. }