C# - 如何在变量名中使用无效字符 [英] C# - how to use invalid characters in a variable name
本文介绍了C# - 如何在变量名中使用无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中,我建设,最终将被序列化到一些外部定义JSON类(这里简化了讨论目的):
In C#, I'm building a class (simplified here for discussion purposes) that eventually will be serialized into some externally defined JSON:
{
"$schema": "http://example.com/person.json",
"name": "John",
"age": 86
}
在我的代码,我会是这样的:
In my code I would have something like:
public class Person
{
public const string $schema= @"http://example.com/person.json";
public string name {get;set; }
public int age {get; set;}
}
...
...
Person person = new Person();
person.name = "John";
person.age = 88;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(person);
在我的代码$架构上面是造成意外字符$的错误。有没有解决方法吗?
In my code above the $schema is causing an "Unexpected character '$' error. Is there a workaround?
推荐答案
提供属性 [DataContract]
你的人
类。
另外,你的意思是让模式
常量
?
Also, did you mean to make schema
const
?
[DataContract]
public class Person
{
[DataMember(Name = "$schema")]
public string schema { get; set; }
public string name { get; set; }
public int age {get; set;}
}
这篇关于C# - 如何在变量名中使用无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文