首页 > 解决方案 > 如何在 .NET Core 中使用 DateTime.ToString() 修复“缺少委托编组数据”错误?

问题描述

我编写了这个程序以最小化错误重现:

using System;

namespace HelloDates
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var today = DateTime.Today;
            Console.WriteLine("I know what day it is!");
            string today_fmt = today.ToString("MM/dd/yyyy");
            Console.WriteLine($"Today is {today_fmt}.");
        }
    }
}

添加了Microsoft.DotNet.ILCompiler,用CoreRT发布,运行,得到了这个结果:

Hello World!
I know what day it is!
Unhandled Exception: EETypeRva:0x00283B48: EETypeRva:0x00295C98 is missing delegate marshalling data. To enable delegate marshalling data, add a MarshalDelegate directive to the application rd.xml file. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=393965
   at HelloDates!<BaseAddress>+0x5fed
   at HelloDates!<BaseAddress>+0xfddcf
   at HelloDates!<BaseAddress>+0x1458fb
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey, ConditionalWeakTable`2.CreateValueCallback) + 0x3d
   at System.Runtime.InteropServices.PInvokeMarshal.GetFunctionPointerForDelegate(Delegate) + 0x83
   at HelloDates!<BaseAddress>+0xb6c38
   at HelloDates!<BaseAddress>+0xb9ab8
   at System.Globalization.DateTimeFormatInfo.set_Calendar(Calendar) + 0x94
   at System.Globalization.CultureInfo.get_DateTimeFormat() + 0x57
   at System.Globalization.CultureInfo.GetFormat(Type) + 0x88
   at System.Globalization.DateTimeFormatInfo.get_CurrentInfo() + 0x50
   at System.Globalization.DateTimeFormatInfo.GetInstance(IFormatProvider) + 0x8b
   at HelloDates!<BaseAddress>+0x9b630
   at HelloDates!<BaseAddress>+0x9b4bd
   at HelloDates.Program.Main(String[]) + 0x62
   at HelloDates!<BaseAddress>+0x155416

显然,当程序尝试格式化日期时会出现错误。但是我不知道委托编组数据或 EETypeRva 是什么,我在任何可以看到的地方都没有 rd.xml 文件,也无法理解链接的文档。关于反射的东西?

尤其令人沮丧的是,在我最初遇到错误的程序的早期版本中,ToString() 工作得很好,甚至可以编译为本机代码,我不知道可能发生了什么变化。

标签: c#datetime.net-coredatetime-format

解决方案


推荐阅读