[IronPython] CustomSymbolDictionary and conversions

Christian Schmidt christian2.schmidt at gmx.de
Mon Aug 25 14:55:28 PDT 2008

Hi sesh,

thanks for your answer.

>> What would be the approach to extend a ScriptScope by the properties of
>> an object? Or in other words: How can I evaluate an expression in an
>> object's scope?
> Any valid IronPython script snippet can be executed in the context of a scope. Since an expression is just another code snippet,you can the appropriate hosting API methods to achieve it. There are more than one ways to do it, the most typical being the one where you'd create a 'ScriptSource' object using the ScriptEngine.CreateScriptSource* methods and invoke the 'Execute' method on it and passing the relevant scope to it. (More information about various ways to execute a code snippet can be found at http://blogs.msdn.com/seshadripv/archive/2008/07/28/various-ways-to-execute-script-using-the-dlr-hosting-api.aspx)
> Here's a typical execute call that executes a expression in a given scope.
>                 ScriptScope scope = pythonEngine.CreateScope();
>             var src = eng.CreateScriptSourceFromString( "someVar1+someVar2",SourceCodeKind.Expression);
>             src.Execute(scope);

I meant something different. I wanted to access the properties of an 
object in a scope. Meanwhile I found the solution:

class ObjectSymbolDictionary : CustomSymbolDictionary {
   ObjectOperations ops;
   object obj;

   public ObjectSymbolDictionary(ScriptEngine engine, object obj)
     : base() {
     this.ops = engine.Operations;
     this.obj = obj;

   override bool TryGetExtraValue(SymbolId key, out object value)
     string name = SymbolTable.IdToString(key);
     if (ops.TryGetMember(obj, name, out value))
       return true;
     value = null;
     return false;


More information about the Users mailing list