Click here to Skip to main content
15,894,630 members
Articles / Desktop Programming / Windows Forms

CX Part II

Rate me:
Please Sign up or sign in to vote.
4.97/5 (33 votes)
5 Aug 2009CPOL47 min read 54.1K   354   48  
Build a Metadata Designer for the CX Dynamic Composition Framework.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

using Clifton.Tools.Strings;
							  
using Cx.Attributes;
using Cx.Exceptions;

namespace Cx.Common
{
	public static class CxCommon
	{
		/// <summary>
		/// Returns the type that implements the specified interface.  Only public class types are inspected.
		/// </summary>
		public static Type FindImplementor(Assembly assy, string componentName, Type targetInterface)
		{
			componentName = StringHelpers.LeftOf(componentName, '`');
			Type compType = null;

			IEnumerable<Type> cxComponents = from classType in assy.GetTypes()
											 where classType.IsClass && (classType.IsPublic || classType.IsNotPublic)
											 from classInterface in classType.GetInterfaces()
											 where classInterface == targetInterface
											 select classType;

			// Find the class of the specified component name.
			foreach (Type t in cxComponents)
			{
				object[] attributes = t.GetCustomAttributes(typeof(CxComponentNameAttribute), false);

				if (attributes.Length == 1)
				{
					if (((CxComponentNameAttribute)attributes[0]).ComponentName == componentName)
					{
						compType = t;
						break;
					}
				}
			}

			// If not found, let's try finding a class with the specified name.  This is a fallback for when we don't actually
			// need to name the component class using an attribute, which helps decouple our application code from the framework.
			foreach (Type t in cxComponents)
			{
				if (t.Name == componentName)
				{
					compType = t;
					break;
				}
			}

			Verify.IsNotNull(compType, "Could not find component in " + assy.FullName + " with the component name " + componentName);

			return compType;
		}
	}
}

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
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions