Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / MFC

Assembly Explorer 1.0

Rate me:
Please Sign up or sign in to vote.
4.46/5 (22 votes)
21 Jan 20032 min read 110.4K   3K   39  
Simple utility to browse assembly object hierarchy
//=============================================================================== 
// Name          : MethodProperties.cs
// Purpose       : Method properties page.
// Description   : 
// Created       : 05.Nov.2002 by Langleyben Leon, leon@srl.co.il
// Last modified : 
//===============================================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Reflection;

namespace AssemblyExplorer
{
	/// <summary>
	/// Summary description for MethodProperties.
	/// </summary>
	public class MethodProperties : System.Windows.Forms.UserControl, IProperties
	{
		private MethodBase m_Metod;
		private ArrayList paramControls = new ArrayList();

		#region Control Members
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label lblReturnType;
		private System.Windows.Forms.Label lblNoParams;
		private System.Windows.Forms.Label lblVisibility;
		private System.Windows.Forms.Label lblIsVirtual;
		private System.Windows.Forms.Label lblIsAbstract;
		private System.Windows.Forms.Label lblIsConstructor;
		private System.Windows.Forms.Label lblOwnerScope;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button cmdExecute;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label lblResult;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label lblCustAttributes;
		private System.Windows.Forms.ListBox lbAttr;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label lblReturnTypeLabel;
		private System.Windows.Forms.Label lblCallingConvention;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.Label lblIsHideBySig;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.Label lblIsSpecialName;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MethodProperties()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label10 = new System.Windows.Forms.Label();
			this.lblReturnType = new System.Windows.Forms.Label();
			this.lblNoParams = new System.Windows.Forms.Label();
			this.lblVisibility = new System.Windows.Forms.Label();
			this.lblIsVirtual = new System.Windows.Forms.Label();
			this.lblIsAbstract = new System.Windows.Forms.Label();
			this.lblIsHideBySig = new System.Windows.Forms.Label();
			this.lblIsConstructor = new System.Windows.Forms.Label();
			this.lblOwnerScope = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.lblReturnTypeLabel = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.cmdExecute = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.lblResult = new System.Windows.Forms.Label();
			this.lblCustAttributes = new System.Windows.Forms.Label();
			this.label9 = new System.Windows.Forms.Label();
			this.lbAttr = new System.Windows.Forms.ListBox();
			this.label8 = new System.Windows.Forms.Label();
			this.lblCallingConvention = new System.Windows.Forms.Label();
			this.label11 = new System.Windows.Forms.Label();
			this.lblIsSpecialName = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(8, 288);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(80, 16);
			this.label10.TabIndex = 37;
			this.label10.Text = "Parameters:";
			// 
			// lblReturnType
			// 
			this.lblReturnType.Location = new System.Drawing.Point(112, 200);
			this.lblReturnType.Name = "lblReturnType";
			this.lblReturnType.Size = new System.Drawing.Size(168, 80);
			this.lblReturnType.TabIndex = 36;
			// 
			// lblNoParams
			// 
			this.lblNoParams.Location = new System.Drawing.Point(88, 288);
			this.lblNoParams.Name = "lblNoParams";
			this.lblNoParams.Size = new System.Drawing.Size(40, 16);
			this.lblNoParams.TabIndex = 35;
			this.lblNoParams.Text = "None";
			this.lblNoParams.Visible = false;
			// 
			// lblVisibility
			// 
			this.lblVisibility.Location = new System.Drawing.Point(112, 8);
			this.lblVisibility.Name = "lblVisibility";
			this.lblVisibility.Size = new System.Drawing.Size(168, 23);
			this.lblVisibility.TabIndex = 34;
			// 
			// lblIsVirtual
			// 
			this.lblIsVirtual.Location = new System.Drawing.Point(112, 152);
			this.lblIsVirtual.Name = "lblIsVirtual";
			this.lblIsVirtual.Size = new System.Drawing.Size(168, 23);
			this.lblIsVirtual.TabIndex = 33;
			// 
			// lblIsAbstract
			// 
			this.lblIsAbstract.Location = new System.Drawing.Point(112, 104);
			this.lblIsAbstract.Name = "lblIsAbstract";
			this.lblIsAbstract.Size = new System.Drawing.Size(168, 23);
			this.lblIsAbstract.TabIndex = 32;
			// 
			// lblIsHideBySig
			// 
			this.lblIsHideBySig.Location = new System.Drawing.Point(112, 128);
			this.lblIsHideBySig.Name = "lblIsHideBySig";
			this.lblIsHideBySig.Size = new System.Drawing.Size(168, 23);
			this.lblIsHideBySig.TabIndex = 31;
			// 
			// lblIsConstructor
			// 
			this.lblIsConstructor.Location = new System.Drawing.Point(112, 176);
			this.lblIsConstructor.Name = "lblIsConstructor";
			this.lblIsConstructor.Size = new System.Drawing.Size(168, 24);
			this.lblIsConstructor.TabIndex = 30;
			// 
			// lblOwnerScope
			// 
			this.lblOwnerScope.Location = new System.Drawing.Point(112, 32);
			this.lblOwnerScope.Name = "lblOwnerScope";
			this.lblOwnerScope.Size = new System.Drawing.Size(168, 23);
			this.lblOwnerScope.TabIndex = 29;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 152);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(80, 23);
			this.label7.TabIndex = 28;
			this.label7.Text = "Is Virtual";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 176);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(80, 23);
			this.label6.TabIndex = 27;
			this.label6.Text = "Is Constructor";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 104);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 23);
			this.label5.TabIndex = 26;
			this.label5.Text = "Is Abstract";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 128);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 23);
			this.label4.TabIndex = 25;
			this.label4.Text = "Is Hide By Sig";
			// 
			// lblReturnTypeLabel
			// 
			this.lblReturnTypeLabel.Location = new System.Drawing.Point(8, 200);
			this.lblReturnTypeLabel.Name = "lblReturnTypeLabel";
			this.lblReturnTypeLabel.Size = new System.Drawing.Size(80, 23);
			this.lblReturnTypeLabel.TabIndex = 24;
			this.lblReturnTypeLabel.Text = "Return Type";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 23);
			this.label2.TabIndex = 23;
			this.label2.Text = "Owner Scope";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 23);
			this.label1.TabIndex = 22;
			this.label1.Text = "Visibility";
			// 
			// cmdExecute
			// 
			this.cmdExecute.Location = new System.Drawing.Point(8, 448);
			this.cmdExecute.Name = "cmdExecute";
			this.cmdExecute.Size = new System.Drawing.Size(88, 24);
			this.cmdExecute.TabIndex = 21;
			this.cmdExecute.Text = "Execute";
			this.cmdExecute.Click += new System.EventHandler(this.cmdExecute_Click);
			// 
			// panel1
			// 
			this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.panel1.AutoScroll = true;
			this.panel1.Location = new System.Drawing.Point(8, 304);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(448, 136);
			this.panel1.TabIndex = 20;
			// 
			// textBox1
			// 
			this.textBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textBox1.Location = new System.Drawing.Point(8, 496);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(448, 56);
			this.textBox1.TabIndex = 19;
			this.textBox1.Text = "";
			// 
			// lblResult
			// 
			this.lblResult.Location = new System.Drawing.Point(8, 480);
			this.lblResult.Name = "lblResult";
			this.lblResult.Size = new System.Drawing.Size(100, 16);
			this.lblResult.TabIndex = 38;
			this.lblResult.Text = "Results:";
			// 
			// lblCustAttributes
			// 
			this.lblCustAttributes.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.lblCustAttributes.Location = new System.Drawing.Point(288, 176);
			this.lblCustAttributes.Name = "lblCustAttributes";
			this.lblCustAttributes.Size = new System.Drawing.Size(168, 80);
			this.lblCustAttributes.TabIndex = 40;
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(288, 8);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(80, 23);
			this.label9.TabIndex = 39;
			this.label9.Text = "Attributes:";
			// 
			// lbAttr
			// 
			this.lbAttr.BackColor = System.Drawing.SystemColors.Control;
			this.lbAttr.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lbAttr.Location = new System.Drawing.Point(288, 26);
			this.lbAttr.Name = "lbAttr";
			this.lbAttr.SelectionMode = System.Windows.Forms.SelectionMode.None;
			this.lbAttr.Size = new System.Drawing.Size(128, 119);
			this.lbAttr.TabIndex = 42;
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(288, 152);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(104, 23);
			this.label8.TabIndex = 43;
			this.label8.Text = "Custom Attributes:";
			// 
			// lblCallingConvention
			// 
			this.lblCallingConvention.Location = new System.Drawing.Point(112, 56);
			this.lblCallingConvention.Name = "lblCallingConvention";
			this.lblCallingConvention.Size = new System.Drawing.Size(168, 23);
			this.lblCallingConvention.TabIndex = 45;
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(8, 56);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(104, 23);
			this.label11.TabIndex = 44;
			this.label11.Text = "Calling Convention";
			// 
			// lblIsSpecialName
			// 
			this.lblIsSpecialName.Location = new System.Drawing.Point(112, 80);
			this.lblIsSpecialName.Name = "lblIsSpecialName";
			this.lblIsSpecialName.Size = new System.Drawing.Size(168, 23);
			this.lblIsSpecialName.TabIndex = 47;
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(8, 80);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(88, 23);
			this.label12.TabIndex = 46;
			this.label12.Text = "Is Special Name";
			// 
			// MethodProperties
			// 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.lblIsSpecialName,
																		  this.label12,
																		  this.lblCallingConvention,
																		  this.label11,
																		  this.label8,
																		  this.lbAttr,
																		  this.lblCustAttributes,
																		  this.label9,
																		  this.lblResult,
																		  this.label10,
																		  this.lblReturnType,
																		  this.lblNoParams,
																		  this.lblVisibility,
																		  this.lblIsVirtual,
																		  this.lblIsAbstract,
																		  this.lblIsHideBySig,
																		  this.lblIsConstructor,
																		  this.lblOwnerScope,
																		  this.label7,
																		  this.label6,
																		  this.label5,
																		  this.label4,
																		  this.lblReturnTypeLabel,
																		  this.label2,
																		  this.label1,
																		  this.cmdExecute,
																		  this.panel1,
																		  this.textBox1});
			this.Name = "MethodProperties";
			this.Size = new System.Drawing.Size(464, 560);
			this.ResumeLayout(false);

		}
		#endregion

		#endregion

		/// <summary>
		/// Initialize property page.
		/// </summary>
		private void Init()
		{
			Label lbl = null;
			TextBox txt = null;

			lblOwnerScope.Text = (m_Metod.IsStatic) ? "Static" : "Instance";
			lblIsVirtual.Text = m_Metod.IsVirtual.ToString();
			lblIsAbstract.Text = m_Metod.IsAbstract.ToString();
			lblIsConstructor.Text = m_Metod.IsConstructor.ToString();
			lblIsSpecialName.Text = m_Metod.IsSpecialName.ToString();
			lblIsHideBySig.Text = m_Metod.IsHideBySig.ToString();
			if (m_Metod is ConstructorInfo)
			{
				lblReturnType.Visible = false;
				lblReturnTypeLabel.Visible = false;
				lblIsConstructor.Text = "True";
			}
			else
			{
				lblReturnType.Text = ((MethodInfo)m_Metod).ReturnType.ToString();
				if (lblReturnType.Text.Length == 0)
					lblReturnType.Text = "void";
			}


			lblCallingConvention.Text = Utils.ListAttributes( typeof( System.Reflection.CallingConventions), (int) m_Metod.CallingConvention );
			if (m_Metod.IsPrivate )
				lblVisibility.Text = "Private";
			else if (m_Metod.IsPublic)
				lblVisibility.Text = "Public";
			else if (m_Metod.IsFamily)
				lblVisibility.Text = "Protected";
			else if (m_Metod.IsFamilyOrAssembly)
				lblVisibility.Text = "Internal Protected";
			else if (m_Metod.IsAssembly)
				lblVisibility.Text = "Internal";

			// Get Method attributes
			MethodAttributes attr = m_Metod.Attributes;
			lbAttr.Items.Clear();
			lbAttr.Items.AddRange(Utils.ListAttributes( typeof( System.Reflection.MethodAttributes ), (int) attr ).Split(','));

			// Get Custom attributes
			Object[] custAttr = m_Metod.GetCustomAttributes(true);
			lblCustAttributes.Text = Utils.ListObjects(custAttr);
			

				
			ParameterInfo[] parameters = null;
			if (m_Metod is MethodInfo)
				parameters = ((MethodInfo)m_Metod).GetParameters();
			else
				parameters = ((ConstructorInfo)m_Metod).GetParameters();

			bool isExecutable = CanRun();
			cmdExecute.Visible = isExecutable;
			textBox1.Text = String.Empty;
			textBox1.Visible = isExecutable;
			lblResult.Visible = isExecutable;
			panel1.Controls.Clear();
			paramControls.Clear();

			if (parameters.Length == 0)
			{
				lblNoParams.Visible = true;
				panel1.Visible = false;
				return;
			}
			else
			{
				lblNoParams.Visible = false;
				panel1.Visible = true;
			}
			for (int i = 0, j = 0; i < parameters.Length; i++)
			{
				paramControls.Add(new System.Windows.Forms.Label());
				lbl = (Label)paramControls[j++];
				lbl.Name = "lblName" + i;
				lbl.Location = new System.Drawing.Point(8, 24*i);
				if (parameters[i].IsRetval)
				{
					lbl.Text = "Return:";
				}
				else
				{
					lbl.Text = parameters[i].Name;
					paramControls.Add(new System.Windows.Forms.TextBox());
					txt = (TextBox)paramControls[j++];
					txt.Text = parameters[i].DefaultValue.ToString();
					txt.Name = "txtVal" + i;
					txt.TabIndex = i;
					txt.Location = new System.Drawing.Point(112, 24*i);
					txt.Enabled = isExecutable;
					this.panel1.Controls.Add(txt);
					string str = "[";
					if (parameters[i].IsIn)
						str += "in";
					if (parameters[i].IsOut)
					{
						if (str.Length > 1)
							str += ",out";
						else
							str += "out";
					}
					if (str.Length > 1)
					{
						paramControls.Add(new System.Windows.Forms.Label());
						lbl = (Label)paramControls[j++];
						lbl.Text = str;
						lbl.Location = new System.Drawing.Point(216, 24*i);
						lbl.Size = new System.Drawing.Size(40, 16);
						this.panel1.Controls.Add(lbl);
					}
				}
				this.panel1.Controls.Add(lbl);
				paramControls.Add(new System.Windows.Forms.Label());
				lbl = (Label)paramControls[j++];
				lbl.Text = parameters[i].ParameterType.ToString();
				lbl.Name = "lblType" + i;
				lbl.Location = new System.Drawing.Point(256, 24*i);
				lbl.Size = new System.Drawing.Size(184, 16);
				lbl.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
					| System.Windows.Forms.AnchorStyles.Right);
				this.panel1.Controls.Add(lbl);
			}
		}

		private void cmdExecute_Click(object sender, System.EventArgs e)
		{
			ParameterInfo[] parameters = m_Metod.GetParameters();
			object[] args = new Object[parameters.Length];
			
			try
			{
				for (int i = 0, j = 0; i < this.panel1.Controls.Count; i++)
					if (this.panel1.Controls[i].Name == "txtVal" + j)
					{					 
						args[j] = Convert.ChangeType(this.panel1.Controls[i].Text, parameters[j].ParameterType);
						j++;
					}
			}
			catch
			{
				textBox1.ForeColor = System.Drawing.Color.Red;
				textBox1.Text = "Unable to parse parameters for this method";
				return;
			}

			try
			{
				textBox1.ForeColor = System.Drawing.Color.Black;
				object o = m_Metod.Invoke(null, args);
				textBox1.Text = (o != null) ? o.ToString() : "OK";
				
			}
			catch (Exception ex)
			{
				textBox1.ForeColor = System.Drawing.Color.Red;
				textBox1.Text = ex.ToString();
			}
		}

		/// <summary>
		/// IProperties implementation
		/// </summary>
		/// <param name="val"></param>
		public void SetObject(object val)
		{
			m_Metod = (MethodBase)val;
			Init();
			this.Visible = true;
		}
		/// <summary>
		/// Check if method could be execurted.
		/// Currently you can run static methods only.
		/// </summary>
		/// <returns>True if method can be executed.</returns>
		private bool CanRun()
		{
			if (!m_Metod.IsStatic)
				return false;
			if (m_Metod.IsAbstract)
				return false;
			if (m_Metod is ConstructorInfo)
				return false;

			return true;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
Leon works as Chief Architect at SRL Group. He leads architectural design and development of various enterprise level projects.
You can meet him on user groups, conferences and forums dedicated to Architecture, ASP.NET, Team System etc. or join him for the next white water rafting adventure

Comments and Discussions