Swapper.NET Logo

Source Code Interfaces ~ IAbout.cs


using System;
using System.Diagnostics;
using System.Reflection;
using System.Xml.Serialization;
using RevolutionaryStuff.JBT;

namespace RevolutionaryStuff.P2P.Basics
{
	public interface IAbout
	{
		Version Version {get;}
		string Title {get;}
		string Description {get;}
		string Company {get;}
		Uri More {get;}
	}

	public class About : IAbout
	{
		public static string ToString(IAbout a)
		{
			if (null==a) throw new ArgumentNullException("a");
			About aa = a is About ? (About) a : new About(a);
			return SimplePersist.ToString(a);
		}

		#region Constructors
		public About(){}

		public About(IAbout a)
		{
			if (null==a) throw new ArgumentNullException("a");
			this.Title = a.Title;
			this.Company = a.Company;
			this.Description = a.Description;
			this.Version = a.Version;
			this.More = a.More;
		}

		public About(Type t) : this(t, true)
		{}

		public About(Type t, bool shared)
		{
			if (null==t) throw new ArgumentNullException("t");
			Assembly a = t.Assembly;
			this.Title = t.Name;
			object[] attrs = a.GetCustomAttributes(true);
			foreach (Attribute attr in attrs)
			{
				if (attr is AssemblyTitleAttribute && !shared)
				{
					this.Title = ((AssemblyTitleAttribute)attr).Title;
				}
				else if (attr is AssemblyDescriptionAttribute && !shared)
				{
					this.Description = ((AssemblyDescriptionAttribute)attr).Description;
				}
				else if (attr is AssemblyCompanyAttribute)
				{
					this.Company = ((AssemblyCompanyAttribute)attr).Company;
				}
			}
			attrs = t.GetCustomAttributes(typeof(AboutXAttribute), true);
			foreach (AboutXAttribute attr in attrs)
			{
				if (attr is AboutTitleAttribute)
				{
					this.Title = (string) attr.Val;
				}
				else if (attr is AboutDescriptionAttribute)
				{
					this.Description = (string) attr.Val;
				}
				else if (attr is AboutMoreAttribute)
				{
					this.More = (Uri) attr.Val;
				}
			}
			this.Version = a.GetName().Version;
		}
		#endregion

		#region IAbout Members

		[XmlElement]
		public virtual string Title
		{
			[DebuggerStepThrough]
			get { return this.Title_p; }
			[DebuggerStepThrough]
			set { this.Title_p = value; }
		}
		private string Title_p;

		[XmlElement]
		public virtual string Description
		{
			[DebuggerStepThrough]
			get { return this.Description_p; }
			[DebuggerStepThrough]
			set { this.Description_p = value; }
		}
		private string Description_p;

		[XmlElement]
		public virtual string Company
		{
			[DebuggerStepThrough]
			get { return this.Company_p; }
			[DebuggerStepThrough]
			set { this.Company_p = value; }
		}
		private string Company_p;
		
		[XmlElement]
		public virtual Version Version
		{
			[DebuggerStepThrough]
			get { return this.Version_p; }
			[DebuggerStepThrough]
			set { this.Version_p = value; }
		}
		private Version Version_p;

		[XmlElement]
		public virtual Uri More
		{
			[DebuggerStepThrough]
			get { return this.More_p; }
			[DebuggerStepThrough]
			set { this.More_p = value; }
		}
		private Uri More_p;
		#endregion
	}

	public abstract class AboutXAttribute : Attribute
	{
		public readonly string Name;
		public readonly object Val;

		protected AboutXAttribute(string name, object val)
		{
			this.Name = name;
			this.Val = val;
		}
	}
	[AttributeUsage(AttributeTargets.Class)]
	public class AboutTitleAttribute : AboutXAttribute
	{
		public AboutTitleAttribute(string title) : base("Title", title)
		{}
	}
	[AttributeUsage(AttributeTargets.Class)]
	public class AboutDescriptionAttribute : AboutXAttribute
	{
		public AboutDescriptionAttribute(string desc) : base("Description", desc)
		{}
	}
	[AttributeUsage(AttributeTargets.Class)]
	public class AboutMoreAttribute : AboutXAttribute
	{
		public AboutMoreAttribute(Uri more) : base("More", more)
		{}
	}
}