Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Demonstrating Custom Attributes: Build An Assembly Searching Tool.

, 25 Dec 2005 CPOL
Build an assembly searching system via custom attributes and reflection.
keywordattributesystem_src.zip
DirectoryLister
KeywordAttribute
Clients
Implementations
Interfaces
KeywordAttributeAssemblyLister
KeywordAttributedAssemblies
App.ico
KeywordAttributeAssemblyLister.csproj.user
KAAssembly01
KAClass01
KADelegate01
KAParameters01
KAProperty01
App.ico
KAAssembly01.csproj.user
keyfile.snk
KAClass01.csproj.user
keyfile.snk
KADelegate01.csproj.user
keyfile.snk
App.ico
KAParameters01.csproj.user
keyfile.snk
KAProperty01.csproj.user
keyfile.snk
AssemblyFilesLister
DirectoryListerBase
AssemblyFilesLister.csproj.user
DirectoryListerBase.csproj.user
DirectoryListerInterface
DirectoryListerInterface.csproj.user
keyfile.snk
KeywordAttribute.csproj.user
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)

Share

About the Author

Lim Bio Liong
Web Developer
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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 25 Dec 2005
Article Copyright 2005 by Lim Bio Liong
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid