我正在使用JsonConvert序列化对象,如下所示:
JsonConvert.SerializeObject(item)
Item是QuestionExtendedView的一个实例,如下所示:
public class QuestionExtendedView : AuditFullView
{
public short QuestionNo { get; set; }
public short TotalQuestions { get; set; }
public short UnansQuestions { get; set; }
}
AuditFullView如下所示:
public partial class AuditFullView : EntityObject
{
public static AuditFullView CreateAuditFullView(global::System.Int32 hAA_ID, global::System.Int16 hAA_Branch, global::System.Int32 hAA_AuditorID, global::System.DateTime hAA_ScheduledDate, global::System.Int32 hAA_TemplateVersionID, global::System.String hAA_Status, global::System.Int32 hAS_ID, global::System.Int32 hAS_AuditID, global::System.Int32 hAS_TemplateSectionID, global::System.Int32 hAE_ID, global::System.Int32 hAE_AuditID, global::System.Int32 hAE_HAS_ID, global::System.Int32 hAE_TemplateElementID, global::System.Int16 hAE_ScriptSequence, global::System.Int32 hAQ_ID, global::System.Int32 hAQ_AuditID, global::System.Int32 hAQ_HAE_ID, global::System.Int32 hAQ_TemplateQuestionID, global::System.Int16 hAQ_ScriptSequence, global::System.Int32 hTS_ID, global::System.Int32 hTS_VersionID, global::System.Int32 hTS_Sequence, global::System.String hTS_SectionName, global::System.Int32 hTE_ID, global::System.Int32 hTE_SectionID, global::System.Int32 hTE_Sequence, global::System.String hTE_Element, global::System.String hTE_Objective, global::System.String hTE_Guidance, global::System.Int32 hTQ_ID, global::System.Int32 hTQ_ElementID, global::System.Int32 hTQ_Sequence, global::System.String hTQ_Question, global::System.Boolean hTQ_WeightedQuestion, global::System.String hSU_Name, global::System.Boolean hAQ_PreviouslyAnsweredQuestion)
{
AuditFullView auditFullView = new AuditFullView();
auditFullView.HAA_ID = hAA_ID;
auditFullView.HAA_Branch = hAA_Branch;
// Loads of properties excluded for clarity
return auditFullView;
}
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 HAA_ID
{
get
{
return _HAA_ID;
}
set
{
if (_HAA_ID != value)
{
OnHAA_IDChanging(value);
ReportPropertyChanging("HAA_ID");
_HAA_ID = StructuralObject.SetValidValue(value);
ReportPropertyChanged("HAA_ID");
OnHAA_IDChanged();
}
}
}
private global::System.Int32 _HAA_ID;
partial void OnHAA_IDChanging(global::System.Int32 value);
partial void OnHAA_IDChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int16 HAA_Branch
{
get
{
return _HAA_Branch;
}
set
{
if (_HAA_Branch != value)
{
OnHAA_BranchChanging(value);
ReportPropertyChanging("HAA_Branch");
_HAA_Branch = StructuralObject.SetValidValue(value);
ReportPropertyChanged("HAA_Branch");
OnHAA_BranchChanged();
}
}
}
private global::System.Int16 _HAA_Branch;
partial void OnHAA_BranchChanging(global::System.Int16 value);
partial void OnHAA_BranchChanged();
// Loads of properties excluded for clarity
}
现在,当我运行此序列化时,我从AuditFullView获取所有属性,但不是来自QuestionExtendedView的属性,如此测试所示:
[TestMethod]
public void CanSerialize()
{
QuestionExtendedView myView = new QuestionExtendedView
{
TotalQuestions = 15,
QuestionNo = 10,
UnansQuestions = 5,
HAA_ID = 100,
HAA_Branch = 213
};
string json = JsonConvert.SerializeObject(myView);
json.Should().Contain("TotalQuestions");
}
结果失败了:
{"$id":"1","HAA_ID":100,"HAA_Branch":213,"EntityKey":null}
(同样,为清晰起见,我已从AuditFullView中排除了许多属性)
我甚至试过这个:
JsonConvert.SerializeObject(Convert.ChangeType(myView, typeof(QuestionExtendedView))
虽然没有区别。我似乎无法找到其他人遇到这个问题。我错过了什么吗?
答案 0 :(得分:1)
您的AuditFullView
课程继承自EntityObject
,其中标有DataContract
属性。因此,JSON.NET要求所有属性必须使用DataMember
属性选择加入序列化。序列化程序将忽略任何没有该属性的属性。
AuditFullView
上的属性标有DataMember
,因此序列化时会包含这些属性; <{1}}上的属性未标有QuestionExtendedView
,因此会被忽略。
因此,直截了当的解决方案是使用DataMember
属性标记QuestionExtendedView
类的属性。