[IronPython] TryGetMember is called for methods instead of TryInvokeMember
Marcus Carvalho
marcusvfcarvalho at gmail.com
Tue Jun 22 19:54:44 CEST 2010
I have an application that passes a DynamicObject to an IronPython script.
The class works perfectly in C# but when a method is invoked inside a
IronPython script the TryGetMember is called instead of TryInvokeMember and
if I try to return a value through TryGetMember I get an exception saying
that the member is not callable.
I found a user with similar problem (
http://social.msdn.microsoft.com/Forums/en/dlr/thread/0556e593-2360-43b8-97e5-201c3c3ce1c5).
He said that dowloading the latest release solved the problem. I have
downloaded the latest source code release (70538), compiled it to .NET 4.0,
but I still having the issue.
Here is a slightly modified code I get from the post, that does not work for
me:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using System.Dynamic;
namespace TestDynamicInvokeFromPython
{
class Program
{
static void Main(string[] args)
{
dynamic myDynamicObject = new MyDynamicObject();
//first tey calling this object from C#, it should call Foo() and they try
to call MissingMethod();
Console.WriteLine("C# Test...");
try
{
myDynamicObject.Foo();
System.Console.WriteLine("Method Result: " +
myDynamicObject.MissingMethod());
}
catch (Exception ex)
{
Console.WriteLine("Got C# exception: " + ex.Message);
}
ScriptEngine pythonEngine = Python.CreateEngine();
ScriptScope scriptScope = pythonEngine.CreateScope();
string pythonScript = SetPythonScript();
ScriptSource script =
pythonEngine.CreateScriptSourceFromString(pythonScript, SourceCodeKind
.Statements);
scriptScope.SetVariable("myDynamicObject", myDynamicObject);
//Now do the same thing from python, I expect to get the same behaviour as
from C# (but I don't)
Console.WriteLine("\r\nScript Test...");
try
{
script.Execute(scriptScope);
}
catch (Exception ex)
{
Console.WriteLine("Got script exception: " + ex.Message);
}
Console.ReadKey();
}
static string SetPythonScript()
{
string s = "";
s += "import clr" + "\r\n";
s += "clr.AddReference('mscorlib')" + "\r\n";
s += "myDynamicObject.Foo();" + "\r\n";
s += "myDynamicObject.MissingMethod();" + "\r\n";
return s;
}
}
public class MyDynamicObject : DynamicObject
{
public void Foo()
{
Console.WriteLine("Foo() Called");
}
public override DynamicMetaObject GetMetaObject(System.Linq.Expressions.
Expression parameter)
{
Console.WriteLine("GetMetaObject() Called");
return base.GetMetaObject(parameter);
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[]
args, out object result)
{
if (binder.Name == "MissingMethod")
{
result = "Method Found";
return true;
}
else
{
result = null;
return false;
}
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
Console.WriteLine("TryGetMember() Called");
return base.TryGetMember(binder, out result);
}
//#region AllOtherOverridees
// ...
//#endregion //AllOtherOverridees
}
}
Marcus Carvalho
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100622/4c3a6968/attachment.html>
More information about the Ironpython-users
mailing list