weixin_39647787 2020-11-22 03:18
浏览 0

Misleading ArgumentTypeException message when calling C# method

From on May 26, 2017 13:2

Hello, this is not a bug per se, but rather something I just noticed. I'm including a minimal example console app that will throw with the unexpected message Expected Char, got str, further discussion below the code.

By the way, this is IronPython v2.7.7 on .NET 4.

csharp
    class Program
    {
        static void Main(string[] args)
        {
            var engine = IronPython.Hosting.Python.CreateEngine();

            var foo = new Foo();
            var locals = new Dictionary<string object> {{"foo", foo}};

            var scope = engine.CreateScope(locals);

            var result = engine.Execute("foo.Bar('code', 'a', 7.2)",scope);

            Console.ReadLine();
        }
    }

    public class Foo
    {
        public string Bar(string x, char y, float z = 0)
        {
            return string.Format("{0}, {1}, {2}", x, y, z);
        }
    }
</string>

Now, the signature of method Bar of class Foo is of course wrong, in that the last parameter should be double rather than float (or we would need to pass in a System.Single to begin with).

The exception message is a bit misleading, though: Expected Char, got str is not really the hint I would expect. I scratched my head for a minute before realizing I had made a dumb mistake. It seems like it should say something along the lines of Expected Single, got Double.

Just thought I would let you guys know.

Copied from original issue: IronLanguages/main#1660

该提问来源于开源项目:IronLanguages/ironpython2

  • 写回答

5条回答 默认 最新

  • weixin_39647787 2020-11-22 03:18
    关注

    From on May 26, 2017 14:30

    Are you sure your minimal example is correct? Seems to me like Expected Char, got str is the correct message in that Bar expects a char but you're passing it the string a.

    评论

报告相同问题?