Hi there,
I'm new to the list, and fairly new to python integration with C#, so I was hoping someone could help me.
I am trying to bind a c# class (which is derived DynamicObject) to Pythonnet but can't get the dynamic aspect working.
In the following Code, the line : AppBind.Description("layout")
works fine and calls the C# method: public void Description(string str)
But AppBind.DynMethod(123)
throws an error stating the method doesn't exist (I was hoping this would be called: public …
[View More]override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
And AppBind.Fred = 123
Assigns 123 to a python variable Fred, when I was hoping it would call: public override bool TrySetMember(SetMemberBinder binder, object value)
Is there a way to make this work?
The closest I've found is this page, but I couldn't work it out: https://github.com/pythonnet/pythonnet/issues/72
Thank you
Guy
Here is the python snippet:
AppBind = PythonSpace.PythonCallbacks()
def DoStuff():
AppBind.Description("layout")
AppBind.DynMethod(123)
AppBind.Fred = 123
Here is the C# code: (This is derived from the the DynamicDictionary example )
public class PythonCallbacks : System.Dynamic.DynamicObject
{
// The inner dictionary.
Dictionary<string, object> dictionary = new Dictionary<string, object>();
public void Description(string str)
{
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
return base.TryInvokeMember(binder, args, out result);
}
// If you try to get a value of a property
// not defined in the class, this method is called.
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
string name = binder.Name.ToLower();
// If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
return dictionary.TryGetValue(name, out result);
}
// If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(SetMemberBinder binder, object value)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
dictionary[binder.Name.ToLower()] = value;
// You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
}
}
[View Less]
Hello, as was discussed in the bi-weekly meeting, the newest version seems
to introduce stricter type checking. Here's a snippet that exhibits two
issues.
using (Py.GIL())
{
dynamic sysmod = Py.Import("sys");
dynamic syspath = sysmod.path;
bool found = false;
foreach (var path in syspath)
{
if (path == Path.GetFullPath("some/test/path/").Replace("\\", "/"))
{
found = true;
break;
}
}
}
The first is a runtime exception …
[View More]with syspath:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : Cannot implicitly
convert type 'Python.Runtime.PyObject' to 'System.Collections.IEnumerable'.
An explicit conversion exists (are you missing a cast?)
The second is maybe a non-issue, but I think it used to work. If syspath is
made to be of type PyList there is now a compiler error: error CS0019:
Operator '==' cannot be applied to operands of type 'PyObject' and 'string'
Thanks
[View Less]