0

In C# I am passing an object to a function. I need to add a property in object that contains dash "-" in it. How can I pass this dashed property in object?

SendPushNotificationByAPNS(
device.RegisterId,
new
{
    aps = new
    {
        badge = 0
        alert = new
        {
            title = notificationTitle,
            body = notificationBody,
            sound = "default"
        },
        mutable-content = 1 <-- I need to add this but C# doesn't allow
    },
    entityId = entityId,
    entityType = entityType
});
Ali Shahzad
  • 5,163
  • 7
  • 36
  • 64
  • 2
    This feels like a XY Problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem Why do you need a `-` in a property name? – mjwills Jul 19 '18 at 12:27
  • If you are using JSON.Net you can define a custom formatter. – Greg Jul 19 '18 at 12:29
  • A custom formatter is probably overkill. It's easy enough to just [add an attribute](https://stackoverflow.com/a/5771591/542251). – Liam Jul 19 '18 at 12:30
  • @KevinAnderson That makes no sense for an identifier such as a variable's name. You don't pass the variable's name but its value. – Camilo Terevinto Jul 19 '18 at 12:30

1 Answers1

1

You don't have to name properties in C# in the same way as in JSON format, you have to create a class with all properties that you need with regard to C# naming conventions.

Then you can add [JsonProperty("mutable-content")] attribute with name of the field that you expected to see in a JSON file.

After your applicaiton will make request to another source it will be automatically serialized.

Aliaksei Futryn
  • 461
  • 5
  • 8