Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Dynamic Binding Using the Factory Pattern

, 1 Nov 2009
Using the Factory design pattern to hide dynamic binding and use configuration strings to determine which classes should be instantiated.
/*************************************************************************
 *
 * Copyright 2009 Richard (Rick) Marvin Wycoff, All Rights Reserved
 * DO NOT ALTER OR REMOVE THIS COPYRIGHT NOTICE.
 *
 * NOTICE: You are permitted to use, modify, and distribute this file in 
 * accordance with the terms of the license agreement accompanying it.  
 * Any existing copyright or authorship information in any given source
 * file must remain intact.
 *
 **************************************************************************/

using System;
using System.Reflection;

namespace Wycoff.Activation
{
    /// <summary>
    /// Loads an assembly/class directly into the current process
    /// </summary>
    public class InProcessActivator : Locator.ILocatorActivator
    {
        #region ILocatorActivator Members

        /// <summary>
        /// Implementation of the Locator.ILocatorActivator interface.  This
        /// implementation is responsible for creating an instance of
        /// a class in the current process.
        /// </summary>
        /// <param name="type">Not Used</param>
        /// <param name="uri">
        /// Uri string in the format:
        /// <list type="bullet">
        /// <item>local://host/AssemblyName/ClassName</item>
        /// <item>
        /// local://host/AssemblyName/ClassName?ConstructorString
        /// </item></list>
        /// </param>
        /// <exception cref="ArgumentException">
        /// Thrown went there is a problem with the Uri string.  Check the
        /// format, the assembly name and the class name.
        /// </exception>
        /// <returns>
        /// A new instance of the specified class.
        /// </returns>
        public object Activate(Type type, Uri uri)
        {
            // Uri must be in the format
            // local://host/AssemblyName/ClassName or
            // local://host/AssemblyName/ClassName?ConstructorString
            string[] pathSegments = uri.LocalPath.Split('/');
            if (pathSegments.Length != 3)
            {
                throw new ArgumentException(uri.ToString());
            }

            // path starts with slash, so element zero is empty
            string assemblyName = pathSegments[1];
            Assembly assembly = Assembly.Load(assemblyName);
            if (null == assembly)
            {
                throw new ArgumentException(uri.ToString());
            }

            string className = pathSegments[2];

            object response = null;

            //Do we have a constructor string?
            if (uri.Query.Length > 0)
            {
                string initString = Uri.UnescapeDataString(uri.Query);
                if ('?' == initString[0])
                {
                    initString = initString.Remove(0, 1);
                }
                object[] initParam = { initString };

                response = assembly.CreateInstance(className, 
                    false,
                    BindingFlags.CreateInstance,
                    null,
                    initParam,
                    null,
                    null);
            }
            else
            {
                response = assembly.CreateInstance(className);
            }

            return response;
        }

        #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

Richard (Rick) Wycoff
Software Developer (Senior)
United States United States
Rick's software passion is developing "invisible” software components, the kind that no one ever thinks about because they just do what they are supposed to do. He believes in standards, design patterns, and designing components for extensibility and reuse.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 1 Nov 2009
Article Copyright 2009 by Richard (Rick) Wycoff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid