Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23085
  • 博文数量: 10
  • 博客积分: 490
  • 博客等级: 下士
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-05 11:06
文章分类
文章存档

2008年(10)

我的朋友
最近访客

分类: 系统运维

2008-09-03 00:45:52

在Blogengine里,每一个扩展的Class都必须有Extension标记
ExtensionAttribute是一个密封类,继承自System.Attribute,System.Attribute没有任何实现,只是一个标记而已。
ExtensionAttribute类的代码:(完整代码可以从下载)
 
  1. [AttributeUsage(AttributeTargets.Class)]  //指明该Attrubite只能应用于Class上   
  2.   public sealed class ExtensionAttribute : System.Attribute   
  3.   {   
  4.     ///    
  5.     /// Creates an instance of the attribute and assigns a description.   
  6.     /// 
  7.   
  8.     public ExtensionAttribute(string description, string version, string author)   
  9.     {   
  10.       _Description = description;   
  11.       _Version = version;   
  12.       _Author = author;   
  13.     }   
  14.   
  15.     private string _Description;   
  16.     ///    
  17.     /// Gets the description of the extension.   
  18.     /// 
  19.   
  20.     public string Description   
  21.     {   
  22.       get { return _Description; }   
  23.     }   
  24.   
  25.     private string _Version;   
  26.   
  27.     ///    
  28.     /// Gets the version number of the extension   
  29.     /// 
  30.   
  31.     public string Version   
  32.     {   
  33.       get { return _Version; }   
  34.     }   
  35.   
  36.     private string _Author;   
  37.   
  38.     ///    
  39.     /// Gets the author of the extension   
  40.     /// 
  41.   
  42.     public string Author   
  43.     {   
  44.       get { return _Author; }   
  45.     }   
  46.   
  47.   }  

这个类非常之简单,只是一些描述性的信息,如作者、版本、描述等。
到这里,ExtensionAttribute类就可以应用在任何一个Class上了
看看mp3player.cs
 
  1. ///    
  2. /// 增加flashMp3播放器   
  3. /// 
  4.   
  5. [Extension("mp3 player""1.0.0.0""lemongtree.com")]  //此处给mp3player类打上Extension标记   
  6. public class mp3player   
  7. {   
  8.     public mp3player()   
  9.     {   
  10.         Post.Serving += new EventHandler(Post_Serving);   
  11.     }   
  12.     private void Post_Serving(object sender, ServingEventArgs e)   
  13.     {   
  14.         //此处略过   
  15.     }   
  16. }  

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有很多地方是值得我们学习的.
阅读(382) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~