Click here to Skip to main content
15,896,417 members
Articles / Desktop Programming / WPF

A framework for comprehensive validation of user input

Rate me:
Please Sign up or sign in to vote.
4.70/5 (8 votes)
19 Jun 2012CPOL28 min read 31.3K   523   18  
Validation of input made as easy as possible for Windows.Forms, WPF, console-applications or any other purposes
// Copyright (c) 2005 - 2012, Andreas Ganzer. All Rights reserved.

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace Ganzer
{
	//############################################################################
	/// <summary>
	/// The AssemblyInfo class encapsulates the access to assembly specific
	/// informations.
	/// </summary>
	/// 
	/// <remarks>
	/// To get informations about the assembly of the application, it may be a
	/// more easy way to use the static methods of <see cref="ApplicationInfo"/>.
	/// </remarks>
	/// 
	public class AssemblyInfo
	{
		#region fields

		private Assembly __assembly;

		#endregion

		#region properties

		/// <summary>
		/// Returns the title attribute of the assembly.
		/// </summary>
		/// 
		public string Title
		{
			get
			{
				object[] attributes = __assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);

				if( attributes.Length > 0 )
				{
					AssemblyTitleAttribute titleattr = (AssemblyTitleAttribute)attributes[0];

					if( !string.IsNullOrEmpty(titleattr.Title) )
						return titleattr.Title;
				}

				return Path.GetFileNameWithoutExtension(__assembly.CodeBase);
			}
		}

		/// <summary>
		/// Returns the copyright information attribute of the assembly.
		/// </summary>
		/// 
		public string Copyright
		{
			get
			{
				object[] attributes = __assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);

				if( attributes.Length == 0 )
					return string.Empty;

				return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
			}
		}

		/// <summary>
		/// Returns the description attribute of the assembly.
		/// </summary>
		/// 
		public string Description
		{
			get
			{
				object[] attributes = __assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);

				if( attributes.Length == 0 )
					return string.Empty;

				return ((AssemblyDescriptionAttribute)attributes[0]).Description;
			}
		}

		/// <summary>
		/// Returns the product name attribute of the assembly.
		/// </summary>
		/// 
		public string ProductName
		{
			get
			{
				object[] attributes = __assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);

				if( attributes.Length == 0 )
					return string.Empty;

				return ((AssemblyProductAttribute)attributes[0]).Product;
			}
		}

		/// <summary>
		/// Returns the company name attribute of the assembly.
		/// </summary>
		/// 
		public string CompanyName
		{
			get
			{
				object[] attributes = __assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);

				if( attributes.Length == 0 )
					return string.Empty;

				return ((AssemblyCompanyAttribute)attributes[0]).Company;
			}
		}

		/// <summary>
		/// Returns the trademark attribute of the assembly.
		/// </summary>
		/// 
		public string Trademark
		{
			get
			{
				object[] attributes = __assembly.GetCustomAttributes(typeof(AssemblyTrademarkAttribute), false);

				if( attributes.Length == 0 )
					return string.Empty;

				return ((AssemblyTrademarkAttribute)attributes[0]).Trademark;
			}
		}

		/// <summary>
		/// Returns the <see cref="System.Version"/> object of the assembly.
		/// </summary>
		/// 
		public Version AssemblyVersion
		{
			get
			{
				return __assembly.GetName().Version;
			}
		}

		/// <summary>
		/// Returns the <see cref="FileVersionInfo"/> object of the assembly.
		/// </summary>
		/// 
		public FileVersionInfo FileVersion
		{
			get
			{
				return FileVersionInfo.GetVersionInfo(__assembly.Location);
			}
		}

		/// <summary>
		/// Returns the directory where the assembly is located.
		/// </summary>
		/// 
		public string DirectoryName
		{
			get
			{
				return Path.GetDirectoryName(__assembly.Location);
			}
		}

		/// <summary>
		/// Returns the name of the assembly. This is normally the name of the
		/// manifest file excluding the extension (the name is "MyAssembly" for
		/// the file "MyAssembly.dll").
		/// </summary>
		/// 
		public string Name
		{
			get
			{
				return __assembly.GetName().Name;
			}
		}

		/// <summary>
		/// Returns the <see cref="System.Reflection.AssemblyName"/> object of
		/// the assembly.
		/// </summary>
		/// 
		public AssemblyName AssemblyName
		{
			get
			{
				return __assembly.GetName();
			}
		}

		/// <summary>
		/// Returns the full name of the assembly including the directory
		/// and the extension.
		/// </summary>
		/// 
		public string FullPath
		{
			get
			{
				return __assembly.Location;
			}
		}

		/// <summary>
		/// Returns the qualified name of the assembly. This contains the name
		/// ot the file as well as the version, the culture and the public key
		/// token if available.
		/// </summary>
		/// 
		public string QualifiedName
		{
			get
			{
				return __assembly.FullName;
			}
		}

		#endregion

		#region ctor/dtor

		/// <summary>
		/// Initializes this object with the given argument.
		/// </summary>
		/// 
		/// <param name="assembly">The assembly to set as information source.</param>
		/// 
		/// <exception cref="ArgumentNullException"><paramref name="assembly"/> is <c>null</c>.</exception>
		/// 
		public AssemblyInfo( Assembly assembly )
		{
			Debug.Assert(assembly != null);

			if( assembly == null )
				throw new ArgumentNullException("assembly");

			__assembly = assembly;
		}

		#endregion

		#region methods

		/// <summary>
		/// Returns the version number of the assembly as string.
		/// </summary>
		/// 
		/// <param name="numFields">The number of fields (dot separated numbers) to
		///   return. If this is less than 1 or greater than the available number of
		///   fields, all fields will be returned.</param>
		/// 
		/// <returns>A string containing the version number.</returns>
		/// 
		/// <remarks>
		/// Other than the method <see cref="System.Version.ToString(Int32)"/> does
		/// this method not throw an exception if <paramref name="numFields"/> is
		/// greater than the available fields of the <see cref="System.Version"/>
		/// object that is returned by the <see cref="AssemblyVersion"/> property.
		/// </remarks>
		/// 
		public string GetAssemblyVersionString( int numFields )
		{
			return GerVersionString(numFields, __assembly.GetName().Version.ToString());
		}

		/// <summary>
		/// Returns the file version number of the assembly as string.
		/// </summary>
		/// 
		/// <param name="numFields">The number of fields (dot separated numbers) to
		///   return. If this is less than 1 or greater than the available number of
		///   fields, all fields will be returned.</param>
		/// 
		/// <returns>A string containing the version number. This is empty if the file
		///   does not conatain a version number.</returns>
		/// 
		public string GetFileVersionString( int numFields )
		{
			return GerVersionString(numFields, FileVersionInfo.GetVersionInfo(__assembly.Location).FileVersion);
		}

		/// <summary>
		/// Splits the specified version number string into the number of specified
		/// fields.
		/// </summary>
		/// 
		/// <param name="numFields">The number of fields (dot separated numbers) to
		///   return. If this is less than 1 or greater than the available number of
		///   fields, all fields will be returned.</param>
		/// <param name="version">The version string to split.</param>
		/// 
		/// <returns>A string containing the version number. This is empty if
		///   <paramref name="version"/> is <c>null</c> or empty.</returns>
		/// 
		private static string GerVersionString( int numFields, string version )
		{
			if( string.IsNullOrEmpty(version) )
				return string.Empty;

			if( numFields > 0 )
			{
				string[] numbers = version.Split('.');

				for( int i = 0; i < numFields && i < numbers.Length; ++i )
				{
					if( i == 0 )
						version = numbers[0];
					else
						version += '.' + numbers[i];
				}
			}

			return version;
		}

		#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
Germany Germany
I am a software developer since many years and have worked on several large projects especially in financial sectors and the logistics industry.

My favorite programming languages are C, C++ und newly C#.

I am the architect and chief developer of Tricentis TDM Studio (former Q-up) - a generator that primarily creates template based synthetic data for software testing.

Comments and Discussions