object to deserialize has a C# keyword

≯℡__Kan透↙ 提交于 2019-11-28 01:41:08

Try using the [DataContract(Name = "@event")] attribute on the relevant property. Then it will (de)serialize correctly, and you can rename the property so that it compiles.

Change

public class Event
{
    public int event { get; set; }
    public EventDetail data { get; set; }
}

to this

public class Event
{
    public int @event { get; set; }
    public EventDetail data { get; set; }
}

This tip shows the quirks involved with escaping in C#:

  • character literal escaping:

e.g. '\'', '\n', '\u20AC' (the Euro € currency sign), '\x9'

(equivalent to \t)) - literal string escaping:

e.g. "...\t...\u0040...\U000000041...\x9..."

  • verbatim string escaping:

e.g. @"...""..."

  • string.Format escaping:

e.g. "...{{...}}..."

  • keyword escaping:

e.g. @if (for if as identifier)

  • identifier escaping:

e.g. i\u0064 (for id)

I was able to just capitalize the "e", and it still works. Looks like the parsing mechanism is case-insensitive.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!