zoukankan      html  css  js  c++  java
  • 扩展 IronPython for ASP.NET:编写自定义属性注入器

    IronPython for ASP.NET 的属性注入器机制可以使得一些代码的语法变得简单(详细了解参考我的这一篇),但是默认的支持似乎现在还很不完备。
    我反编译了 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'

    注:这个做法因为是通过反编译实现的,仅供个人研究参考。
  • 相关阅读:
    codeforces 349B Color the Fence 贪心,思维
    luogu_2022 有趣的数
    luogu_2320 [HNOI2006]鬼谷子的钱袋
    luogu_1879 [USACO06NOV]玉米田Corn Fields
    SAC E#1
    luogu_1984 [SDOI2008]烧水问题
    luogu_2085 最小函数值
    luogu_1631 序列合并
    luogu_1196 银河英雄传说
    luogu_1037 产生数
  • 原文地址:https://www.cnblogs.com/RChen/p/ipy_attributes_injector_customization.html
Copyright © 2011-2022 走看看