Assembly Tree Viewer : Assembly « Reflection « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Reflection » Assembly 
19.12.6.Assembly Tree Viewer
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Text;

  public class Form1 : System.Windows.Forms.Form
  {
        private System.Windows.Forms.MainMenu mainMenu1  = new System.Windows.Forms.MainMenu();
        private System.Windows.Forms.MenuItem menuItem1 = new System.Windows.Forms.MenuItem();
        private System.Windows.Forms.MenuItem mnuOpen = new System.Windows.Forms.MenuItem();
        private System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        private Assembly mAssembly;
        private System.Windows.Forms.TreeView carAssembly = new System.Windows.Forms.TreeView();
        private System.Windows.Forms.PropertyGrid pgObject = new System.Windows.Forms.PropertyGrid();

    public Form1()
    {
            this.SuspendLayout();

            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1});
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.mnuOpen});
            this.menuItem1.Text = "&File";

            this.mnuOpen.Index = 0;
            this.mnuOpen.Text = "&Open";
            this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);

            this.openFileDialog1.CheckFileExists = false;
            this.openFileDialog1.CheckPathExists = false;
            this.openFileDialog1.Filter = "Assemblies|*.exe;*.dll";
            this.openFileDialog1.ValidateNames = false;

            this.carAssembly.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left| System.Windows.Forms.AnchorStyles.Right)));
            this.carAssembly.ImageIndex = -1;
            this.carAssembly.Location = new System.Drawing.Point(88);
            this.carAssembly.Name = "carAssembly";
            this.carAssembly.SelectedImageIndex = -1;
            this.carAssembly.Size = new System.Drawing.Size(672400);
            this.carAssembly.TabIndex = 0;
            this.carAssembly.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.carAssembly_AfterSelect);

            this.pgObject.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
                | System.Windows.Forms.AnchorStyles.Left
                | System.Windows.Forms.AnchorStyles.Right)));
            this.pgObject.BackColor = System.Drawing.Color.Gray;
            this.pgObject.CommandsBackColor = System.Drawing.Color.Gray;
            this.pgObject.CommandsForeColor = System.Drawing.Color.Gray;
            this.pgObject.CommandsVisibleIfAvailable = true;
            this.pgObject.HelpBackColor = System.Drawing.Color.Gray;
            this.pgObject.HelpForeColor = System.Drawing.Color.White;
            this.pgObject.HelpVisible = false;
            this.pgObject.LargeButtons = false;
            this.pgObject.LineColor = System.Drawing.Color.Gray;
            this.pgObject.Location = new System.Drawing.Point(8416);
            this.pgObject.Name = "pgObject";
            this.pgObject.Size = new System.Drawing.Size(672128);
            this.pgObject.TabIndex = 1;
            this.pgObject.Text = "propertyGrid1";
            this.pgObject.ToolbarVisible = false;
            this.pgObject.ViewBackColor = System.Drawing.Color.Gray;
            this.pgObject.ViewForeColor = System.Drawing.Color.White;

            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(688553);
            this.Controls.Add(this.pgObject);
            this.Controls.Add(this.carAssembly);
            this.Menu = this.mainMenu1;
            this.Text = "Assembly Viewer";
            this.ResumeLayout(false);

        }
    [STAThread]
    static void Main() 
    {
      Application.Run(new Form1());
    }

        private void mnuOpen_Click(object sender, System.EventArgs e)
        {
            ifopenFileDialog1.ShowDialog(this== DialogResult.OK )
            {
                try
                {
                    mAssembly = Assembly.LoadFileopenFileDialog1.FileName );
                    PopulateTree();
                }
                catchException ex )
                {
                    MessageBox.Showex.Message )
                }
            }
        }

        private void PopulateTree()
        {
            TreeNode newNode = new TreeNodemAssembly.GetName().Name );
            newNode.Tag = mAssembly;
            carAssembly.Nodes.AddnewNode );

            foreachModule mod in mAssembly.GetModules() )
            {
                AddModulemod, newNode );
            }
        }

        private void AddModuleModule mod, TreeNode parent )
        {
            TreeNode newNode = new TreeNodemod.Name );
            newNode.Tag = mod;
            parent.Nodes.AddnewNode );

            foreachType t in mod.GetTypes() )
            {
                AddTypet, newNode );
            }
        }

        private void AddTypeType t, TreeNode parent )
        {
            TreeNode newNode = new TreeNodet.Name );
            newNode.Tag = t;
            TreeNode curType;
            TreeNode curMember;

            curType = new TreeNode"Constructors" );
            foreachConstructorInfo constructor in t.GetConstructors() )
            {
                curMember = new TreeNodeconstructor.Name );
                curMember.Tag = constructor;
                curType.Nodes.AddcurMember );
            }
            newNode.Nodes.AddcurType );

            curType = new TreeNode"Methods" );
            foreachMethodInfo method in t.GetMethods() )
            {
                string methodString = method.Name + "( ";
                int count = method.GetParameters().Length;

                foreachParameterInfo param in method.GetParameters() )
                {
                    methodString += param.ParameterType;
                    ifparam.Position < count-)
                        methodString += ", ";
                }
                methodString += " )";
                curMember = new TreeNodemethodString );
                curMember.Tag = method;
                curType.Nodes.AddcurMember );
            }
            newNode.Nodes.AddcurType );

            curType = new TreeNode"Properties" );
            foreachPropertyInfo property in t.GetProperties() )
            {
                curMember = new TreeNodeproperty.Name );
                curMember.Tag = property;
                curType.Nodes.AddcurMember );
            }
            newNode.Nodes.AddcurType );

            curType = new TreeNode"Fields" );
            foreachFieldInfo field in t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.GetField) )
            {
                string fieldInfo = field.FieldType.Name;
                fieldInfo += " " + field.Name;
                curMember = new TreeNodefieldInfo );
                curMember.Tag = field;
                curType.Nodes.AddcurMember );
            }
            newNode.Nodes.AddcurType );

            curType = new TreeNode"Events" );
            foreachEventInfo curEvent in t.GetEvents() )
            {
                string eventInfo = curEvent.Name;
                eventInfo += " Delegate Type=" + curEvent.EventHandlerType;
                curMember = new TreeNodeeventInfo );
                curMember.Tag = curEvent;
                curType.Nodes.AddcurMember );
            }
            newNode.Nodes.AddcurType );

            parent.Nodes.AddnewNode );
        }

        private void carAssembly_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            ife.Node.Tag != null )
            {
                pgObject.SelectedObject = e.Node.Tag;
            }
            else
            {
                pgObject.SelectedObject = null;
            }
        }
  }
19.12.Assembly
19.12.1.Load Assembly from Dll
19.12.2.Reflecting An Assembly
19.12.3.Reflecting On A Type
19.12.4.Execute Assembly
19.12.5.Search member method in Assembly
19.12.6.Assembly Tree Viewer
19.12.7.DefineDynamicAssembly method and AssemblyResolve event.
19.12.8.Assembly makes internal types and internal members visible to the assembly called your_assemblies.
19.12.9.Use the file name to load the assembly into the current application domain.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.