简体   繁体   English

C#在类中使用参数变量名

[英]c# Using params variable name in Class

I have the following Class definition: 我有以下类定义:

class PostObject
{
    public string jsonrpc { get; set; }
    public string method { get; set; }
    public MyObject params { get; set; }
    public string id { get; set; }
}

I use this class for a post call (serialized in json) and the server has ' params ' as an input post var and there's no way to change it. 我将此类用于发布调用(在json中序列化),并且服务器将“ params ”作为输入发布var,并且无法更改它。

The question is that as params is a reserved keyword in c#, what should I do? 问题是,由于params是c#中的保留关键字,该怎么办?

You can use serialization attributes to set it's name. 您可以使用序列化属性来设置其名称。 Like this (NewtonSoft.Json): 像这样(NewtonSoft.Json):

[JsonProperty(PropertyName = "params")]
public MyObject parameters { get; set; }

Serialization Attributes in NewtonSoft.Json NewtonSoft.Json中的序列化属性

Please see MSDN for the same. 同样,请参见MSDN。 You can still use keywords as variable name but by appending '@'. 您仍然可以使用关键字作为变量名,但可以附加“ @”。 Example :- int @int= 100; 示例:-int @ int = 100;

Source, 资源,

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/

除了@转义和JsonPropertyAttribute ,还值得一提的是,您不需要使用NewtonSoft.Json这样的库来进行精确的大写:

public string Params { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM