首先,对于大量代码,描述可能有点长,但我不能提供所有可用的信息。 我正在为我的应用程序进行逻辑删除工作,我想保存一个序列化的类:
[DataContract]
private class Tombstone
{
[DataMember]
public UserDetails UserProfile { get; set; }
}
// Create instance of the tombstone class
private Tombstone _tombstone;
public ProfileSetup()
{
_tombstone = new Tombstone();
}
//Add data to userProfile
void UserInformationAccess_OnGetUserDetailsComplete(GetUserDetailsResponse response)
{
_tombstone.UserProfile = response.userDetails;
}
我在导航中有例外......完全在这里:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (_tombstone != null)
this.SaveState("tombstone", _tombstone);
}
//The state manager class
public static class StateManager
{
/// <summary>
/// Saves a key-value pair into the state object
/// </summary>
/// <param name="phoneApplicationPage">The phone application page.</param>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public static void SaveState(this PhoneApplicationPage phoneApplicationPage, string key, object value)
{
if (phoneApplicationPage.State.ContainsKey(key))
{
phoneApplicationPage.State.Remove(key);
}
phoneApplicationPage.State.Add(key, value);
}
}
现在,你们自己也会遇到异常:
"SecurityException" at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type, SerializationMode mode) at System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Runtime.Serialization.XmlFormatWriter.InternalSerialize(MethodInfo methodInfo, Object memberValue, Type memberType, Boolean writeXsiType, XmlObjectSerializerWriteContext context, XmlWriterDelegator xmlWriter) at System.Runtime.Serialization.XmlFormatWriter.WriteValue(Type memberType, Object memberValue, Boolean writeXsiType, XmlObjectSerializerWriteContext context, XmlWriterDelegator xmlWriter) at System.Runtime.Serialization.XmlFormatWriter.WriteMember(SerializingObject serObj, Int32 memberIndex, ClassDataContract derivedMostClassContract) at System.Runtime.Serialization.XmlFormatWriter.WriteClass(CallStackElement`1 callStackElement) at System.Runtime.Serialization.XmlFormatWriter.Serialize(XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlFormatWriter.InitializeCallStack(XmlWriterDelegator xmlWriterDel, Object obj, XmlObjectSerializerWriteContext writeContext, DataContract contract) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph) at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph) at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(Stream stream, Object graph) at Microsoft.Phone.Shell.StreamPersister.Serialize(IDictionary`2 dictionary, IEnumerable`1 knownTypes) at Microsoft.Phone.Shell.StreamPersister.Save(ShellPage shellPage, String key, IDictionary`2 dictionary, IEnumerable`1 knownTypes) at Microsoft.Phone.Controls.PhoneApplicationPage.InternalOnNavigatedFrom(NavigationEventArgs e) at System.Windows.Navigation.NavigationService.RaiseNavigated(Object content, Uri uri, NavigationMode mode, Boolean isNavigationInitiator, PhoneApplicationPage existingContentPage, PhoneApplicationPage newContentPage) at System.Windows.Navigation.NavigationService.Journal_NavigatedExternally(Object sender, JournalEventArgs args) at System.Windows.Navigation.Journal.OnNavigatedExternally(String name, Uri uri, NavigationMode mode) at System.Windows.Navigation.Journal.ShellPage_NavigatedAway(Object sender, NavigateAwayEventArgs args) at Microsoft.Phone.Shell.Interop.ShellPageCallback.FireOnNavigateAway(IntPtr thisPage, Direction direction, IntPtr pageNext)
我已经研究了很多与此相关的内容,我提出空洞:(感谢任何帮助。
[编辑]
像问题一样的问题是TombStone类具有私有可访问性,我改变了......但是我得到了以下异常:
键入&#39; Newtonsoft.Json.Linq.JObject&#39; with data contract name&#39; ArrayOfKeyValueOfstringJTokeneJCYCtcq:http://schemas.microsoft.com/2003/10/Serialization/Arrays' 不是预期的。将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或添加它们 到传递给DataContractSerializer的已知类型列表。
这里也是UserDetails类
[KnownType(typeof(Phone[]))]
[KnownType(typeof(UserInterest<InterestCategory?, object>))]
[KnownType(typeof(UserInterest<InterestCategory?, object>[]))]
[DataContract]
public class UserDetails
{
/// <summary>
/// Specifies an identifier of user
/// </summary>
///
[DataMember]
public long userId { get; set; }
/// <summary>
/// Specifies a nick of user
/// </summary>
///
[DataMember]
public string nick { get; set; }
/// <summary>
/// Specifies a full name of user. The field is absent if it has null value
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public string fullName { get; set; }
/// <summary>
/// Specifies a gender of user. The field is absent if it has null value
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(EnumAttributeConverter<Gender>))]
[DataMember]
public Gender? gender { get; set; }
/// <summary>
/// Specifies a birthday of user as string in dd.MM.yyyy format.
/// The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public string birthday { get; set; }
/// <summary>
/// Specifies an e-mail of user. The field is absent if it has null value
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public string email { get; set; }
/// <summary>
/// Specifies a website of user. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public string website { get; set; }
/// <summary>
/// Specifies a general information about user. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public string about { get; set; }
/// <summary>
/// Specifies a place of birth for user. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public Address<Country?>? homeAddress { get; set; }
/// <summary>
/// Specifies a place of residence for user. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public Address<Country?>? currentAddress { get; set; }
/// <summary>
/// Specifies a list of user's phones. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public Phone[] phones { get; set; }
/// <summary>
/// Specifies an URI of avatar of profile. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public string avatar { get; set; }
/// <summary>
/// Specifies a job. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public string job { get; set; }
/// <summary>
/// Specifies a mood status of user as a color.The field is absent in read commands if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public int? mood { get; set; }
/// <summary>
/// Specifies a relationship status of user.The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public RelationshipStatus? relationshipStatus { get; set; }
/// <summary>
/// Defines a discovery purpose specified by user.The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public DiscoveryPurpose? discoveryPurpose { get; set; }
/// <summary>
/// Specifies a list of user interests. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public UserInterest<InterestCategory?, object>[] interests { get; set; }
/// <summary>
/// Specifies a status of user. The field is absent if it has null value.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public string status { get; set; }
/// <summary>
/// Specifies an availability status of user.
/// </summary>
[JsonConverter(typeof(EnumAttributeConverter<AvailabilityStatus>))]
[DataMember]
public AvailabilityStatus availabilityStatus { get; set; }
/// <summary>
/// Specifies a location of user. The field is absent if location is undefined.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public Location location { get; set; }
/// <summary>
/// Defines if the given user and the requestor interacted previously.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[DataMember]
public bool interacted { get; set; }
/// <summary>
/// Defines the status of pending friendship request if it exists.
/// Equals NONE if there is no pending friendship request.
/// Equals PENDING_IN if the given user sent friendship
/// request to the user (requestor) who requests extended
/// information.
/// Equals PENDING_OUT if the requestor sent friendship
/// request to the given request.
/// </summary>
[JsonConverter(typeof(EnumAttributeConverter<FriendshipRequestStatus>))]
[DataMember]
public FriendshipRequestStatus friendshipRequestStatus { get; set; }
/// <summary>
/// Defines if the given user was ignored by the requestor.
/// </summary>
///
[DataMember]
public bool ignored { get; set; }
}
答案 0 :(得分:2)
异常表示引用的类型之一在序列化程序的上下文中不可访问/可见... 问题是您将类定义为私有:
“私人阶级墓碑”
这意味着它仅对定义它的类可见 你需要公开这个课。
答案 1 :(得分:1)
问题在于DataContractSerializer
能够序列化UserDetails
个对象。
页面的Add
字典的State
方法将使用`DataContractSerializer序列化对象。
您已经证明您已使用DataContract序列化属性标记了Tombstone
类但是UserDetails
呢?
幸运的是,您可以通过尝试自行序列化和反序列化对象来调试此问题
您可能还会发现使用另一个序列化程序可以获得更好的性能(json.net很好且易于使用),因为DataContractSerializer
非常慢。