using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsProgramTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void openAssembleToolStripMenuItem_Click(object sender, EventArgs e) { var openFile = new OpenFileDialog(); if (openFile.ShowDialog(this) == DialogResult.OK) { var assembly = Assembly.LoadFile(openFile.FileName); var assemblyNode = new TreeNode(assembly.GetName().Name); this.treeView1.Nodes.Add(assemblyNode); //module var modules = assembly.GetModules(); //foreach (var module in modules) foreach (var module in modules) { //module view var moduleNode = new TreeNode(module.Name); assemblyNode.Nodes.Add(moduleNode); //reference var referenceNode = new TreeNode("Reference"); moduleNode.Nodes.Add(referenceNode); var refers = assembly.GetReferencedAssemblies(); foreach (var refer in refers) { var referNode = new TreeNode(refer.Name); referenceNode.Nodes.Add(referNode); } //types Type[] typearr = null; try { typearr= module.GetTypes(); } catch(ReflectionTypeLoadException ex) { typearr = ex.Types; } //字典 分离键值对key-value(mul) var typeGroup = new System.Collections.Generic.Dictionary <String, List<Type>>(); foreach (var type in typearr) { if (type == null) continue; string np = null; try { np = type.Namespace; } catch { } if (String.IsNullOrEmpty(np)) { np = "-"; } if (typeGroup.ContainsKey(np)) { typeGroup[np].Add(type); continue; } var list = new List<Type>(); list.Add(type); typeGroup.Add(np, list); } //显示namespace和class foreach (var typeItem in typeGroup) { var nameSpaceNode = new TreeNode(typeItem.Key); moduleNode.Nodes.Add(nameSpaceNode); foreach ( var type in typeItem.Value) { var classNode = new TreeNode(type.Name); nameSpaceNode.Nodes.Add(classNode); //construc var constructInfos = type.GetConstructors(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static|BindingFlags.DeclaredOnly|BindingFlags.Instance); ParameterInfo[] consParameters = null; String parsList = String.Empty; try { foreach (var cons in constructInfos) { consParameters = cons.GetParameters(); var conlist = new List<String>(); foreach (var consParameter in consParameters) { conlist.Add(consParameter.ParameterType.Name); } parsList = String.Join(" , ", conlist.ToArray()); var constructNode = new TreeNode(cons.Name + "(" + parsList + ")"); classNode.Nodes.Add(constructNode); } } catch { } //method var methods = type.GetMethods(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static|BindingFlags.DeclaredOnly|BindingFlags.Instance); foreach (var method in methods) { var methodName = method.Name; if (method.IsSpecialName) { continue; } string paramStr = string.Empty; String methodRT = null; try { var parameters = method.GetParameters(); var paraList = new List<String>(); foreach (var parameter in parameters) { paraList.Add(parameter.ParameterType.Name); } paramStr = String.Join(", ", paraList.ToArray()); methodRT = method.ReturnType.Name; } catch { paramStr = "UnKnow"; methodRT = "methodRT_UnKnow"; } var methodNode = new TreeNode(method.Name + "(" + paramStr + ")" + " : " + methodRT); classNode.Nodes.Add(methodNode); } //property var proInfos = type.GetProperties(); foreach (var proinfo in proInfos) { TreeNode proNode = null; try { proNode = new TreeNode(proinfo.Name + " : " + proinfo.PropertyType.Name); classNode.Nodes.Add(proNode); } catch { proNode = new TreeNode(proinfo.Name + " : " + "UnKnow"); classNode.Nodes.Add(proNode); } MethodInfo promethods = null; ParameterInfo[] proParameters = null; List<String> proList = null; String parsStr2 = String.Empty; String prt = null; if (proinfo.CanRead) { try { promethods = proinfo.GetMethod; proParameters = promethods.GetParameters(); proList = new List<String>(); foreach (var proParameter in proParameters) { proList.Add(proParameter.ParameterType.Name); } parsStr2 = string.Join(", ", proList.ToArray()); prt = promethods.ReturnType.Name; } catch { } var proInfoNode = new TreeNode(promethods.Name + "(" + parsStr2 + ")" + prt ); proNode.Nodes.Add(proInfoNode); } if (proinfo.CanWrite) { try { promethods = proinfo.GetMethod; proParameters = promethods.GetParameters(); proList = new List<String>(); foreach (var proParameter in proParameters) { proList.Add(proParameter.ParameterType.Name); } } catch { } var proInfoNode = new TreeNode(promethods.Name + "(" + string.Join(", ", proList.ToArray()) + ")" + promethods.ReturnType.Name); proNode.Nodes.Add(proInfoNode); } //filds var filds = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); try { foreach (var fildsInfo in filds) { var fildNode = new TreeNode(fildsInfo.Name + ": " + fildsInfo.FieldType.Name); classNode.Nodes.Add(fildNode); } } catch { } } } } } } } } }
时间: 2024-11-05 17:31:25