扩展 IronPython for ASP.NET:编写自定义属性注入器
点击次数:37 次 发布日期:2008-11-26 11:28:18 作者:源代码网
|
源代码网推荐 我反编译了 Microsoft.Web.IronPython.dll,在其中增加了对 RepeaterItem 和 Session (HttpSessionState) 的属性注入支持。 源代码网推荐 源代码网推荐 对 RepeaterItem 的支持很简单,因为本身已经有了 ControlAttributesInjector. 所以只要在 DynamicLanguageHttpModule.cs 的静态构造器中加一行代码即可: 源代码网推荐 源代码网推荐 // repeater item 源代码网推荐 Ops.RegisterAttributesInjectorForType(typeof(RepeaterItem), new ControlAttributesInjector(), false); 源代码网推荐 而 Session 则不那么幸运,这个类实现了 ICollection,但是确没有实现 IDictionary 接口。所以就无法利用 DictionaryAttributesInjector. 没办法,我自己给加了个 SessionAttributesInjector 类。代码如下: 源代码网推荐 源代码网推荐 using IronPython.Runtime; 源代码网推荐 using System; 源代码网推荐 using System.Collections; 源代码网推荐 using System.Runtime.InteropServices; 源代码网推荐 using System.Web.SessionState; 源代码网推荐 using System.Diagnostics; 源代码网推荐 源代码网推荐 namespace Microsoft.Web.IronPython.AttributesInjectors { 源代码网推荐 // added by Neil Chen. 源代码网推荐 internal class SessionAttributesInjector: IAttributesInjector { 源代码网推荐 List IAttributesInjector.GetAttrNames(object obj) { 源代码网推荐 HttpSessionState session = obj as HttpSessionState; 源代码网推荐 List list = new List(); 源代码网推荐 foreach (string key in session.Keys) { 源代码网推荐 list.Add(key); 源代码网推荐 } 源代码网推荐 return list; 源代码网推荐 } 源代码网推荐 源代码网推荐 bool IAttributesInjector.TryGetAttr(object obj, SymbolId nameSymbol, out object value) { 源代码网推荐 HttpSessionState session = obj as HttpSessionState; 源代码网推荐 value = session[nameSymbol.GetString()]; 源代码网推荐 return true; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 附上我修改过的 Microsoft.Web.IronPython.dll 源代码网推荐 源代码网推荐 需要说明的是,属性注入器只对 get 操作有用,比如 源代码网推荐 name = Session.Name 是可以的, 源代码网推荐 源代码网推荐 但是设置则不行: 源代码网推荐 Session.Name = "some name" 会报错。 源代码网推荐 源代码网推荐 还是需要用这个语法: 源代码网推荐 Session["Name"] = "some name" 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
