Connecting Tech Pros Worldwide Forums | Help | Site Map

loading multiple assemblies

Newbie
 
Join Date: May 2007
Posts: 1
#1: May 31 '07
hi friends....

I have got a problem regarding loading of multiple assemblies, actually its required for an editor which implements the intellisenseas in vb or dotnet.
for that it is required to load that assembly whose sub-classes are required to be loaded into the list box after pressing dot.

for example in the code given below i have given the path of System.dll assembly file, now all the sub class of System.Data are automatically displayed in list box but if i need to load more than one assmbly i.e 5-6 assemblies how should i do that.

source code

Expand|Select|Wrap|Line Numbers
  1. private void readAssembly()
  2. {
  3. this.treeViewItems.Nodes.Clear();
  4. namespaces = new Hashtable();
  5.  
  6. Type [] assemblyTypes = assembly.GetTypes(); 
  7. this.treeViewItems.Nodes.Clear();
  8.  
  9. // Cycle through types
  10. foreach (Type type in assemblyTypes) 
  11. {
  12. if ( type.Namespace != null )
  13. {
  14. if ( namespaces.ContainsKey(type.Namespace) )
  15. {
  16. // Already got namespace, add the class to it
  17. TreeNode treeNode = (TreeNode) namespaces[type.Namespace];
  18. treeNode = treeNode.Nodes.Add(type.Name);
  19. this.addMembers(treeNode,type);
  20.  
  21. if ( type.IsClass )
  22. {
  23. treeNode.Tag = MemberTypes.Custom;
  24. }
  25. }
  26. else
  27. {
  28. // New namespace
  29. TreeNode membersNode = null;
  30.  
  31. if ( type.Namespace.IndexOf(".") != -1 )
  32. {
  33. // Search for already existing parts of the namespace
  34. nameSpaceNode = null;
  35. foundNode = false;
  36.  
  37. this.currentPath = "";
  38. searchTree(this.treeViewItems.Nodes,type.Namespace,false);
  39.  
  40. // No existing namespace found
  41. if ( nameSpaceNode == null )
  42. {
  43. // Add the namespace
  44. string[] parts = type.Namespace.Split('.');
  45.  
  46. TreeNode treeNode = treeViewItems.Nodes.Add(parts[0]);
  47. string sNamespace = parts[0];
  48.  
  49. if ( !namespaces.ContainsKey(sNamespace) )
  50. {
  51. namespaces.Add(sNamespace,treeNode);
  52. }
  53.  
  54. for (int i=1;i < parts.Length;i++)
  55. {
  56. treeNode = treeNode.Nodes.Add(parts[i]);
  57. sNamespace += "." +parts[i];
  58. if ( !namespaces.ContainsKey(sNamespace) )
  59. {
  60. namespaces.Add(sNamespace,treeNode);
  61. }
  62. }
  63.  
  64. membersNode = treeNode.Nodes.Add(type.Name);
  65. }
  66. else
  67. {
  68. // Existing namespace, add this namespace to it,
  69. // and add the class
  70. string[] parts = type.Namespace.Split('.');
  71. TreeNode newNamespaceNode = null;
  72.  
  73. if ( !namespaces.ContainsKey(type.Namespace) )
  74. {
  75. newNamespaceNode = nameSpaceNode.Nodes.Add(parts[parts.Length-1]);
  76. namespaces.Add(type.Namespace,newNamespaceNode);
  77. }
  78. else
  79. {
  80. newNamespaceNode = (TreeNode) namespaces[type.Namespace];
  81. }
  82.  
  83. if ( newNamespaceNode != null )
  84. {
  85. membersNode = newNamespaceNode.Nodes.Add(type.Name);
  86. if ( type.IsClass )
  87. {
  88. membersNode.Tag = MemberTypes.Custom;
  89. }
  90. }
  91. }
  92.  
  93. }
  94. else
  95. {
  96. // Single root namespace, add to root
  97. membersNode = treeViewItems.Nodes.Add(type.Namespace);
  98. }
  99.  
  100. // Add all members
  101. if ( membersNode != null )
  102. {
  103. this.addMembers(membersNode,type);
  104. }
  105. }
  106. }
  107.  
  108. }
  109. }
  110.  
  111. /// 
  112. /// Adds all members to the node's children, grabbing the parameters
  113. /// for methods.
  114. /// 
  115. /// 
  116. /// 
  117. private void addMembers(TreeNode treeNode,System.Type type)
  118. {
  119. // Get all members except methods
  120. MemberInfo[] memberInfo = type.GetMembers();
  121. for (int j=0;j < memberInfo.Length;j++)
  122. if ( memberInfo[j].ReflectedType.IsPublic && memberInfo[j].MemberType != MemberTypes.Method )
  123. {
  124. TreeNode node = treeNode.Nodes.Add(memberInfo[j].Name);
  125. node.Tag = memberInfo[j].MemberType; 
  126. }
  127. }
  128.  
  129. // Get all methods
  130. MethodInfo[] methodInfo = type.GetMethods();
  131. for (int j=0;j < methodInfo.Length;j++)
  132. {
  133. TreeNode node = treeNode.Nodes.Add(methodInfo[j].Name);
  134. string parms = "";
  135.  
  136. ParameterInfo[] parameterInfo = methodInfo[j].GetParameters();
  137. for (int f=0;f < parameterInfo.Length;f++)
  138. {
  139. parms += parameterInfo[f].ParameterType.ToString()+ " " +parameterInfo[f].Name+ ", ";
  140. }
  141.  
  142. // Knock off remaining ", "
  143. if ( parms.Length > 2 )
  144. {
  145. parms = parms.Substring(0,parms.Length -2);
  146. }
  147.  
  148. node.Tag = parms;
  149. }
  150. }
  151.  
  152. /// 
  153. /// Searches the tree view for a namespace, saving the node. The method
  154. /// stops and returns as soon as the namespace search can't find any
  155. /// more items in its path, unless continueUntilFind is true.
  156. /// 
  157. /// 
  158. /// 
  159. /// 
  160. private void searchTree(TreeNodeCollection treeNodes,string path,bool continueUntilFind)
  161. {
  162. if ( this.foundNode )
  163. {
  164. return;
  165. }
  166.  
  167. string p = "";
  168. int n = 0;
  169. n = path.IndexOf(".");
  170.  
  171. if ( n != -1)
  172. {
  173. p = path.Substring(0,n);
  174.  
  175. if ( currentPath != "" )
  176. {
  177. currentPath += "." +p;
  178. }
  179. else
  180. {
  181. currentPath = p;
  182. }
  183.  
  184. // Knock off the first part
  185. path = path.Remove(0,n+1);
  186. }
  187. else
  188. {
  189. currentPath += "." +path;
  190. }
  191.  
  192. for (int i=0;i < treeNodes.Count;i++)
  193. {
  194. if ( treeNodes[i].FullPath == currentPath )
  195. {
  196. if ( continueUntilFind )
  197. {
  198. nameSpaceNode = treeNodes[i];
  199. }
  200.  
  201. nameSpaceNode = treeNodes[i];
  202.  
  203. // got a dot, continue, or return
  204. this.searchTree(treeNodes[i].Nodes,path,continueUntilFind);
  205.  
  206. }
  207. else if ( !continueUntilFind )
  208. {
  209. foundNode = true;
  210. return;
  211. }
  212. }
  213. }
  214.  
  215. /// 
  216. /// Searches the tree until the given path is found, storing
  217. /// the found node in a member var.
  218. /// 
  219. /// 
  220. /// 
  221. private void findNode(string path,TreeNodeCollection treeNodes)
  222. {
  223. for (int i=0;i < treeNodes.Count;i++)
  224. {
  225. if ( treeNodes[i].FullPath == path )
  226. {
  227. this.findNodeResult = treeNodes[i];
  228. break;
  229. }
  230. else if ( treeNodes[i].Nodes.Count > 0 )
  231. {
  232. this.findNode(path,treeNodes[i].Nodes);
  233. }
  234. }
  235. }
  236.  
  237. /// 
  238. /// Called when a "." is pressed - the previous word is found,
  239. /// and if matched in the treeview, the members listbox is
  240. /// populated with items from the tree, which are first sorted.
  241. /// 
  242. /// Whether an items are found for the word
  243. private bool populateListBox()
  244. {
  245. bool result = false;
  246. string word = this.getLastWord();
  247.  
  248. //System.Diagnostics.Debug.WriteLine(" - Path: " +word);
  249.  
  250. if ( word != "" )
  251. {
  252. findNodeResult = null;
  253. findNode(word,this.treeViewItems.Nodes); 
  254.  
  255. if (this.findNodeResult != null )
  256. {
  257. this.listBoxAutoComplete.Items.Clear();
  258.  
  259. if ( this.findNodeResult.Nodes.Count > 0 )
  260. {
  261. result = true;
  262.  
  263. // Sort alphabetically (this could be replaced with
  264. // a sortable treeview)
  265. MemberItem[] items = new MemberItem[this.findNodeResult.Nodes.Count];
  266. for (int n=0;n < this.findNodeResult.Nodes.Count;n++)
  267. {
  268. MemberItem memberItem = new MemberItem();
  269. memberItem.DisplayText = this.findNodeResult.Nodes[n].Text;
  270. memberItem.Tag = this.findNodeResult.Nodes[n].Tag;
  271.  
  272. if ( this.findNodeResult.Nodes[n].Tag != null )
  273. {
  274. System.Diagnostics.Debug.WriteLine(this.findNodeResult.Nodes[n].Tag.GetType().ToString());
  275. }
  276.  
  277. items[n] = memberItem;
  278. }
  279. Array.Sort(items);
  280.  
  281. for (int n=0;n < items.Length;n++)
  282. {
  283. int imageindex = 0;
  284.  
  285. if ( items[n].Tag != null )
  286. {
  287. // Default to method (contains text for parameters)
  288. imageindex = 2;
  289. if ( items[n].Tag is MemberTypes)
  290. {
  291. MemberTypes memberType = (MemberTypes) items[n].Tag;
  292.  
  293. switch ( memberType )
  294. {
  295. case MemberTypes.Custom:
  296. imageindex = 1;
  297. break;
  298. case MemberTypes.Property:
  299. imageindex = 3;
  300. break;
  301. case MemberTypes.Event:
  302. imageindex = 4;
  303. break;
  304. }
  305. }
  306. }
  307.  
  308. this.listBoxAutoComplete.Items.Add(new GListBoxItem(items[n].DisplayText,imageindex) );
  309. }
  310. }
  311. }
  312. }
  313.  
  314. return result;
  315. }
  316.  
  317. /// 
  318. /// Autofills the selected item in the member listbox, by
  319. /// taking everything before and after the "." in the richtextbox,
  320. /// and appending the word in the middle.
  321. /// 
  322. private void selectItem()
  323. {
  324. if ( this.wordMatched )
  325. {
  326. int selstart = this.richTextBox1.SelectionStart;
  327. int prefixend = this.richTextBox1.SelectionStart - typed.Length;
  328. int suffixstart = this.richTextBox1.SelectionStart + typed.Length;
  329.  
  330. if ( suffixstart >= this.richTextBox1.Text.Length )
  331. {
  332. suffixstart = this.richTextBox1.Text.Length;
  333. }
  334.  
  335. string prefix = this.richTextBox1.Text.Substring(0,prefixend);
  336. string fill = this.listBoxAutoComplete.SelectedItem.ToString();
  337. string suffix = this.richTextBox1.Text.Substring(suffixstart,this.richTextBox1.Text.Length - suffixstart);
  338.  
  339. this.richTextBox1.Text = prefix + fill + suffix;
  340. this.richTextBox1.SelectionStart = prefix.Length + fill.Length;
  341. }
  342. }
  343.  
  344. /// 
  345. /// Searches backwards from the current caret position, until
  346. /// a space or newline is found.
  347. /// 
  348. /// The previous word from the carret position
  349. private string getLastWord()
  350. {
  351. string word = "";
  352.  
  353. int pos = this.richTextBox1.SelectionStart;
  354. if ( pos > 1 )
  355. {
  356.  
  357. string tmp = "";
  358. char f = new char();
  359. while ( f != ' ' && f != 10 && pos > 0 )
  360. {
  361. pos--;
  362. tmp = this.richTextBox1.Text.Substring(pos,1);
  363. f = (char) tmp[0];
  364. word += f; 
  365. }
  366.  
  367. char[] ca = word.ToCharArray();
  368. Array.Reverse( ca );
  369. word = new String( ca );
  370.  
  371. }
  372. return word.Trim();
  373.  
  374. }
  375. #endregion
  376.  
  377. private void FormMain_Load(object sender, EventArgs e)
  378. {
  379. readAssembly();
  380.  
  381.  
  382. }
  383. }
  384.  












Raja

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Jun 4 '07

re: loading multiple assemblies


So you wish to populate the autocomplete section of a listbox from a hashtable you have created by parsing .dll files for namespaces/classes and the like?
Could you not just keep passing more .dll locations and adding to your hashtable?
Reply