Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / C#

CodeDom Assistant

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
21 Sep 20074 min read 139.7K   6.6K   82  
Generating CodeDom Code By Parsing C# or VB
using System;
using System.Reflection;
using RemoteLoader;

namespace RemoteLoader
{
	/// <summary>
	/// Factory class to create objects exposing IRemoteInterface
	/// </summary>
	public class RemoteLoaderFactory : MarshalByRefObject
	{
		private const BindingFlags bfi = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;

		public RemoteLoaderFactory() {}

		/// <summary> Factory method to create an instance of the type whose name is specified,
		/// using the named assembly file and the constructor that best matches the specified parameters. </summary>
		/// <param name="assemblyFile"> The name of a file that contains an assembly where the type named typeName is sought. </param>
		/// <param name="typeName"> The name of the preferred type. </param>
		/// <param name="constructArgs"> An array of arguments that match in number, order, and type the parameters of the constructor to invoke, or null for default constructor. </param>
		/// <returns> The return value is the created object represented as ILiveInterface. </returns>
		public IRemoteInterface Create( string assemblyFile, string typeName, object[] constructArgs )
		{
			return (IRemoteInterface) Activator.CreateInstanceFrom(
				assemblyFile, typeName, false, bfi, null, constructArgs,
				null, null, null ).Unwrap();
		}

        public string GetAssemblyName(string assemblyFile)
        {
            try
            {
                Assembly ass = Assembly.LoadFile(assemblyFile);

                return ass.FullName;
            }
            catch
            {
            }

            return null;
        }

        /// <summary>
        /// Does Assembly Contain A Class That Is A SubClass of type name
        /// </summary>
        /// <param name="assemblyFile"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public bool IsSubclassOfCodeDomProvider(string assemblyFile)
        {
            try
            {
                Type basetype = typeof(System.CodeDom.Compiler.CodeDomProvider);

                Assembly ass = Assembly.LoadFile(assemblyFile);

                Type[] types = ass.GetExportedTypes();
                foreach (Type type in types)
                {
                    if (type.IsSubclassOf(basetype))
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }

            return false;
        }
	}	
}

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
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions