浅谈BlogEngine扩展机制的实现

在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有很多地方是值得我们学习的.

转载于:https://www.cnblogs.com/olimpca2008/archive/2008/09/02/1282418.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值