Click here to Skip to main content
15,885,915 members
Articles / Programming Languages / C#

Macro Management Framework

Rate me:
Please Sign up or sign in to vote.
4.92/5 (23 votes)
28 Feb 200510 min read 42.2K   494   35  
Pluggable text macros.
using System;
using System.Collections;
using System.Reflection;
using System.Diagnostics;

namespace MacMan
{
   /// <summary>
   /// Creates Macros from a class attributed with macro attributes.
   /// </summary>
   sealed class MacroSuiteBuilder
   {
      MacroSuiteBuilder() {}

      internal static int Build(object obj, Hashtable macromap)
      {
         Type T; CheckArgs(obj, macromap, out T);

         MacroSuiteAttribute msa = MacroSuiteAttribute.OnType(T);

         MethodInfo[] methods = GetMethodsOf(T);
         int n = 0;
         foreach (MethodInfo method in methods)
         {
            MacroAttribute ma = MacroAttribute.OnMethod(method);
            if (ma == null) continue;
            if (macromap.ContainsKey(method.Name))
            {
               Trace.WriteLine(string.Format("Macro allready defined: {0}, skipping", method.Name), "Macro Suite Builder");
               continue;
            }
            if (!IsMethodSignatureOK(method))
            {
               Trace.WriteLine(string.Format("Method signature mismatch: {0}, skipping", method.Name), "Macro Suite Builder");
               continue;
            }

            macromap.Add( method.Name, new Macro(obj, method, ma, msa) );
            ++n;
         }

         return n;
      }

      internal static int Strip(object obj, Hashtable macromap)
      {
         Type T; CheckArgs(obj, macromap, out T);

         MethodInfo[] methods = GetMethodsOf(T);
         int n = 0;
         foreach (MethodInfo method in methods)
         {
            if (!macromap.ContainsKey(method.Name)) continue;
            macromap.Remove( method.Name );
            ++n;
         }

         return n;
      }

      static bool IsMethodSignatureOK(MethodInfo method)
      {
         if (method.ReturnType != typeof(string)) return false;
         ParameterInfo[] pars = method.GetParameters();
         if (pars.Length != 1 || pars[0].ParameterType != typeof(string)) return false;

         return true;
      }

      [System.Diagnostics.DebuggerHidden]
      static void CheckArgs(object obj, Hashtable macromap, out Type T)
      {
         if (obj == null)
            throw new ArgumentNullException("obj");
         if (macromap == null)
            throw new ArgumentNullException("macromap");
         T = obj.GetType();
         if ( !MacroSuiteAttribute.IsDefined(T) )
            throw new ArgumentException("Not a macro resolution suite", "obj");
      }

      static MethodInfo[] GetMethodsOf(Type T)
      {
         return T.GetMethods(BindingFlags.Instance|BindingFlags.Public);
      }
   }
}

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

Comments and Discussions