Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cant Decode DateTime #1496

Closed
Closed
@innominate227

Description

@innominate227

Environment

  • Pythonnet version: 2.5.2
  • Python version: 3.8.10
  • Operating System: Windows 10
  • .NET Runtime: Framework 4.7

Details

  • Trying to pass a python datetime.datetime to a c# method accepting a System.DateTime.

C# code

using System;using Python.Runtime;namespace MyNamespace{    public static class MyClass    {        public static void TestInt(int i)        {            Console.WriteLine(i);        }        public static void TestDateTime(DateTime dateTime)        {            Console.WriteLine(dateTime);        }    }    public class MyCodecs : IPyObjectDecoder    {        public static void Setup()        {            Console.WriteLine("Setup Called");            PyObjectConversions.RegisterDecoder(new MyCodecs());        }        public bool CanDecode(PyObject objectType, Type targetType)        {            Console.WriteLine("CanDecode Called");            return targetType == typeof(DateTime);        }        public bool TryDecode<T>(PyObject pyObj, out T value)        {            Console.WriteLine("TryDecode Called");            var dt = new DateTime(                pyObj.GetAttr("year").As<int>(),                pyObj.GetAttr("month").As<int>(),                pyObj.GetAttr("day").As<int>(),                pyObj.GetAttr("hour").As<int>(),                pyObj.GetAttr("minute").As<int>(),                pyObj.GetAttr("second").As<int>());            value = (T)(object)dt;            return true;        }    }}

Python code

import clrfrom datetime import datetimeclr.AddReference('MyLibrary')from MyNamespace import MyClass, MyCodecs#these two workMyCodecs.Setup()MyClass.TestInt(22)#error hereMyClass.TestDateTime(datetime(2021, 1, 22))

The exception is:

TypeError: No method matches given arguments for TestDateTime: (<class 'datetime.datetime'>)

Its doent appear that any attempt is made to call MyCodecs.CanDecode or MyCodecs.TryDecode. The WriteLines in those methods never get hit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp