/FlowEditor/Designer/Comm channels/Reflection/AssemblyData.cs

https://gitlab.com/Tiger66639/neural_network_designer
C# | 147 lines | 98 code | 18 blank | 31 comment | 18 complexity | dbce504a31cf975d4c4788b9cf0f3f6c MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. namespace JaStDev.HAB.Designer.Comm_channels.Reflection
  5. {
  6. /// <summary>
  7. /// Contains all the
  8. /// </summary>
  9. public class AssemblyData : ReflectionData
  10. {
  11. #region Fields
  12. ObservedCollection<NamespaceData> fChildren;
  13. Assembly fAssembly;
  14. #endregion
  15. #region ctor
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="AssemblyData"/> class.
  18. /// </summary>
  19. /// <param name="name">The name of the assembly to load.</param>
  20. public AssemblyData(string name)
  21. {
  22. fChildren = new ObservedCollection<NamespaceData>(this);
  23. Assembly iAsm = (from i in AppDomain.CurrentDomain.GetAssemblies()
  24. where i.FullName == name
  25. select i).FirstOrDefault();
  26. if (iAsm != null)
  27. LoadAssembly(iAsm);
  28. else
  29. Log.Log.LogError("AssemblyData.Create", string.Format("Failed to find assembly with name: '{0}'.", name));
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="AssemblyData"/> class.
  33. /// </summary>
  34. public AssemblyData()
  35. {
  36. fChildren = new ObservedCollection<NamespaceData>(this);
  37. }
  38. #endregion
  39. #region prop
  40. #region Children
  41. /// <summary>
  42. /// Gets the list of all the types found in the namespace
  43. /// </summary>
  44. public ObservedCollection<NamespaceData> Children
  45. {
  46. get { return fChildren; }
  47. }
  48. #endregion
  49. #region IsLoaded
  50. /// <summary>
  51. /// Gets or sets the function(s)/children are loaded.
  52. /// </summary>
  53. /// <value>
  54. /// <c>true</c>: all the children are loaded - the function is loaded
  55. /// <c>false</c>: none of the children are loaded - the function is not loaded.
  56. /// <c>null</c>: some loaded - invalid.
  57. /// </value>
  58. /// <remarks>
  59. /// Setting to null is not processed.
  60. /// </remarks>
  61. public override bool? IsLoaded
  62. {
  63. get
  64. {
  65. if (Children.Count > 0)
  66. {
  67. bool? iRes = Children[0].IsLoaded;
  68. for (int i = 1; i < Children.Count; i++)
  69. {
  70. if (Children[i].IsLoaded != iRes)
  71. return null;
  72. }
  73. return iRes;
  74. }
  75. else
  76. return null;
  77. }
  78. set
  79. {
  80. if (value.HasValue == true)
  81. {
  82. foreach (NamespaceData i in Children)
  83. i.IsLoaded = value;
  84. OnLoadedChanged();
  85. }
  86. }
  87. }
  88. #endregion
  89. #region Assembly
  90. /// <summary>
  91. /// Gets/sets the assembly that this object wraps.
  92. /// </summary>
  93. public Assembly Assembly
  94. {
  95. get { return fAssembly; }
  96. set { LoadAssembly(value); }
  97. }
  98. #endregion
  99. #endregion
  100. #region Functions
  101. /// <summary>
  102. /// Loads all the data for the assembly.
  103. /// </summary>
  104. /// <param name="iAsm">The assembly to load.</param>
  105. private void LoadAssembly(Assembly asm)
  106. {
  107. fAssembly = asm;
  108. var iTypes = from i in asm.GetTypes()
  109. where i.IsPublic == true
  110. orderby i.Namespace
  111. select i;
  112. NamespaceData iNsData = null;
  113. foreach (Type i in iTypes)
  114. {
  115. if (iNsData == null || iNsData.Name != i.Namespace) //need to create a new namespace item
  116. {
  117. if (iNsData != null && iNsData.Children.Count > 0) //we only add if there are actually items in the ns -> there were static public functions in the ns.
  118. Children.Add(iNsData);
  119. iNsData = new NamespaceData();
  120. iNsData.Name = i.Namespace;
  121. }
  122. TypeData iType = new TypeData(i);
  123. if (iType.Children.Count > 0)
  124. iNsData.Children.Add(iType);
  125. }
  126. }
  127. #endregion
  128. }
  129. }