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 ...
PackDB的创建是为了提供数据访问层的.Net实现,该访问层使用MessagePack作为后端的数据序列化程序。 PackDB.Core提供了允许PackDB用户与基础PackDB扩展项目进行交互的基础。 PackDB的一个关键概念是易于扩展以以...
可扩展基础:用于设置 Javascript 类的简单库 利用Backbone.extend语义以通用方式定义子类,使设置 javascript 类层次结构变得简单。 为方便起见,添加逻辑以便Base类的默认构造函数调用继承链中所有类的...
"Extendable SSI (Server Side Include) - 开源"是一个基于CGI的程序,它旨在增强和扩展Apache Web服务器的XSSI(Extended Server-Side Includes)功能。XSSI是一种服务器端脚本技术,允许Web开发者在HTML页面中嵌入...
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
而"extendable-media-recorder-wav-encoder-broker"是针对JavaScript环境中的一个特定工具,它与媒体处理和录制有关。从标题和描述我们可以推测,这是一个用于扩展媒体记录器功能的程序包,特别强调了对WAV音频格式...
"Programmers Extendable Text Editor"(程序员可扩展文本编辑器)是一个专为开发者设计的项目,其目标是构建一个基于WTL库和其他免费资源的高效、可定制的文本编辑器。WTL(Windows Template Library)是Microsoft...
XB,全称eXtendable Binary,是一个开源的通用文件格式及库,设计目标是实现二进制XML和高效XML数据交换。该格式旨在在保持数据序列化速度和紧凑性的同时,提供足够的健壮性,以适应各种类型的数据结构。这种技术...
- 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. - ...
XIE,全称为"Extendable Internet Engine",是一款开源的Web开发工具,旨在提供一个集成了强大脚本语言和HTML-like模板的平台。这个引擎的独特之处在于它的可扩展性,允许开发者根据项目需求定制和扩展其功能,从而...
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 ...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装