C# 为什么CosmosDB只保存公共成员?
我有一个测试类,它包含一些受保护和私有的成员:C# 为什么CosmosDB只保存公共成员?,c#,azure-cosmosdb,C#,Azure Cosmosdb,我有一个测试类,它包含一些受保护和私有的成员: public class Doc_PrivateValues : Document { public int PublicIntProperty { get; set; } public int PublicIntField; protected int ProtectedIntProperty { get; set; } protected int Protecte
public class Doc_PrivateValues : Document
{
public int PublicIntProperty { get; set; }
public int PublicIntField;
protected int ProtectedIntProperty { get; set; }
protected int ProtectedIntField;
private int PrivateIntProperty { get; set; }
private int PrivateIntField;
public SimpleDocument PublicDocument;
protected SimpleDocument ProtectedDocument;
private SimpleDocument PrivateDocument;
public SimpleStruct PublicStruct;
protected SimpleStruct ProtectedStruct;
private SimpleStruct PrivateStruct;
}
我以非常简单的方式将此文档保存到CosmosDB中:
Microsoft.Azure.Documents.Document result = CosmosClient.CreateDocumentAsync( CosmosCollection.SelfLink, document ).Result.Resource;
document.id = result.Id;
以及数据库中的结果:
{
"PublicIntField": 2,
"PublicDocument": {
"StrVal": "seven",
"IntVal": 7,
"id": null
},
"PublicStruct": {
"StrVal": "ten",
"IntVal": 10
},
"PublicIntProperty": 1,
"id": "58f18ccf-9e0c-41a6-85cd-a601f12a120a",
"_rid": "mPlfANiOud4BAAAAAAAAAA==",
"_self": "dbs/mPlfAA==/colls/mPlfANiOud4=/docs/mPlfANiOud4BAAAAAAAAAA==/",
"_etag": "\"00000000-0000-0000-8b2a-d853688c01d4\"",
"_attachments": "attachments/",
"_ts": 1543856923
}
该文档仅包含公共成员。
我怎样才能拯救非公众成员呢
谢谢 JSON.NET没有访问非公共属性的权限,因此它无法处理这些属性。它根本看不到它们 您可以编写自己的
ContractResolver
,它使用反射来获取非公共属性
然后,您只需在DocumentClient
或操作级别上提供JsonSerializerSettings
这里描述了这样做的方法:@GyörgyGulyás您也可以接受它作为答案:)