文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>浅谈BlogEngine扩展机制的实现

浅谈BlogEngine扩展机制的实现

时间:2008-09-03  来源:david_shar

在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.   }  
[AttributeUsage(AttributeTargets.Class)] //指明该Attrubite只能应用于Class上 public sealed class ExtensionAttribute : System.Attribute { /// <summary> /// Creates an instance of the attribute and assigns a description. /// </summary> public ExtensionAttribute(string description, string version, string author) { _Description = description; _Version = version; _Author = author; } private string _Description; /// <summary> /// Gets the description of the extension. /// </summary> public string Description { get { return _Description; } } private string _Version; /// <summary> /// Gets the version number of the extension /// </summary> public string Version { get { return _Version; } } private string _Author; /// <summary> /// Gets the author of the extension /// </summary> public string Author { get { return _Author; } } } 

这个类非常之简单,只是一些描述性的信息,如作者、版本、描述等。
到这里,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. }  
/// <summary> /// 增加flashMp3播放器 /// </summary> [Extension("mp3 player", "1.0.0.0", "lemongtree.com")] //此处给mp3player类打上Extension标记 public class mp3player { public mp3player() { Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving); } private void Post_Serving(object sender, ServingEventArgs e) { //此处略过 } } 

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.       }  
Assembly a = Assembly.Load(assemblyName); Type[] types = a.GetTypes(); //获取当前程序集中的所有类型 foreach (Type type in types) { object[] attributes = type.GetCustomAttributes(typeof(ExtensionAttribute), false); //获取含有ExtensionAttribute标记的类 foreach (object attribute in attributes) { if (ExtensionManager.ExtensionEnabled(type.Name)) { a.CreateInstance(type.FullName); //创建实例 } } } 

到这里很明了了,扩展类决定于它有没有打Extension标记,是否创建实例取决于该扩展有没有被启用.
BlogEngine有很多地方是值得我们学习的.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载