Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C#

ProjectMIDI: an extensible set of small MIDI .NET programs

Rate me:
Please Sign up or sign in to vote.
4.70/5 (14 votes)
24 Jan 200626 min read 85.4K   3.4K   57  
This article describes how multiple .NET assemblies work together to control MIDI devices in a live performance environment.
using System;
using System.Windows;
using System.Collections;
using System.Reflection;

namespace ProjectMidiNS
{
	/// <summary>
	/// Collection of PMAssembly objects
	/// </summary>
	public class PMAssembly : IPMAssembly
	{
		private IProjectMidi	iprojectmidi;
		private Assembly		assembly;
		private string			name;				// "Friendly" name from attribute

		private PMPropertyInfoCollection properties = new PMPropertyInfoCollection();
		private PMMethodInfoCollection methods = new PMMethodInfoCollection();
		private PMEventInfoCollection events = new PMEventInfoCollection();
		private PMActionInfoCollection actions = new PMActionInfoCollection();

		public PMPropertyInfoCollection Properties { get { return properties; } }
		public PMMethodInfoCollection Methods { get { return methods; } }
		public PMEventInfoCollection Events { get { return events; } }
		public PMActionInfoCollection Actions { get { return actions; } }

		public Assembly Asm { get { return assembly; } }
		public string Name { get { return name; } }
		public string FilePath { get { return assembly.Location; } }

		public IProjectMidi IProjMidi { get { return iprojectmidi; } }

		public PMAssembly( string assemName, IProjectMidi iproj, Assembly a, Type t )
		{
			iprojectmidi = iproj;
			this.name = assemName;
			assembly = a;

			try
			{
				// Determine all of the properties, methods and events
				MemberInfo[] memberInfos = t.GetMembers(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static);
				foreach(MemberInfo member in memberInfos)
				{
					object[] customAttrs = member.GetCustomAttributes( false );
					foreach(object attr in customAttrs)
					{
						if(attr.GetType() == typeof(ProjectMidiPropertyAttribute))
						{
							Console.WriteLine("{0}: property.",assemName);
							properties.Add( new PMPropertyInfo(member,(ProjectMidiPropertyAttribute)attr, iproj) );
						}
						else if(attr.GetType() == typeof(ProjectMidiEventAttribute))
						{
							ProjectMidiEventAttribute eventAttr = attr as ProjectMidiEventAttribute;
							Console.WriteLine("{0}: Event {1}",assemName,eventAttr.Name);
							events.Add( new PMEventInfo(member, assemName, eventAttr, iproj) );
						}
						else if(attr.GetType() == typeof(ProjectMidiMenuEventAttribute))
						{
							ProjectMidiMenuEventAttribute menuEventAttr = attr as ProjectMidiMenuEventAttribute;
							Console.WriteLine("{0}: MenuEvent {1}",assemName,menuEventAttr.Name);
							events.Add( new PMEventInfo(member, assemName, menuEventAttr, iproj) );
						}
						else if(attr.GetType() == typeof(ProjectMidiActionAttribute))
						{
							ProjectMidiActionAttribute actionAttr = attr as ProjectMidiActionAttribute;
							Console.WriteLine("{0}: Action {1}",assemName,actionAttr.Name);
							actions.Add( new PMActionInfo(member, assemName, actionAttr, iproj) );
						}
						else if(attr.GetType() == typeof(ProjectMidiMethodAttribute))
						{
							Console.WriteLine("{0}: Method.",assemName);
							methods.Add( new PMMethodInfo(member,(ProjectMidiMethodAttribute)attr, iproj) );
						}
					}
				}
			}
			catch(Exception err)
			{
				Console.WriteLine("PMAssembly() error: {0}",err.Message);
			}
		}
	}

	#region PMAssemblyCollection
	/// <summary>
	/// Collection of PMAssembly objects
	/// </summary>
	public class PMAssemblyCollection : CollectionBase, IPMAssemblyCollection
	{
		public PMAssemblyCollection()
		{
		}
		//----------------------------------
		public void Add(PMAssembly pmassembly)
		{
			List.Add(pmassembly);
		}

		//----------------------------------
		public void Remove()
		{
			// Check to see if there is a widget at the supplied index.
			if ( Count < 1 )
				// If no widget exists, a messagebox is shown and the operation 
				// is cancelled.
			{
				System.Windows.Forms.MessageBox.Show("PMAssemblyCollection: Attempt to remove from empty collection!");
			}
			else
			{
				List.RemoveAt( Count-1 ); 
			}
		}
		public void Remove(int index)
		{
			// Check to see if there is a widget at the supplied index.
			if (index > Count - 1 || index < 0)
				// If no widget exists, a messagebox is shown and the operation 
				// is cancelled.
			{
				System.Windows.Forms.MessageBox.Show("PMAssemblyCollection: Index not valid!");
			}
			else
			{
				List.RemoveAt(index); 
			}
		}

		// ITEM
		public PMAssembly Item(int Index)
		{
			return (PMAssembly) List[Index];
		}
	}
	#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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Ron is a senior software engineer.
His hobbies include riding motorcycles, travel, and scuba diving.

He enjoys learning about science, particularly quantum physics and cosmology.

He is active with his church where he plays drums and keyboards with the contemporary church band each week.
He also designed and maintains his church and band websites (http://TheRockUMC.org and http://TheRockBand.org).

Comments and Discussions