The property injector mechanism of IronPython for ASP.NET can make the syntax of some codes simple (for details, please refer to my article), but the default support seems to be incomplete yet.
I decompiled Microsoft.Web.IronPython.dll and added property injection support for RepeaterItem and Session (HttpSessionState).
Support for RepeaterItem is very simple, because it already has ControlAttributesInjector. So just add a line of code to the static constructor of DynamicLanguageHttpModule.cs:
// repeater item
Ops.RegisterAttributesInjectorForType(typeof(RepeaterItem), new ControlAttributesInjector(), false);
Session is not so lucky. This class implements ICollection, but does not implement the IDictionary interface. So I can't use DictionaryAttributesInjector. No way, I added a SessionAttributesInjector class myself. The code is as follows:
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;
}
}
}
Attached is my modified Microsoft.Web.IronPython.dll.
It should be noted that the attribute injector is only useful for get operations, such as
name = Session.Name is OK,
but setting is not:
Session.Name = 'some name' will report an error.
You still need to use this syntax:
Session["Name"] = 'some name'