[IronPython] unsupported operand type(s) for & operator overloading.

李兵 dreamwinterlb at gmail.com
Wed May 13 08:29:26 CEST 2009


Thank Dino Viehland, Seo Sanghyeon.  I have resolved.
Code in the following:

namespace TestLibrary {
    public class Foo {
        public string Value = "abc";

        public static FooGroup operator &(Foo left, Foo right) {
            return new FooGroup(left, right);
        }
    }

    public class Factory {
        public static Foo Create() { return new Foo(); }
    }
}


    public class FooGroup {
        public FooGroup(Foo left,Foo right) {
            this.Left = left;
            this.Right = right;
        }
        public Foo Left { get; private set; }
        public Foo Right { get; private set; }
        public override string ToString() {
            return Left.Value + " AND " + Right.Value; //output abcabc
        }
    }
}

  class Program {
        static void Main(string[] args) {
            ScriptRuntime runtime =
IronPython.Hosting.Python.CreateRuntime();
            ScriptEngine engine = runtime.GetEngine("py");
            //import
            runtime.LoadAssembly(typeof(Foo).Assembly);

            string importExpression = @"from TestLibrary import *";

            ScriptSource source
=engine.CreateScriptSourceFromString(importExpression,
Microsoft.Scripting.SourceCodeKind.Statements);
            CompiledCode tempCode = source.Compile();
            tempCode.Execute(runtime.GetBuiltinModule());


            string script = "Factory.Create() & Factory.Create()"; //Using
brackets to group mulit-conditions
            ScriptSource exeSource =
engine.CreateScriptSourceFromString(script,
Microsoft.Scripting.SourceCodeKind.Expression);
            CompiledCode target = exeSource.Compile();
            var group = target.Execute<FooGroup>(engine.CreateScope());
            Console.WriteLine( group.ToString());  //out "abc AND abc"
            Console.Read();
        }

2009/5/13 Dino Viehland <dinov at microsoft.com>

>  Can you include the hosting code which is evaluating the expression?  If
> I do:
>
>
>
> Fooa.cs:
>
> namespace Baz {
>
>     public class foo {
>
>         public static void Main(string[] args) { }
>
>               public static  object operator &(foo left, foo right) {
>
>                 return 42;
>
>             }
>
>     }
>
> }
>
>
>
> import clr
>
> clr.AddReference('fooa')
>
> import Baz
>
> x = Baz.foo()
>
> x & x
>
>
>
> this prints 42 (on 2.0 RTM and some random 2.6 build).    So either
> A.Create(“Test1”) or A.CreateProperty(“Sum”) is not returning a Condition or
> there’s something w/ the hosting APIs that’s throwing this off.
>
>
>
> I also can’t track down the specific error message – specifically “operator
> overloading” doesn’t seem to show up anywhere so I’m a little bit lost what
> could be causing it.  I would expect something which includes the type names
> such as “unsupported operand type(s) for &: 'str' and 'str'”.
>
>
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *??
> *Sent:* Tuesday, May 12, 2009 8:05 PM
> *To:* users at lists.ironpython.com
> *Subject:* [IronPython] unsupported operand type(s) for & operator
> overloading.
>
>
>
> Hi,
>    I implemented '&' operator overloading in C# class.  But When I used
> ScriptRuntime compile the expression that use '&' operator, I got a
> exception 'unsupported operand type(s) for & operator overloading'.
> The following is C# code.
>
>       public static  Group operator &(Condition left, Condition right) {
>             if (left == null) {
>                 throw new ArgumentNullException("left");
>             }
>             if (right == null) {
>                 throw new ArgumentNullException("right");
>             }
>              Group re = new  Group(left, right, LogicalOperator.And);
>             return re;
>         }
>
> IronPython expression like this ' A.Create("Audit") ==  A.Create("Test1") &
> A.CreateProperty("Sum") == A.Create("Test2") '
>
> I tried to convert '&' to 'and'. Although no exception, I lost the left
> expression.
>
> Anyone can help me? Thanks.
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090513/92614d15/attachment.html>


More information about the Ironpython-users mailing list