RuntimeBinderException occurred with CSharpScript and DynamicObject. #2968

Closed
kekyo opened this Issue May 21, 2015 · 1 comment

Projects

None yet

4 participants

@kekyo
kekyo commented May 21, 2015

Sorry poor english...

  • I tested CSharpScript and DynamicObject. RuntimeBinderException occurred, but I don't know the cause.
  • Used:
    • "Microsoft.CodeAnalysis.CSharp 1.0.0-rc2"
    • "Microsoft.CodeAnalysis.Scripting.CSharp 1.0.0-rc2"
using System;
using System.Dynamic;
using System.Linq;

using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Scripting.CSharp;

namespace Test
{
    public sealed class TargetObject : DynamicObject
    {
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            if (binder.Name == "CreateValues")
            {
                var count = (int)args[0];
                var r = new Random();
                result = string.Join(",", Enumerable.Range(0, count).Select(index => r.Next()));
                return true;
            }

            return base.TryInvokeMember(binder, args, out result);
        }
    }

    public sealed class HostObject
    {
        public HostObject()
        {
            this.Target = new TargetObject();
        }

        public dynamic Target
        {
            get;
            private set;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var scriptOptions = ScriptOptions.Default.WithReferences(
                typeof(object).Assembly,
                typeof(Uri).Assembly,
                typeof(Enumerable).Assembly,
                typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly);

            var host = new HostObject();

            var script = CSharpScript.Create(
                "Console.WriteLine(Target.CreateValues(10));",
                scriptOptions);

            // Raise RuntimeBinderException : Cannot implicitly convert type 'void'  to 'object'
            script.Run(host);
        }
    }
}

Any idea?
Thanks.

@ManishJayaswal ManishJayaswal added the Bug label May 21, 2015
@ManishJayaswal ManishJayaswal added this to the 1.1 milestone May 21, 2015
@ManishJayaswal ManishJayaswal self-assigned this May 21, 2015
@tannergooding
Member

@ManishJayaswal, This is the same as #170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment