summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/Serialization
diff options
context:
space:
mode:
authorJiyoung Yun <jy910.yun@samsung.com>2017-04-13 14:17:19 +0900
committerJiyoung Yun <jy910.yun@samsung.com>2017-04-13 14:17:19 +0900
commita56e30c8d33048216567753d9d3fefc2152af8ac (patch)
tree7e5d979695fc4a431740982eb1cfecc2898b23a5 /src/mscorlib/src/System/Runtime/Serialization
parent4b11dc566a5bbfa1378d6266525c281b028abcc8 (diff)
downloadcoreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.tar.gz
coreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.tar.bz2
coreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.zip
Imported Upstream version 2.0.0.11353upstream/2.0.0.11353
Diffstat (limited to 'src/mscorlib/src/System/Runtime/Serialization')
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/FormatterConverter.cs121
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/FormatterServices.cs75
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/IDeserializationCallback.cs22
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/IFormatterConverter.cs39
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/IObjectReference.cs27
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/ISerializable.cs30
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/SafeSerializationManager.cs240
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/SerializationAttributes.cs59
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/SerializationException.cs44
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/SerializationInfo.cs41
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/SerializationInfoEnumerator.cs145
-rw-r--r--src/mscorlib/src/System/Runtime/Serialization/StreamingContext.cs72
12 files changed, 128 insertions, 787 deletions
diff --git a/src/mscorlib/src/System/Runtime/Serialization/FormatterConverter.cs b/src/mscorlib/src/System/Runtime/Serialization/FormatterConverter.cs
index 27f7ecf9d2..52e748db89 100644
--- a/src/mscorlib/src/System/Runtime/Serialization/FormatterConverter.cs
+++ b/src/mscorlib/src/System/Runtime/Serialization/FormatterConverter.cs
@@ -12,42 +12,53 @@
**
**
============================================================*/
-namespace System.Runtime.Serialization {
- using System;
- using System.Globalization;
- using System.Diagnostics.Contracts;
- internal class FormatterConverter : IFormatterConverter {
+using System;
+using System.Globalization;
+using System.Diagnostics.Contracts;
- public FormatterConverter() {
+namespace System.Runtime.Serialization
+{
+ internal class FormatterConverter : IFormatterConverter
+ {
+ public FormatterConverter()
+ {
}
- public Object Convert(Object value, Type type) {
- if (value==null) {
+ public Object Convert(Object value, Type type)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
}
- public Object Convert(Object value, TypeCode typeCode) {
- if (value==null) {
+ public Object Convert(Object value, TypeCode typeCode)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ChangeType(value, typeCode, CultureInfo.InvariantCulture);
}
- public bool ToBoolean(Object value) {
- if (value==null) {
+ public bool ToBoolean(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);
}
- public char ToChar(Object value) {
- if (value==null) {
+ public char ToChar(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
@@ -55,24 +66,30 @@ namespace System.Runtime.Serialization {
}
[CLSCompliant(false)]
- public sbyte ToSByte(Object value) {
- if (value==null) {
+ public sbyte ToSByte(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToSByte(value, CultureInfo.InvariantCulture);
}
- public byte ToByte(Object value) {
- if (value==null) {
+ public byte ToByte(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToByte(value, CultureInfo.InvariantCulture);
}
- public short ToInt16(Object value) {
- if (value==null) {
+ public short ToInt16(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
@@ -80,16 +97,20 @@ namespace System.Runtime.Serialization {
}
[CLSCompliant(false)]
- public ushort ToUInt16(Object value) {
- if (value==null) {
+ public ushort ToUInt16(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToUInt16(value, CultureInfo.InvariantCulture);
}
- public int ToInt32(Object value) {
- if (value==null) {
+ public int ToInt32(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
@@ -97,16 +118,20 @@ namespace System.Runtime.Serialization {
}
[CLSCompliant(false)]
- public uint ToUInt32(Object value) {
- if (value==null) {
+ public uint ToUInt32(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToUInt32(value, CultureInfo.InvariantCulture);
}
- public long ToInt64(Object value) {
- if (value==null) {
+ public long ToInt64(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
@@ -114,48 +139,60 @@ namespace System.Runtime.Serialization {
}
[CLSCompliant(false)]
- public ulong ToUInt64(Object value) {
- if (value==null) {
+ public ulong ToUInt64(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToUInt64(value, CultureInfo.InvariantCulture);
- }
+ }
- public float ToSingle(Object value) {
- if (value==null) {
+ public float ToSingle(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToSingle(value, CultureInfo.InvariantCulture);
}
- public double ToDouble(Object value) {
- if (value==null) {
+ public double ToDouble(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToDouble(value, CultureInfo.InvariantCulture);
}
- public Decimal ToDecimal(Object value) {
- if (value==null) {
+ public Decimal ToDecimal(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToDecimal(value, CultureInfo.InvariantCulture);
}
- public DateTime ToDateTime(Object value) {
- if (value==null) {
+ public DateTime ToDateTime(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
return System.Convert.ToDateTime(value, CultureInfo.InvariantCulture);
}
- public String ToString(Object value) {
- if (value==null) {
+ public String ToString(Object value)
+ {
+ if (value == null)
+ {
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
@@ -163,4 +200,4 @@ namespace System.Runtime.Serialization {
}
}
}
-
+
diff --git a/src/mscorlib/src/System/Runtime/Serialization/FormatterServices.cs b/src/mscorlib/src/System/Runtime/Serialization/FormatterServices.cs
index c3f9eb4452..18139324a3 100644
--- a/src/mscorlib/src/System/Runtime/Serialization/FormatterServices.cs
+++ b/src/mscorlib/src/System/Runtime/Serialization/FormatterServices.cs
@@ -11,25 +11,28 @@
**
**
============================================================*/
-namespace System.Runtime.Serialization {
-
- using System;
- using System.Reflection;
- using System.Collections;
- using System.Collections.Generic;
- using System.Security;
- using System.Runtime.Remoting;
- using System.Runtime.CompilerServices;
- using System.Runtime.Versioning;
- using System.Threading;
- using System.IO;
- using System.Text;
- using System.Globalization;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
- public static class FormatterServices {
-
+using System;
+using System.Reflection;
+using System.Collections;
+using System.Collections.Generic;
+using System.Security;
+using System.Runtime.Remoting;
+using System.Runtime.CompilerServices;
+using System.Runtime.Versioning;
+using System.Threading;
+using System.IO;
+using System.Text;
+using System.Globalization;
+using System.Diagnostics;
+using System.Diagnostics.Contracts;
+
+namespace System.Runtime.Serialization
+{
+ // This class duplicates a class on CoreFX. We are keeping it here -- just this one method --
+ // as it was widely invoked by reflection to workaround it being missing in .NET Core 1.0
+ internal static class FormatterServices
+ {
// Gets a new instance of the object. The entire object is initalized to 0 and no
// constructors have been run. **THIS MEANS THAT THE OBJECT MAY NOT BE IN A STATE
// CONSISTENT WITH ITS INTERNAL REQUIREMENTS** This method should only be used for
@@ -37,42 +40,24 @@ namespace System.Runtime.Serialization {
// will not create an unitialized string because it is non-sensical to create an empty
// instance of an immutable type.
//
- public static Object GetUninitializedObject(Type type) {
- if ((object)type == null) {
+ public static Object GetUninitializedObject(Type type)
+ {
+ if ((object)type == null)
+ {
throw new ArgumentNullException(nameof(type));
}
Contract.EndContractBlock();
-
- if (!(type is RuntimeType)) {
- throw new SerializationException(Environment.GetResourceString("Serialization_InvalidType", type.ToString()));
+
+ if (!(type is RuntimeType))
+ {
+ throw new SerializationException(SR.Format(SR.Serialization_InvalidType, type.ToString()));
}
return nativeGetUninitializedObject((RuntimeType)type);
}
-
+
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern Object nativeGetUninitializedObject(RuntimeType type);
- private static Binder s_binder = Type.DefaultBinder;
-
- /*============================LoadAssemblyFromString============================
- **Action: Loads an assembly from a given string. The current assembly loading story
- ** is quite confusing. If the assembly is in the fusion cache, we can load it
- ** using the stringized-name which we transmitted over the wire. If that fails,
- ** we try for a lookup of the assembly using the simple name which is the first
- ** part of the assembly name. If we can't find it that way, we'll return null
- ** as our failure result.
- **Returns: The loaded assembly or null if it can't be found.
- **Arguments: assemblyName -- The stringized assembly name.
- **Exceptions: None
- ==============================================================================*/
- internal static Assembly LoadAssemblyFromString(String assemblyName) {
- //
- // Try using the stringized assembly name to load from the fusion cache.
- //
- BCLDebug.Trace("SER", "[LoadAssemblyFromString]Looking for assembly: ", assemblyName);
- Assembly found = Assembly.Load(assemblyName);
- return found;
- }
}
}
diff --git a/src/mscorlib/src/System/Runtime/Serialization/IDeserializationCallback.cs b/src/mscorlib/src/System/Runtime/Serialization/IDeserializationCallback.cs
deleted file mode 100644
index 2911a4016e..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/IDeserializationCallback.cs
+++ /dev/null
@@ -1,22 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-** Interface: IDeserializationEventListener
-**
-**
-** Purpose: Implemented by any class that wants to indicate that
-** it wishes to receive deserialization events.
-**
-**
-===========================================================*/
-namespace System.Runtime.Serialization {
- using System;
-
- // Interface does not need to be marked with the serializable attribute
- public interface IDeserializationCallback {
- void OnDeserialization(Object sender);
- }
-}
diff --git a/src/mscorlib/src/System/Runtime/Serialization/IFormatterConverter.cs b/src/mscorlib/src/System/Runtime/Serialization/IFormatterConverter.cs
deleted file mode 100644
index 0be0db9acd..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/IFormatterConverter.cs
+++ /dev/null
@@ -1,39 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-**
-**
-** Purpose: The interface provides the connection between an
-** instance of SerializationInfo and the formatter-provided
-** class which knows how to parse the data inside the
-** SerializationInfo.
-**
-**
-============================================================*/
-namespace System.Runtime.Serialization {
- using System;
-
- [CLSCompliant(false)]
- public interface IFormatterConverter {
- Object Convert(Object value, Type type);
- Object Convert(Object value, TypeCode typeCode);
- bool ToBoolean(Object value);
- char ToChar(Object value);
- sbyte ToSByte(Object value);
- byte ToByte(Object value);
- short ToInt16(Object value);
- ushort ToUInt16(Object value);
- int ToInt32(Object value);
- uint ToUInt32(Object value);
- long ToInt64(Object value);
- ulong ToUInt64(Object value);
- float ToSingle(Object value);
- double ToDouble(Object value);
- Decimal ToDecimal(Object value);
- DateTime ToDateTime(Object value);
- String ToString(Object value);
- }
-}
diff --git a/src/mscorlib/src/System/Runtime/Serialization/IObjectReference.cs b/src/mscorlib/src/System/Runtime/Serialization/IObjectReference.cs
deleted file mode 100644
index ef5ee6ade0..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/IObjectReference.cs
+++ /dev/null
@@ -1,27 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-** Interface: IObjectReference
-**
-**
-** Purpose: Implemented by objects that are actually references
-** to a different object which can't be discovered until
-** this one is completely restored. During the fixup stage,
-** any object implementing IObjectReference is asked for it's
-** "real" object and that object is inserted into the graph.
-**
-**
-===========================================================*/
-namespace System.Runtime.Serialization {
-
- using System;
- // Interface does not need to be marked with the serializable attribute
- public interface IObjectReference {
- Object GetRealObject(StreamingContext context);
- }
-}
-
-
diff --git a/src/mscorlib/src/System/Runtime/Serialization/ISerializable.cs b/src/mscorlib/src/System/Runtime/Serialization/ISerializable.cs
deleted file mode 100644
index 816aa0484b..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/ISerializable.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-** Interface: ISerializable
-**
-**
-** Purpose: Implemented by any object that needs to control its
-** own serialization.
-**
-**
-===========================================================*/
-
-namespace System.Runtime.Serialization {
- using System.Runtime.Remoting;
- using System.Runtime.Serialization;
- using System;
- using System.Reflection;
-
- public interface ISerializable {
- void GetObjectData(SerializationInfo info, StreamingContext context);
- }
-
-}
-
-
-
-
diff --git a/src/mscorlib/src/System/Runtime/Serialization/SafeSerializationManager.cs b/src/mscorlib/src/System/Runtime/Serialization/SafeSerializationManager.cs
deleted file mode 100644
index 260e873bc7..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/SafeSerializationManager.cs
+++ /dev/null
@@ -1,240 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Diagnostics.Contracts;
-using System.Reflection;
-using System.Security;
-
-namespace System.Runtime.Serialization
-{
- //
- // #SafeSerialization
- //
- // Types which are serializable via the ISerializable interface have a problem when it comes to allowing
- // transparent subtypes which can allow themselves to serialize since the GetObjectData method is
- // SecurityCritical.
- //
- // For instance, System.Exception implements ISerializable, however it is also desirable to have
- // transparent exceptions with their own fields that need to be serialized. (For instance, in transparent
- // assemblies such as the DLR and F#, or even in partial trust application code). Since overriding
- // GetObjectData requires that the overriding method be security critical, this won't work directly.
- //
- // SafeSerializationManager solves this problem by allowing any partial trust code to contribute
- // individual chunks of serializable data to be included in the serialized version of the derived class.
- // These chunks are then deserialized back out of the serialized type and notified that they should
- // populate the fields of the deserialized object when serialization is complete. This allows partial
- // trust or transparent code to participate in serialization of an ISerializable type without having to
- // override GetObjectData or implement the ISerializable constructor.
- //
- // On the serialization side, SafeSerializationManager has an event SerializeObjectState which it will
- // fire in response to serialization in order to gather the units of serializable data that should be
- // stored with the rest of the object during serialization. Methods which respond to these events
- // create serializable objects which implement the ISafeSerializationData interface and add them to the
- // collection of other serialized data by calling AddSerializedState on the SafeSerializationEventArgs
- // passed into the event.
- //
- // By using an event rather than a virtual method on the base ISerializable object, we allow multiple
- // potentially untrusted subclasses to participate in serialization, without each one having to ensure
- // that it calls up to the base type in order for the whole system to work. (For instance Exception :
- // TrustedException : UntrustedException, in this scenario UntrustedException would be able to override
- // the virtual method an prevent TrustedException from ever seeing the method call, either accidentally
- // or maliciously).
- //
- // Further, by only allowing additions of new chunks of serialization state rather than exposing the
- // whole underlying list, we avoid exposing potentially sensitive serialized state to any of the
- // potentially untrusted subclasses.
- //
- // At deserialization time, SafeSerializationManager performs the reverse operation. It deserializes the
- // chunks of serialized state, and then notifies them that the object they belong to is deserialized by
- // calling their CompleteSerialization method. In repsonse to this call, the state objects populate the
- // fields of the object being deserialized with the state that they held.
- //
- // From a security perspective, the chunks of serialized state can only contain data that the specific
- // subclass itself had access to read (otherwise it wouldn't be able to populate the type with that
- // data), as opposed to having access to far more data in the SerializationInfo that GetObjectData uses.
- // Similarly, at deserialization time, the serialized state can only modify fields that the type itself
- // has access to (again, as opposed to the full SerializationInfo which could be modified).
- //
- // Individual types which wish to participate in safe serialization do so by containing an instance of a
- // SafeSerializationManager and exposing its serialization event. During GetObjectData, the
- // SafeSerializationManager is serialized just like any other field of the containing type. However, at
- // the end of serialization it is called back one last time to CompleteSerialization.
- //
- // In CompleteSerialization, if the SafeSerializationManager detects that it has extra chunks of
- // data to handle, it substitutes the root type being serialized (formerly the real type hosting the
- // SafeSerializationManager) with itself. This allows it to gain more control over the deserialization
- // process. It also saves away an extra bit of state in the serialization info indicating the real type
- // of object that should be recreated during deserialization.
- //
- // At this point the serialized state looks like this:
- // Data:
- // realSerializedData1
- // ...
- // realSerializedDataN
- // safeSerializationData -> this is the serialization data member of the parent type
- // m_serializedState -> list of saved serialized states from subclasses responding to the safe
- // serialization event
- // RealTypeSerializationName -> type which is using safe serialization
- // Type:
- // SafeSerializationManager
- //
- // That is, the serialized data claims to be of type SafeSerializationManager, however contains only the
- // data from the real object being serialized along with one bit of safe serialization metadata.
- //
- // At deserialization time, since the serialized data claims to be of type SafeSerializationManager, the
- // root object being created is an instance of the SafeSerializationManager class. However, it detects
- // that this isn't a real SafeSerializationManager (by looking for the real type field in the metadata),
- // and simply saves away the SerializationInfo and the real type being deserialized.
- //
- // Since SafeSerializationManager implements IObjectReference, the next step of deserialization is the
- // GetRealObject callback. This callback is the one responsible for getting the
- // SafeSerializationManager out of the way and instead creating an instance of the actual type which was
- // serialized.
- //
- // It does this by first creating an instance of the real type being deserialzed (saved away in the
- // deserialzation constructor), but not running any of its constructors. Instead, it walks the
- // inheritance hierarchy (moving toward the most derived type) looking for the last full trust type to
- // implement the standard ISerializable constructor before any type does not implement the constructor.
- // It is this last type's deserialization constructor which is then invoked, passing in the saved
- // SerializationInfo. Once the constructors are run, we return this object as the real deserialized
- // object.
- //
- // The reason that we do this walk is so that ISerializable types can protect themselves from malicious
- // input during deserialization by making their deserialization constructors unavailable to partial
- // trust code. By not requiring every type have a copy of this constructor, partial trust code can
- // participate in safe serialization and not be required to have access to the parent's constructor.
- //
- // It should be noted however, that this heuristic means that if a full trust type does derive from
- // a transparent or partial trust type using this safe serialization mechanism, that full trust type
- // will not have its constructor called. Further, the protection of not invoking partial trust
- // deserialization constructors only comes into play if SafeSerializationManager is in control of
- // deserialization, which means there must be at least one (even empty) safe serialization event
- // handler registered.
- //
- // Another interesting note is that at this point there are now two SafeSerializationManagers alive for
- // this deserialization. The first object is the one which is controlling the deserialization and was
- // created as the root object of the deserialization. The second one is the object which contains the
- // serialized data chunks and is a data member of the real object being deserialized. For this reason,
- // the data objects cannot be notified that the deserialization is complete during GetRealObject since
- // the ISafeSerializationData objects are not members of the active SafeSerializationManager instance.
- //
- // The next step is the OnDeserialized callback, which comes to SafeSerializableObject since it was
- // pretending to be the root object of the deserialization. It responds to this callback by calling
- // any existing OnDeserialized callback on the real type that was deserialized.
- //
- // The real type needs to call its data member SafeSerializationData object's CompleteDeserialization
- // method in response to the OnDeserialized call. This CompleteDeserialization call will then iterate
- // through the ISafeSerializationData objects calling each of their CompleteDeserialization methods so
- // that they can plug the nearly-complete object with their saved data.
- //
- // The reason for having a new ISafeSerializationData interface which is basically identical to
- // IDeserializationCallback is that IDeserializationCallback will be called on the stored data chunks
- // by the serialization code when they are deserialized, and that's not a desirable behavior.
- // Essentially, we need to change the meaning of the object parameter to mean "parent object which
- // participated in safe serialization", rather than "this object".
- //
- // Implementing safe serialization on an ISerialiable type is relatively straight forward. (For an
- // example, see System.Exception):
- //
- // 1. Include a data member of type SafeSerializationManager:
- //
- // private SafeSerializationManager m_safeSerializationManager;
- //
- // 2. Add a protected SerializeObjectState event, which passes through to the SafeSerializationManager:
- //
- // protected event EventHandler<SafeSerializationEventArgs> SerializeObjectState
- // {
- // add { m_safeSerializationManager.SerializeObjectState += value; }
- // remove { m_safeSerializationManager.SerializeObjectState -= value; }
- // }
- //
- // 3. Serialize the safe serialization object in GetObjectData, and call its CompleteSerialization method:
- //
- // {
- // info.AddValue("m_safeSerializationManager", m_safeSerializationManager, typeof(SafeSerializationManager));
- // m_safeSerializationManager.CompleteSerialization(this, info, context);
- // }
- //
- // 4. Add an OnDeserialized handler if one doesn't already exist, and call CompleteDeserialization in it:
- //
- // [OnDeserialized]
- // private void OnDeserialized(StreamingContext context)
- // {
- // m_safeSerializationManager.CompleteDeserialization(this);
- // }
- //
- // On the client side, using safe serialization is also pretty easy. For example:
- //
- // [Serializable]
- // public class TransparentException : Exception
- // {
- // [Serializable]
- // private struct TransparentExceptionState : ISafeSerializationData
- // {
- // public string m_extraData;
- //
- // void ISafeSerializationData.CompleteDeserialization(object obj)
- // {
- // TransparentException exception = obj as TransparentException;
- // exception.m_state = this;
- // }
- // }
- //
- // [NonSerialized]
- // private TransparentExceptionState m_state = new TransparentExceptionState();
- //
- // public TransparentException()
- // {
- // SerializeObjectState += delegate(object exception, SafeSerializationEventArgs eventArgs)
- // {
- // eventArgs.AddSerializedState(m_state);
- // };
- // }
- //
- // public string ExtraData
- // {
- // get { return m_state.m_extraData; }
- // set { m_state.m_extraData = value; }
- // }
- // }
- //
-
- // SafeSerializationEventArgs are provided to the delegates which do safe serialization. Each delegate
- // serializes its own state into an IDeserializationCallback instance which must, itself, be serializable.
- // These indivdiual states are then added to the SafeSerializationEventArgs in order to be saved away when
- // the original ISerializable type is serialized.
- public sealed class SafeSerializationEventArgs : EventArgs
- {
- private StreamingContext m_streamingContext;
- private List<object> m_serializedStates = new List<object>();
-
- internal SafeSerializationEventArgs() {}
-
- public void AddSerializedState(ISafeSerializationData serializedState)
- {
- if (serializedState == null)
- throw new ArgumentNullException(nameof(serializedState));
- if (!serializedState.GetType().IsSerializable)
- throw new ArgumentException(Environment.GetResourceString("Serialization_NonSerType", serializedState.GetType(), serializedState.GetType().Assembly.FullName));
-
- m_serializedStates.Add(serializedState);
- }
-
- public StreamingContext StreamingContext
- {
- get { return m_streamingContext; }
- }
- }
-
- // Interface to be supported by objects which are stored in safe serialization stores
- public interface ISafeSerializationData
- {
- // CompleteDeserialization is called when the object to which the extra serialized data was attached
- // has completed its deserialization, and now needs to be populated with the extra data stored in
- // this object.
- void CompleteDeserialization(object deserialized);
- }
-}
diff --git a/src/mscorlib/src/System/Runtime/Serialization/SerializationAttributes.cs b/src/mscorlib/src/System/Runtime/Serialization/SerializationAttributes.cs
deleted file mode 100644
index 38abffa66e..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/SerializationAttributes.cs
+++ /dev/null
@@ -1,59 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-**
-**
-** Purpose: Various Attributes for Serialization
-**
-**
-============================================================*/
-namespace System.Runtime.Serialization
-{
- using System;
- using System.Diagnostics.Contracts;
- using System.Reflection;
-
- [AttributeUsage(AttributeTargets.Field, Inherited=false)]
- public sealed class OptionalFieldAttribute : Attribute
- {
- int versionAdded = 1;
- public OptionalFieldAttribute() { }
-
- public int VersionAdded
- {
- get {
- return this.versionAdded;
- }
- set {
- if (value < 1)
- throw new ArgumentException(Environment.GetResourceString("Serialization_OptionalFieldVersionValue"));
- Contract.EndContractBlock();
- this.versionAdded = value;
- }
- }
- }
-
- [AttributeUsage(AttributeTargets.Method, Inherited=false)]
- public sealed class OnSerializingAttribute : Attribute
- {
- }
-
- [AttributeUsage(AttributeTargets.Method, Inherited=false)]
- public sealed class OnSerializedAttribute : Attribute
- {
- }
-
- [AttributeUsage(AttributeTargets.Method, Inherited=false)]
- public sealed class OnDeserializingAttribute : Attribute
- {
- }
-
- [AttributeUsage(AttributeTargets.Method, Inherited=false)]
- public sealed class OnDeserializedAttribute : Attribute
- {
- }
-
-}
diff --git a/src/mscorlib/src/System/Runtime/Serialization/SerializationException.cs b/src/mscorlib/src/System/Runtime/Serialization/SerializationException.cs
deleted file mode 100644
index e9cb79b7af..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/SerializationException.cs
+++ /dev/null
@@ -1,44 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*=============================================================================
-**
-**
-**
-** Purpose: Thrown when something goes wrong during serialization or
-** deserialization.
-**
-**
-=============================================================================*/
-
-namespace System.Runtime.Serialization {
-
- using System;
- using System.Runtime.Serialization;
-
- [Serializable]
- public class SerializationException : SystemException {
-
- private static String _nullMessage = Environment.GetResourceString("Arg_SerializationException");
-
- // Creates a new SerializationException with its message
- // string set to a default message.
- public SerializationException()
- : base(_nullMessage) {
- SetErrorCode(__HResults.COR_E_SERIALIZATION);
- }
-
- public SerializationException(String message)
- : base(message) {
- SetErrorCode(__HResults.COR_E_SERIALIZATION);
- }
-
- public SerializationException(String message, Exception innerException) : base (message, innerException) {
- SetErrorCode(__HResults.COR_E_SERIALIZATION);
- }
-
- protected SerializationException(SerializationInfo info, StreamingContext context) : base (info, context) {
- }
- }
-}
diff --git a/src/mscorlib/src/System/Runtime/Serialization/SerializationInfo.cs b/src/mscorlib/src/System/Runtime/Serialization/SerializationInfo.cs
index 45521f2397..7fc3ce27d9 100644
--- a/src/mscorlib/src/System/Runtime/Serialization/SerializationInfo.cs
+++ b/src/mscorlib/src/System/Runtime/Serialization/SerializationInfo.cs
@@ -11,25 +11,25 @@
**
**
===========================================================*/
-namespace System.Runtime.Serialization
-{
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Runtime.Remoting;
- using System.Globalization;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
- using System.Security;
- using System.Runtime.CompilerServices;
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Runtime.Remoting;
+using System.Globalization;
+using System.Diagnostics;
+using System.Diagnostics.Contracts;
+using System.Security;
+using System.Runtime.CompilerServices;
+namespace System.Runtime.Serialization
+{
public sealed class SerializationInfo
{
private const int defaultSize = 4;
private const string s_mscorlibAssemblySimpleName = System.CoreLib.Name;
private const string s_mscorlibFileName = s_mscorlibAssemblySimpleName + ".dll";
-
+
// Even though we have a dictionary, we're still keeping all the arrays around for back-compat.
// Otherwise we may run into potentially breaking behaviors like GetEnumerator() not returning entries in the same order they were added.
internal String[] m_members;
@@ -94,7 +94,7 @@ namespace System.Runtime.Serialization
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
-
+
m_fullTypeName = value;
isFullTypeNameSetExplicit = true;
}
@@ -113,9 +113,9 @@ namespace System.Runtime.Serialization
throw new ArgumentNullException(nameof(value));
}
Contract.EndContractBlock();
- if (this.requireSameTokenInPartialTrust)
+ if (requireSameTokenInPartialTrust)
{
- DemandForUnsafeAssemblyNameAssignments(this.m_assemName, value);
+ DemandForUnsafeAssemblyNameAssignments(m_assemName, value);
}
m_assemName = value;
isAssemblyNameSetExplicit = true;
@@ -130,7 +130,7 @@ namespace System.Runtime.Serialization
}
Contract.EndContractBlock();
- if (this.requireSameTokenInPartialTrust)
+ if (requireSameTokenInPartialTrust)
{
DemandForUnsafeAssemblyNameAssignments(this.ObjectType.Assembly.FullName, type.Assembly.FullName);
}
@@ -331,7 +331,7 @@ namespace System.Runtime.Serialization
if (m_nameToIndex.ContainsKey(name))
{
BCLDebug.Trace("SER", "[SerializationInfo.AddValue]Tried to add ", name, " twice to the SI.");
- throw new SerializationException(Environment.GetResourceString("Serialization_SameNameTwice"));
+ throw new SerializationException(SR.Serialization_SameNameTwice);
}
m_nameToIndex.Add(name, m_currMember);
@@ -382,7 +382,6 @@ namespace System.Runtime.Serialization
m_data[index] = value;
m_types[index] = type;
}
-
}
private int FindElement(String name)
@@ -416,7 +415,7 @@ namespace System.Runtime.Serialization
int index = FindElement(name);
if (index == -1)
{
- throw new SerializationException(Environment.GetResourceString("Serialization_NotFound", name));
+ throw new SerializationException(SR.Format(SR.Serialization_NotFound, name));
}
Debug.Assert(index < m_data.Length, "[SerializationInfo.GetElement]index<m_data.Length");
@@ -451,7 +450,6 @@ namespace System.Runtime.Serialization
public Object GetValue(String name, Type type)
{
-
if ((object)type == null)
{
throw new ArgumentNullException(nameof(type));
@@ -460,7 +458,7 @@ namespace System.Runtime.Serialization
RuntimeType rt = type as RuntimeType;
if (rt == null)
- throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
+ throw new ArgumentException(SR.Argument_MustBeRuntimeType);
Type foundType;
Object value;
@@ -698,6 +696,5 @@ namespace System.Runtime.Serialization
}
return m_converter.ToString(value);
}
-
}
}
diff --git a/src/mscorlib/src/System/Runtime/Serialization/SerializationInfoEnumerator.cs b/src/mscorlib/src/System/Runtime/Serialization/SerializationInfoEnumerator.cs
deleted file mode 100644
index 0858dfc19f..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/SerializationInfoEnumerator.cs
+++ /dev/null
@@ -1,145 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-**
-**
-** Purpose: A formatter-friendly mechanism for walking all of
-** the data in a SerializationInfo. Follows the IEnumerator
-** mechanism from Collections.
-**
-**
-============================================================*/
-namespace System.Runtime.Serialization {
- using System;
- using System.Diagnostics;
- using System.Collections;
- using System.Diagnostics.Contracts;
-
- //
- // The tuple returned by SerializationInfoEnumerator.Current.
- //
- public struct SerializationEntry {
- private Type m_type;
- private Object m_value;
- private String m_name;
-
- public Object Value {
- get {
- return m_value;
- }
- }
-
- public String Name {
- get {
- return m_name;
- }
- }
-
- public Type ObjectType {
- get {
- return m_type;
- }
- }
-
- internal SerializationEntry(String entryName, Object entryValue, Type entryType) {
- m_value = entryValue;
- m_name = entryName;
- m_type = entryType;
- }
- }
-
- //
- // A simple enumerator over the values stored in the SerializationInfo.
- // This does not snapshot the values, it just keeps pointers to the
- // member variables of the SerializationInfo that created it.
- //
- public sealed class SerializationInfoEnumerator : IEnumerator {
- String[] m_members;
- Object[] m_data;
- Type[] m_types;
- int m_numItems;
- int m_currItem;
- bool m_current;
-
- internal SerializationInfoEnumerator(String[] members, Object[] info, Type[] types, int numItems) {
- Debug.Assert(members!=null, "[SerializationInfoEnumerator.ctor]members!=null");
- Debug.Assert(info!=null, "[SerializationInfoEnumerator.ctor]info!=null");
- Debug.Assert(types!=null, "[SerializationInfoEnumerator.ctor]types!=null");
- Debug.Assert(numItems>=0, "[SerializationInfoEnumerator.ctor]numItems>=0");
- Debug.Assert(members.Length>=numItems, "[SerializationInfoEnumerator.ctor]members.Length>=numItems");
- Debug.Assert(info.Length>=numItems, "[SerializationInfoEnumerator.ctor]info.Length>=numItems");
- Debug.Assert(types.Length>=numItems, "[SerializationInfoEnumerator.ctor]types.Length>=numItems");
-
- m_members = members;
- m_data = info;
- m_types = types;
- //The MoveNext semantic is much easier if we enforce that [0..m_numItems] are valid entries
- //in the enumerator, hence we subtract 1.
- m_numItems = numItems-1;
- m_currItem = -1;
- m_current = false;
- }
-
- public bool MoveNext() {
- if (m_currItem<m_numItems) {
- m_currItem++;
- m_current = true;
- } else {
- m_current = false;
- }
- return m_current;
- }
-
- /// <internalonly/>
- Object IEnumerator.Current { //Actually returns a SerializationEntry
- get {
- if (m_current==false) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
- }
- return (Object)(new SerializationEntry(m_members[m_currItem], m_data[m_currItem], m_types[m_currItem]));
- }
- }
-
- public SerializationEntry Current { //Actually returns a SerializationEntry
- get {
- if (m_current==false) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
- }
- return (new SerializationEntry(m_members[m_currItem], m_data[m_currItem], m_types[m_currItem]));
- }
- }
-
- public void Reset() {
- m_currItem = -1;
- m_current = false;
- }
-
- public String Name {
- get {
- if (m_current==false) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
- }
- return m_members[m_currItem];
- }
- }
- public Object Value {
- get {
- if (m_current==false) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
- }
- return m_data[m_currItem];
- }
- }
- public Type ObjectType {
- get {
- if (m_current==false) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
- }
- return m_types[m_currItem];
- }
- }
- }
-}
diff --git a/src/mscorlib/src/System/Runtime/Serialization/StreamingContext.cs b/src/mscorlib/src/System/Runtime/Serialization/StreamingContext.cs
deleted file mode 100644
index c01a3edc4f..0000000000
--- a/src/mscorlib/src/System/Runtime/Serialization/StreamingContext.cs
+++ /dev/null
@@ -1,72 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-/*============================================================
-**
-** ValueType: StreamingContext
-**
-**
-** Purpose: A value type indicating the source or destination of our streaming.
-**
-**
-===========================================================*/
-namespace System.Runtime.Serialization {
-
- using System.Runtime.Remoting;
- using System;
- [Serializable]
- public struct StreamingContext {
- internal Object m_additionalContext;
- internal StreamingContextStates m_state;
-
- public StreamingContext(StreamingContextStates state)
- : this (state, null) {
- }
-
- public StreamingContext(StreamingContextStates state, Object additional) {
- m_state = state;
- m_additionalContext = additional;
- }
-
- public Object Context {
- get { return m_additionalContext; }
- }
-
- public override bool Equals(Object obj) {
- if (!(obj is StreamingContext)) {
- return false;
- }
- if (((StreamingContext)obj).m_additionalContext == m_additionalContext &&
- ((StreamingContext)obj).m_state == m_state) {
- return true;
- }
- return false;
- }
-
- public override int GetHashCode() {
- return (int)m_state;
- }
-
- public StreamingContextStates State {
- get { return m_state; }
- }
- }
-
- // **********************************************************
- // Keep these in sync with the version in vm\runtimehandles.h
- // **********************************************************
-[Serializable]
-[Flags]
- public enum StreamingContextStates {
- CrossProcess=0x01,
- CrossMachine=0x02,
- File =0x04,
- Persistence =0x08,
- Remoting =0x10,
- Other =0x20,
- Clone =0x40,
- CrossAppDomain =0x80,
- All =0xFF,
- }
-}