Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / C#

Demonstrating Custom Attributes: Build An Assembly Searching Tool.

Rate me:
Please Sign up or sign in to vote.
4.56/5 (10 votes)
25 Dec 2005CPOL32 min read 49.7K   332   39  
Build an assembly searching system via custom attributes and reflection.
using System;
using DirectoryListerInterfaceNamespace;
using System.IO;

namespace DirectoryListerBaseNamespace
{
	/// <summary>
	/// Summary description for DirectoryListerBase.
	/// </summary>
	public class DirectoryListerBase : IDirectoryLister
	{
		#region Code pertaining to internal member data.
		string				m_strDirectoryToList;
		bool				m_bRecursive;
		DirectoryEntry		m_deRoot;
		#endregion

		#region Code pertaining to constructors and destructors.
		public DirectoryListerBase()
		{
			//
			// TODO: Add constructor logic here
			//
			InitMemberData();
		}
		#endregion

		#region Code pertaining to IDirectoryLister interface implementations

		public string DirectoryToList
		{
			get
			{
				return m_strDirectoryToList;
			}

			set
			{
				m_strDirectoryToList = value;
			}
		}

		public bool Recursive
		{
			get
			{
				return m_bRecursive;
			}
			set
			{
				m_bRecursive = value;
			}
		}

		public DirectoryEntry DirectoryEntry
		{
			get
			{
				return m_deRoot;
			}
		}

		public bool BeginListing()
		{
			if (m_strDirectoryToList == "")
			{
				return false;
			}

			m_deRoot.m_strName = m_strDirectoryToList;
			m_deRoot.m_lSize = 0;
			m_deRoot.m_dtLastModification = Directory.GetLastWriteTime(m_strDirectoryToList);

			GatherDirectories(m_strDirectoryToList, ref m_deRoot);

			return true;
		}

		#endregion

		#region Code pertaining to internal helper functions.
		protected virtual void InitMemberData()
		{
			m_strDirectoryToList = "";
			m_bRecursive = false;
		}

		protected virtual void GatherFiles(string strDirectoryName, ref DirectoryEntry directory_entry_to_set)
		{
			try
			{
				// Get all files from given directory name.
				string[]	strFileNames = Directory.GetFiles(strDirectoryName);
				int			iIndex = 0;

				directory_entry_to_set.m_feArray = new FileEntry[strFileNames.Length];

				foreach(string strFileName in strFileNames)
				{
					FileInfo fi = new FileInfo(strFileName);

					(directory_entry_to_set.m_feArray)[iIndex].m_strName = strFileName;
					(directory_entry_to_set.m_feArray)[iIndex].m_lSize = fi.Length;
					(directory_entry_to_set.m_feArray)[iIndex].m_dtLastModification = fi.LastWriteTime;

					iIndex++;
				}
			}
			catch(Exception e)
			{
				System.Console.WriteLine("Exception Message : {0}", e.Message);
				System.Console.WriteLine("Exception TargetSite : {0}", e.TargetSite);
				System.Console.WriteLine("Exception Source : {0}", e.Source);
			}
		}

		protected virtual void GatherDirectories(string strDirectoryName, ref DirectoryEntry directory_entry_to_set)
		{
			try
			{
				// First thing to do : gather all the files from the current directory.
				GatherFiles(strDirectoryName, ref directory_entry_to_set);

				string[]	strSubdirectoryNames = Directory.GetDirectories(strDirectoryName);
				int			iIndex = 0;

				// Next, gather all the sub-directories inside the current directory.
				directory_entry_to_set.m_deArray = new DirectoryEntry[strSubdirectoryNames.Length];

				iIndex = 0;
				foreach(string strSubdirectoryName in strSubdirectoryNames)
				{
					directory_entry_to_set.m_deArray[iIndex].m_strName = strSubdirectoryName;
					directory_entry_to_set.m_deArray[iIndex].m_lSize = 0;
					directory_entry_to_set.m_deArray[iIndex].m_dtLastModification = Directory.GetLastWriteTime(strSubdirectoryName);

					if (Recursive)
					{
						GatherDirectories(strSubdirectoryName, ref directory_entry_to_set.m_deArray[iIndex]);
					}

					iIndex++;
				}
			}
			catch(Exception e)
			{
				System.Console.WriteLine("Exception Message : {0}", e.Message);
				System.Console.WriteLine("Exception TargetSite : {0}", e.TargetSite);
				System.Console.WriteLine("Exception Source : {0}", e.Source);
			}
		}

		#endregion
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer NEC
Singapore Singapore
Lim Bio Liong is a Specialist at a leading Software House in Singapore.

Bio has been in software development for over 10 years. He specialises in C/C++ programming and Windows software development.

Bio has also done device-driver development and enjoys low-level programming. Bio has recently picked up C# programming and has been researching in this area.

Comments and Discussions