Click here to Skip to main content
15,895,799 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.Reflection;

namespace MacMan
{
   /// <summary>
   /// Indicates a class that provides macro resolution methods.
   /// </summary>
   [AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
   public class MacroSuiteAttribute : Attribute
   {
      /// <summary>
      /// Checks if this attribute is attached to a given type.
      /// </summary>
      internal static bool IsDefined(Type t)
      {
         return t.IsDefined(typeof(MacroSuiteAttribute), false);
      }

      /// <summary>
      /// Retrieves this attribute on a given type. Returns null if none defined.
      /// </summary>
      internal static MacroSuiteAttribute OnType(Type T)
      {
         foreach ( MacroSuiteAttribute a in T.GetCustomAttributes(typeof(MacroSuiteAttribute), false) )
            return a;
         return null;
      }

      private string _category = "Misc";
      /// <summary>
      /// Default macro category for a macro suite.
      /// </summary>
      public string Category
      {
         get { return this._category; }
         set { this._category = (value != null && value != "") ? value : "Misc"; }
      }

   }


   /// <summary>
   /// Indicates a method designed for macro resolution.
   /// The method must have the signature 'string methodname(string)'.
   /// </summary>
   [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
   public class MacroAttribute : Attribute
   {
      private string _caption = "";
      /// <summary>
      /// A human-readable name of the macro; ex.: "Application Directory".
      /// </summary>
      public string Caption
      {
         get { return this._caption; }
         set { this._caption = (value != null) ? value : ""; }
      }

      private string _category = "";
      /// <summary>
      /// Macro category, used for grouping macros.
      /// </summary>
      public string Category
      {
         get { return this._category; }
         set { this._category = (value != null) ? value : ""; }
      }

      private string _argument = "";
      /// <summary>
      /// Name of macro argument, or empty/null if the macro requires no arguments.
      /// Reserved for future use (currently not evaluated).
      /// </summary>
      public string Argument
      {
         get { return this._argument; }
         set { this._argument = (value != null) ? value : ""; }
      }

      /// <summary>
      /// Retrieves this attribute on a given method. Returns null if none defined.
      /// </summary>
      internal static MacroAttribute OnMethod(MethodInfo method)
      {
         foreach ( MacroAttribute a in method.GetCustomAttributes(typeof(MacroAttribute), false) )
            return a;
         return null;
      }
   }
}

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