People have asked me how we build the extension model into BlogEngine.NET. There’s nothing to it - really, there isn’t.
You need one small class and 14 lines of code in the global.asax. That is all you need to make your ASP.NET application extendable. An extension is just a normal class that is somehow marked as being an extension. No magic is needed.
The scenario I liked best was one where you could just drop your custom extension into the App_Code folder and then it would just work. Reflection would take care of creating an instance of the extension, but we need a way to separate the extensions from all other classes in the App_Code folder. That’s where we need the small class.
The class
I decided it would make most sense to have all custom extensions be decorated with a custom attribute called Extension and then let global.asax activate them when the application starts. That would make the class definition of a custom extension look like this:
[Extension("description")]
public class MyCustomExtension
To do that, we need to write a very small class that inherits from System.Attribute and it looks like this:
public class ExtensionAttribute : System.Attribute
{
/// <summary>
/// Creates an instance of the attribute and assigns a description.
/// </summary>
public ExtensionAttribute(string description)
{
_Description = description;
}
private string _Description;
/// <summary>
/// Gets the description of the extension.
/// </summary>
public string Description
{
get { return _Description; }
}
}
It’s a very simple class that just has a description property and that’s it. The description property is not really needed to implement extensions, so you can leave it out if you’d like.
Global.asax
Now we have our custom attribute, so it’s time to let global.asax use reflection to activate the extensions. This method iterates through all the types in the App_Code folder and when it finds a class that is decorated with the Extension attribute then it creates an instance of it.
void Application_Start(object sender, EventArgs e)
{
Assembly a = Assembly.Load("__code");
Type[] types = a.GetTypes();
foreach (Type type in types)
{
object[] attributes = type.GetCustomAttributes(typeof(ExtensionAttribute), false);
foreach (object attribute in attributes)
{
a.CreateInstance(type.FullName);
}
}
}
That’s it. Now your ASP.NET application handles custom extensions made by who ever wants to write them. There are many ways to customize this implementation. For instance, you can put the reflection code in the Begin_Request method instead of the Application_Start to let your extensions act as an HttpModule.
相关推荐
* Extendable FastReport.Net architecture allows you to create your own objects, export filters, wizards, DB engines. If existing abilities are not enough for you - extend it! * Very compact and ...
- Extendable FastReport.Net architecture allows you to create your own objects, export filters, wizards, DB engines. If existing abilities are not enough for you - extend it! - Very compact and really...
UPX is a portable, extendable, high-performance executable packer for several different executable formats. It achieves an excellent compression ratio and offers **very** fast decompression. Your ...
OpenBlocks开发用资料
$ npm install --save is-extendable 用法 var isExtendable = require ( 'is-extendable' ) ; 如果值为以下任意值,则返回true: 大批 普通物体 功能 笔记 JavaScript中的所有对象都可以具有键,但是对此进行检查...
extendable-media-recorder在上可用,可以照常安装。 npm install extendable-media-recorder 它导出MediaRecorder构造函数。可以像本机实现一样使用它。以下示例将使用浏览器定义的默认编码器。 import { ...
Develop extendable plugins with the use of WordPress features in core modules Develop pluggable modules to extend the core features of WordPress as independent modules Follow WordPress coding ...
from the framework are standardized,easy operated and extendable.Based on the framework,this paper well researches an implementation technique,and develops an application of function test automation ...
PackDB的创建是为了提供数据访问层的.Net实现,该访问层使用MessagePack作为后端的数据序列化程序。 PackDB.Core提供了允许PackDB用户与基础PackDB扩展项目进行交互的基础。 PackDB的一个关键概念是易于扩展以以...
可扩展基础:用于设置 Javascript 类的简单库 利用Backbone.extend语义以通用方式定义子类,使设置 javascript 类层次结构变得简单。 为方便起见,添加逻辑以便Base类的默认构造函数调用继承链中所有类的...
dependencies and make our application loosely coupled, extendable, and maintainable. We can implement dependency injection to move the dependency resolution from compile-time to runtime. This book ...
DokiLog Pleasant Android Kotlin logging by DokiDevs. Inspired by Timber AnkoLogger. Features Use different LogProfile for different ...In your Application class, create a LogProfile with a unique name a
- Exchange your complete UI without touching any logic. You want to try the new Unity UI instead of your current NGUI-based UI? No problem. - No additional UI code necessary to visualize your data. - ...
Programmers Extendable Text Editor 项目致力于使用 WTL 和其他免费提供的库来创建文本编辑器。 将来,该项目将包括可配置的语法突出显示以及其他以程序员为中心的功能。
Odin puts your Unity workflow on steroids, making it easy to build powerful and advanced user-friendly editors for you and your entire team. The Asset Store version is for entities or companies with ...
一个CGI,它复制并扩展了Apache XSSI的功能。 命令是模块化的,因此可以随意添加新命令。 用GNU Awk写。
XB是一种通用文件格式(和一种库),它试图满足所谓的二进制XML和高效XML交换的要求。 XB格式允许对任何任意数据的树进行快速,紧凑但仍然足够健壮的序列化。
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装