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 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;
        }

    }
}




_______________________________________________
PythonNet mailing list -- pythonnet@python.org
To unsubscribe send an email to pythonnet-le...@python.org
https://mail.python.org/mailman3/lists/pythonnet.python.org/
Member address: arch...@mail-archive.com

Reply via email to