浅谈BlogEngine扩展机制的实现

      我的日志 2008-9-3 1:1

在Blogengine里,每一个扩展的Class都必须有Extension标记
ExtensionAttribute是一个密封类,继承自System.Attribute,System.Attribute没有任何实现,只是一个标记而已。
ExtensionAttribute类的代码:(完整代码可以从BlogEngine.NET.zip下载)

 
  1. [AttributeUsage(AttributeTargets.Class)]  //指明该Attrubite只能应用于Class上   
  2.   public sealed class ExtensionAttribute : System.Attribute   
  3.   {   
  4.     /// <summary>   
  5.     /// Creates an instance of the attribute and assigns a description.   
  6.     /// </summary>   
  7.     public ExtensionAttribute(string description, string version, string author)   
  8.     {   
  9.       _Description = description;   
  10.       _Version = version;   
  11.       _Author = author;   
  12.     }   
  13.   
  14.     private string _Description;   
  15.     /// <summary>   
  16.     /// Gets the description of the extension.   
  17.     /// </summary>   
  18.     public string Description   
  19.     {   
  20.       get { return _Description; }   
  21.     }   
  22.   
  23.     private string _Version;   
  24.   
  25.     /// <summary>   
  26.     /// Gets the version number of the extension   
  27.     /// </summary>   
  28.     public string Version   
  29.     {   
  30.       get { return _Version; }   
  31.     }   
  32.   
  33.     private string _Author;   
  34.   
  35.     /// <summary>   
  36.     /// Gets the author of the extension   
  37.     /// </summary>   
  38.     public string Author   
  39.     {   
  40.       get { return _Author; }   
  41.     }   
  42.   
  43.   }  


这个类非常之简单,只是一些描述性的信息,如作者、版本、描述等。
到这里,ExtensionAttribute类就可以应用在任何一个Class上了
看看mp3player.cs

 
  1. /// <summary>   
  2. /// 增加flashMp3播放器   
  3. /// </summary>   
  4. [Extension("mp3 player""1.0.0.0""lemongtree.com")]  //此处给mp3player类打上Extension标记   
  5. public class mp3player   
  6. {   
  7.     public mp3player()   
  8.     {   
  9.         Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);   
  10.     }   
  11.     private void Post_Serving(object sender, ServingEventArgs e)   
  12.     {   
  13.         //此处略过   
  14.     }   
  15. }  


mp3player就是一个针对于Post的扩展了。
有朋友可能会问了:mp3player这个在什么时候实例化的呢?或是我如何得知这个扩展是不是Enable的呢?
整个实例化的过程在global.asax的Application_Start事件中

 
  1. Assembly a = Assembly.Load(assemblyName);   
  2.       Type[] types = a.GetTypes();   //获取当前程序集中的所有类型   
  3.   
  4.       foreach (Type type in types)   
  5.       {   
  6.         object[] attributes = type.GetCustomAttributes(typeof(ExtensionAttribute), false);   //获取含有ExtensionAttribute标记的类   
  7.         foreach (object attribute in attributes)   
  8.         {   
  9.           if (ExtensionManager.ExtensionEnabled(type.Name))   
  10.           {   
  11.             a.CreateInstance(type.FullName);  //创建实例   
  12.           }   
  13.         }   
  14.       }  


到这里很明了了,扩展类决定于它有没有打Extension标记,是否创建实例取决于该扩展有没有被启用.
BlogEngine有很多地方是值得我们学习的.

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}