在Blogengine里,每一个扩展的Class都必须有
Extension标记
ExtensionAttribute是一个密封类,继承自System.Attribute,System.Attribute没有任何实现,只是一个标记而已。
ExtensionAttribute类的代码:(完整代码可以从
下载)
- [AttributeUsage(AttributeTargets.Class)]
- public sealed class ExtensionAttribute : System.Attribute
- {
-
-
-
- public ExtensionAttribute(string description, string version, string author)
- {
- _Description = description;
- _Version = version;
- _Author = author;
- }
-
- private string _Description;
-
-
-
- public string Description
- {
- get { return _Description; }
- }
-
- private string _Version;
-
-
-
-
- public string Version
- {
- get { return _Version; }
- }
-
- private string _Author;
-
-
-
-
- public string Author
- {
- get { return _Author; }
- }
-
- }
[AttributeUsage(AttributeTargets.Class)] //指明该Attrubite只能应用于Class上
public sealed class ExtensionAttribute : System.Attribute
{
///
/// Creates an instance of the attribute and assigns a description.
///
public ExtensionAttribute(string description, string version, string author)
{
_Description = description;
_Version = version;
_Author = author;
}
private string _Description;
///
/// Gets the description of the extension.
///
public string Description
{
get { return _Description; }
}
private string _Version;
///
/// Gets the version number of the extension
///
public string Version
{
get { return _Version; }
}
private string _Author;
///
/// Gets the author of the extension
///
public string Author
{
get { return _Author; }
}
}
这个类非常之简单,只是一些描述性的信息,如作者、版本、描述等。
到这里,ExtensionAttribute类就可以应用在任何一个Class上了
看看mp3player.cs
-
-
-
- [Extension("mp3 player", "1.0.0.0", "lemongtree.com")]
- public class mp3player
- {
- public mp3player()
- {
- Post.Serving += new EventHandler(Post_Serving);
- }
- private void Post_Serving(object sender, ServingEventArgs e)
- {
-
- }
- }
///
/// 增加flashMp3播放器
///
[Extension("mp3 player", "1.0.0.0", "lemongtree.com")] //此处给mp3player类打上Extension标记
public class mp3player
{
public mp3player()
{
Post.Serving += new EventHandler(Post_Serving);
}
private void Post_Serving(object sender, ServingEventArgs e)
{
//此处略过
}
}
mp3player就是一个针对于Post的扩展了。
有朋友可能会问了:mp3player这个在什么时候实例化的呢?或是我如何得知这个扩展是不是Enable的呢?
整个实例化的过程在global.asax的Application_Start事件中
- Assembly a = Assembly.Load(assemblyName);
- Type[] types = a.GetTypes();
-
- foreach (Type type in types)
- {
- object[] attributes = type.GetCustomAttributes(typeof(ExtensionAttribute), false);
- foreach (object attribute in attributes)
- {
- if (ExtensionManager.ExtensionEnabled(type.Name))
- {
- a.CreateInstance(type.FullName);
- }
- }
- }
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有很多地方是值得我们学习的.
阅读(382) | 评论(0) | 转发(0) |