Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / XML

Attributes Inspector

Rate me:
Please Sign up or sign in to vote.
4.55/5 (12 votes)
28 Jan 20032 min read 74.9K   731   25  
Simple program to inspect custom attributes in assemblies
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Text;
namespace ObsAtribs
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox NomAssem;
		private System.Windows.Forms.Button button1;
		private Assembly oAsAct=null;
		private System.Windows.Forms.RichTextBox richTextBox1;
		private System.Windows.Forms.ListBox AtribMem;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ListBox tipus;
		private System.Windows.Forms.CheckBox chShow;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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 Windows Form 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.NomAssem = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.tipus = new System.Windows.Forms.ListBox();
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.AtribMem = new System.Windows.Forms.ListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.chShow = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// NomAssem
			// 
			this.NomAssem.Location = new System.Drawing.Point(16, 16);
			this.NomAssem.Name = "NomAssem";
			this.NomAssem.Size = new System.Drawing.Size(480, 20);
			this.NomAssem.TabIndex = 0;
			this.NomAssem.Text = "Assembly name";
			this.NomAssem.DoubleClick += new System.EventHandler(this.NomAssem_Click);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(504, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(48, 23);
			this.button1.TabIndex = 1;
			this.button1.Text = "Load";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// tipus
			// 
			this.tipus.Location = new System.Drawing.Point(8, 72);
			this.tipus.Name = "tipus";
			this.tipus.Size = new System.Drawing.Size(544, 173);
			this.tipus.Sorted = true;
			this.tipus.TabIndex = 2;
			this.tipus.SelectedIndexChanged += new System.EventHandler(this.tipus_SelectedIndexChanged);
			// 
			// richTextBox1
			// 
			this.richTextBox1.Location = new System.Drawing.Point(8, 416);
			this.richTextBox1.Name = "richTextBox1";
			this.richTextBox1.Size = new System.Drawing.Size(544, 112);
			this.richTextBox1.TabIndex = 4;
			this.richTextBox1.Text = "Attributes";
			this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
			// 
			// AtribMem
			// 
			this.AtribMem.Location = new System.Drawing.Point(8, 280);
			this.AtribMem.Name = "AtribMem";
			this.AtribMem.Size = new System.Drawing.Size(544, 108);
			this.AtribMem.TabIndex = 5;
			this.AtribMem.SelectedIndexChanged += new System.EventHandler(this.AtribMem_SelectedIndexChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(288, 16);
			this.label1.TabIndex = 6;
			this.label1.Text = "Exported types";
			this.label1.Click += new System.EventHandler(this.label1_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 264);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(288, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "Properties && Members";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 400);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(288, 16);
			this.label3.TabIndex = 6;
			this.label3.Text = "Custom attributes";
			// 
			// chShow
			// 
			this.chShow.Location = new System.Drawing.Point(336, 392);
			this.chShow.Name = "chShow";
			this.chShow.Size = new System.Drawing.Size(168, 24);
			this.chShow.TabIndex = 7;
			this.chShow.Text = "Show field properties";
			this.chShow.CheckedChanged += new System.EventHandler(this.chShow_CheckedChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(560, 541);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.chShow,
																		  this.label1,
																		  this.AtribMem,
																		  this.richTextBox1,
																		  this.tipus,
																		  this.button1,
																		  this.NomAssem,
																		  this.label2,
																		  this.label3});
			this.Name = "Form1";
			this.Text = "Attribute Inspector";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}


		private void button1_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog oDiag=new OpenFileDialog();
			oDiag.CheckFileExists=true;
			oDiag.DereferenceLinks=true;
			oDiag.Multiselect=false;
			oDiag.RestoreDirectory=true;
			oDiag.Filter="Executable files (*.exe)|*.exe|Libraries (*.dll)|*.dll";
			oDiag.ShowDialog();
			
			try
			{
				oAsAct=Assembly.LoadFrom(oDiag.FileName);
				NomAssem.Text=oDiag.FileName+","+oAsAct.FullName;
				oDiag.Dispose();
				LListatipus();
				OmpleAtrs(oAsAct);
			}
			catch(Exception ex)
			{
				tipus.Items.Clear();
				AtribMem.Items.Clear();
				oAsAct=null;
				richTextBox1.Text="Exception loading assembly:\r\n"+ex.ToString().Replace("\n","\r\n");
			}
		}
		private void LListatipus()
		{
			tipus.Items.Clear();
			foreach(Type oTip in oAsAct.GetExportedTypes())
			{
				tipus.Items.Add(oTip);
			}			
		}
		private void LListaMetIProp(Type o)
		{
			AtribMem.Items.Clear();
			AtribMem.Items.Add(">>>Propertries<<<");
			foreach(PropertyInfo p in o.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic|BindingFlags.DeclaredOnly))
			{
				AtribMem.Items.Add(p);
			}
			AtribMem.Items.Add(">>>Methods<<<");
			foreach(MethodInfo m in o.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static| BindingFlags.NonPublic|BindingFlags.DeclaredOnly))
			{
				AtribMem.Items.Add(m);
			}
			AtribMem.Items.Add(">>>Fields<<<");
			foreach(FieldInfo f in o.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static| BindingFlags.NonPublic|BindingFlags.DeclaredOnly))
			{
				AtribMem.Items.Add(f);
			}
		}
		private void tipus_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (tipus.SelectedIndex!=-1)
			{
				OmpleAtrs(tipus.SelectedItem);
				LListaMetIProp((Type)tipus.SelectedItem);
			}
		}
		private void OmpleAtrs(object orig)
		{			
			object[] oAts=null;
			richTextBox1.Text="";			
			try
			{
				richTextBox1.SelectionColor=Color.Black;
				richTextBox1.AppendText(orig.GetType().Name+": ");
				richTextBox1.SelectionColor=Color.Red;
				if (orig is Assembly)
				{
					oAts=((Assembly)orig).GetCustomAttributes(true);								
					richTextBox1.AppendText(((Assembly)orig).FullName);
				}
				if (orig is Type)
				{
					oAts=((Type)orig).GetCustomAttributes(true);
					richTextBox1.AppendText(((Type)orig).FullName);
				}
				if (orig is PropertyInfo) 
				{
					oAts=((PropertyInfo)orig).GetCustomAttributes(true);
					richTextBox1.AppendText(((PropertyInfo)orig).Name);
				}
				if (orig is MethodInfo) 
				{
					oAts=((MethodInfo)orig).GetCustomAttributes(true);
					richTextBox1.AppendText(((MethodInfo)orig).Name);
				}
				if (orig is FieldInfo) 
				{
					oAts=((FieldInfo)orig).GetCustomAttributes(true);
					richTextBox1.AppendText(((FieldInfo)orig).Name);
				}
				richTextBox1.AppendText("\r\n\r\n");
				
				foreach(object oAt in oAts)
				{
					richTextBox1.SelectionColor=Color.Black;
					richTextBox1.AppendText(string.Format("{0} ",oAt.ToString()));
					richTextBox1.SelectionColor=Color.Red;
					//richTextBox1.AppendText(oAt.GetType().DeclaringType.Name);
					richTextBox1.SelectionColor=Color.Black;
					richTextBox1.AppendText(":\r\n");
					foreach(PropertyInfo prop in oAt.GetType().GetProperties(BindingFlags.NonPublic| BindingFlags.Instance))
					{
						richTextBox1.SelectionColor=Color.DarkGreen;
						richTextBox1.AppendText(" "+prop.Name);
						try
						{
							richTextBox1.SelectionColor=Color.Blue;
							richTextBox1.AppendText(":"+prop.GetValue(oAt,null));
							richTextBox1.AppendText("\r\n");
						} 
						catch(Exception ex)
						{
							richTextBox1.SelectionColor=Color.DarkMagenta;
							richTextBox1.AppendText(",exception:"+ex.Message);
							richTextBox1.AppendText("\r\n");
						};
					}	
					foreach(PropertyInfo prop in oAt.GetType().GetProperties(BindingFlags.Public| BindingFlags.Instance))
					{
						if (string.Compare(prop.Name,"typeid",true)!=0)
						{
							richTextBox1.SelectionColor=Color.Green;
							richTextBox1.AppendText(" "+prop.Name);
							try
							{
								richTextBox1.SelectionColor=Color.Blue;
								richTextBox1.AppendText(":"+prop.GetValue(oAt,null));
								richTextBox1.AppendText("\r\n");
							} 
							catch(Exception ex)
							{
								richTextBox1.SelectionColor=Color.DarkMagenta;
								richTextBox1.AppendText(",exception:"+ex.Message);
								richTextBox1.AppendText("\r\n");
							};
						}
					}	
					if (chShow.Checked)
					{
						foreach(FieldInfo prop in oAt.GetType().GetFields(BindingFlags.NonPublic| BindingFlags.Public| BindingFlags.Instance))
						{
							if (string.Compare(prop.Name,"typeid",true)!=0)
							{
								richTextBox1.SelectionColor=Color.BlueViolet;
								richTextBox1.AppendText(" "+prop.Name);
								try
								{
									richTextBox1.SelectionColor=Color.DarkGoldenrod;
									richTextBox1.AppendText(":"+prop.GetValue(oAt));
									richTextBox1.AppendText("\r\n");
								} 
								catch(Exception ex)
								{
									richTextBox1.SelectionColor=Color.DarkMagenta;
									richTextBox1.AppendText(",exception:"+ex.Message);
									richTextBox1.AppendText("\r\n");
								};
							}
						}
					}
					richTextBox1.AppendText("\r\n");
				}
			}
			catch(Exception e)
			{
				richTextBox1.Text="Exception obtaining attributes:\r\n"+e.ToString().Replace("\n","\r\n");
			}
		}

		private void richTextBox1_TextChanged(object sender, System.EventArgs e)
		{
		
		}

		private void AtribMem_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (AtribMem.SelectedIndex!=-1) OmpleAtrs(AtribMem.SelectedItem);
		}

		private void label1_Click(object sender, System.EventArgs e)
		{
		
		}

		private void NomAssem_Click(object sender, System.EventArgs e)
		{
			if (oAsAct!=null) OmpleAtrs(oAsAct);				
		}

		private void chShow_CheckedChanged(object sender, System.EventArgs e)
		{
			richTextBox1.Text="";
		}
	}
}

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.


Written By
Hzi
Web Developer Ajuntament del Vendrell
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions