/// <summary> /// Serializes an object into a SerializationInfo object. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <returns></returns> public SerializationInfo SerializeObject(object obj) { SerializationInfo info; if (obj == null) { NullPlaceholder nullPlaceholder = new NullPlaceholder(); info = new SerializationInfo(_serializationReferences.Count + 1); _serializationReferences.Add(nullPlaceholder, info); info.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(typeof(NullPlaceholder)); } else { var thisType = obj.GetType(); if (obj is System.Security.Claims.ClaimsPrincipal cp) { obj = new Security.CslaClaimsPrincipal(cp); thisType = obj.GetType(); } if (!thisType.IsSerializable) { throw new InvalidOperationException( string.Format(Resources.ObjectNotSerializableFormatted, thisType.FullName)); } if (!(obj is IMobileObject mobile)) { throw new InvalidOperationException( string.Format(Resources.MustImplementIMobileObject, thisType.Name)); } if (!_serializationReferences.TryGetValue(mobile, out info)) { info = new SerializationInfo(_serializationReferences.Count + 1); _serializationReferences.Add(mobile, info); info.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(thisType); if (thisType.Equals(typeof(Security.CslaClaimsPrincipal))) { var principal = (Security.CslaClaimsPrincipal)obj; using (var buffer = new System.IO.MemoryStream()) { using (var writer = new System.IO.BinaryWriter(buffer)) { principal.WriteTo(writer); info.AddValue("s", buffer.ToArray()); } } } else { mobile.GetChildren(info, this); mobile.GetState(info); } } } return(info); }
/// <summary> /// Serializes an object into a SerializationInfo object. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <returns></returns> public SerializationInfo SerializeObject(object obj) { SerializationInfo info; if (obj == null) { NullPlaceholder nullPlaceholder = new NullPlaceholder(); info = new SerializationInfo(_serializationReferences.Count + 1); _serializationReferences.Add(nullPlaceholder, info); info.TypeName = typeof(NullPlaceholder).AssemblyQualifiedName; } else { var thisType = obj.GetType(); if (!IsSerializable(thisType)) { throw new InvalidOperationException( string.Format(Resources.ObjectNotSerializableFormatted, thisType.FullName)); } var mobile = obj as IMobileObject; if (mobile == null) { throw new InvalidOperationException( string.Format(Resources.MustImplementIMobileObject, #if NETFX_CORE thisType.Name())); } #else thisType.Name)); #endif if (!_serializationReferences.TryGetValue(mobile, out info)) { info = new SerializationInfo(_serializationReferences.Count + 1); _serializationReferences.Add(mobile, info); info.TypeName = thisType.AssemblyQualifiedName; mobile.GetChildren(info, this); mobile.GetState(info); } } return(info); }
/// <summary> /// Serializes an object into a SerializationInfo object. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <returns></returns> public SerializationInfo SerializeObject(object obj) { SerializationInfo info; if (obj == null) { NullPlaceholder nullPlaceholder = new NullPlaceholder(); info = new SerializationInfo(_serializationReferences.Count + 1); _serializationReferences.Add(nullPlaceholder, info); info.TypeName = typeof(NullPlaceholder).AssemblyQualifiedName; } else { var thisType = obj.GetType(); if (!IsSerializable(thisType)) throw new InvalidOperationException( string.Format(Resources.ObjectNotSerializableFormatted, thisType.FullName)); var mobile = obj as IMobileObject; if (mobile == null) throw new InvalidOperationException( string.Format(Resources.MustImplementIMobileObject, #if NETFX_CORE thisType.Name())); #else thisType.Name)); #endif if (!_serializationReferences.TryGetValue(mobile, out info)) { info = new SerializationInfo(_serializationReferences.Count + 1); _serializationReferences.Add(mobile, info); info.TypeName = thisType.AssemblyQualifiedName; mobile.GetChildren(info, this); mobile.GetState(info); } } return info; }
/// <summary> /// Serializes an object into a SerializationInfo object. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <returns></returns> public SerializationInfo SerializeObject(object obj) { SerializationInfo info; if (obj == null) { NullPlaceholder nullPlaceholder = new NullPlaceholder(); info = new SerializationInfo(_serializationReferences.Count + 1); _serializationReferences.Add(nullPlaceholder, info); info.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(typeof(NullPlaceholder)); } else { var thisType = obj.GetType(); if (!IsSerializable(thisType)) { throw new InvalidOperationException( string.Format(Resources.ObjectNotSerializableFormatted, thisType.FullName)); } var mobile = obj as IMobileObject; if (mobile == null) { throw new InvalidOperationException( string.Format(Resources.MustImplementIMobileObject, #if NETFX_CORE thisType.Name())); } #else thisType.Name)); #endif if (!_serializationReferences.TryGetValue(mobile, out info)) { info = new SerializationInfo(_serializationReferences.Count + 1); _serializationReferences.Add(mobile, info); info.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(thisType); #if !NET40 && !NET45 && !WINDOWS_UWP && !NETSTANDARD1_5 && !NETSTANDARD1_6 && !PCL46 && !PCL259 if (thisType.Equals(typeof(Security.CslaClaimsPrincipal))) { var principal = (Security.CslaClaimsPrincipal)obj; using (var buffer = new System.IO.MemoryStream()) { using (var writer = new System.IO.BinaryWriter(buffer)) { principal.WriteTo(writer); info.AddValue("s", buffer.ToArray()); } } } else { mobile.GetChildren(info, this); mobile.GetState(info); } #else mobile.GetChildren(info, this); mobile.GetState(info); #endif } } return(info); }