Interfacing DynamicObjects to script code

May 7, 2010 at 1:45 PM

I expose a custom object derived from DynamicObject to IronPython and IronRuby scripts.

During testing I found some curious behavior:

1) When I want to access a dynamic property of my custom object and I use IronRuby, the name of the property is always capitalized and underscores are removed. e.g.: puts myObject.foo_bar => TryGetMember("FooBar"). This only happens with IronRuby and when accessing a member, when setting a member the name is not modified.

2) TryConvert never gets called on my dynamic object. For example if I assign my dynamic object to an int property of another object, the runtime reports an error "Cannot convert xxx to yyy".  If I add an implicit cast operator to int to my dynamic object class, the runs through without errors. I've tested this with IronPython and IronRuby.

I'm using DLR V1.0 for .Net 2.0


May 7, 2010 at 4:55 PM

1) This is probably the same issue we know of. See bug:

2) Could you provide a snippet of source code that reproduces the behavior (the script that blows up as well as your implementation of DynamicObject subclass)?

May 7, 2010 at 9:58 PM

Thank you for your reply.

Below you can find the source which shows the problem. If you uncomment the implicit casting operator, the script runs through without any errors.


using System;
using System.Dynamic;
using Microsoft.Scripting.Hosting;

public class PropertyClass {
    public int IntProperty { get; set; }

public class MyDynamicObject : DynamicObject {

    /*public static implicit operator int(MyDynamicObject o) {
        return 42;

    public override bool TryConvert(ConvertBinder binder, out object result) {
        result = 0;

        return true;

public class Program {

	public static void Main(string[] args) {
		var runtime = ScriptRuntime.CreateFromConfiguration();
        var engine = runtime.GetEngine("rb");//or py
        var scope = engine.CreateScope();
		scope.SetVariable("propObj", new PropertyClass());
        scope.SetVariable("dynObj", new MyDynamicObject());
		engine.Execute("propObj.IntProperty = dynObj", scope);
		// Produces the following error:
		//(eval):0: can't convert MyDynamicObject into Fixnum (TypeError)