Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Move serialization primitives to System.Runtime
Browse files Browse the repository at this point in the history
We need BinaryFormatter to be able to serialize types from coreclr that use the various serialization types (e.g. ISerializable), which means those types must have the same identity as the ones BinaryFormatter recognizes, and we need a bunch of types all across corefx to be able to use these same types to implement serialization.  As such, the types are moving down to System.Runtime, and on coreclr, are type-forwarding to the implementations there.

There are a few outstanding issues:
- SR is not currently available in System.Runtime.  The few uses of SR in the versions of the code used for corert have been replaced by hardcoded strings and TODOs.
- Debug.Assert is not currently available in System.Runtime.  Asserts have been commented out with TODOs.
- To get things to build as types are moving around, P2P references are used.
  • Loading branch information
stephentoub authored and joperezr committed Aug 15, 2016
1 parent 6b2664a commit 90be359
Show file tree
Hide file tree
Showing 36 changed files with 389 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,145 +8,15 @@
using System;
using System.Collections;

namespace System
{
[AttributeUsage(AttributeTargets.Field, Inherited = false)]
public sealed class NonSerializedAttribute : Attribute
{
public NonSerializedAttribute()
{
}
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Delegate, Inherited = false)]
public sealed class SerializableAttribute : Attribute
{
public SerializableAttribute()
{
}
}
}
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.NonSerializedAttribute))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.SerializableAttribute))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.IDeserializationCallback))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.IFormatterConverter))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.ISerializable))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.SerializationEntry))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.SerializationInfo))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.SerializationInfoEnumerator))]

namespace System.Runtime.Serialization
{
public interface IObjectReference
{
object GetRealObject(StreamingContext context);
}
[System.AttributeUsageAttribute(System.AttributeTargets.Field, Inherited = false)]
public sealed partial class OptionalFieldAttribute : System.Attribute
{
public OptionalFieldAttribute() { }
public int VersionAdded { get { return default(int); } set { } }
}
[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);
[CLSCompliant(false)]
sbyte ToSByte(object value);
byte ToByte(object value);
short ToInt16(object value);
[CLSCompliant(false)]
ushort ToUInt16(object value);
int ToInt32(object value);
[CLSCompliant(false)]
uint ToUInt32(object value);
long ToInt64(object value);
[CLSCompliant(false)]
ulong ToUInt64(object value);
float ToSingle(object value);
double ToDouble(object value);
Decimal ToDecimal(object value);
DateTime ToDateTime(object value);
String ToString(object value);
}

public interface ISerializable
{
void GetObjectData(SerializationInfo info, StreamingContext context);
}

public interface IDeserializationCallback
{
void OnDeserialization(object sender);
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct SerializationEntry
{
public string Name { get { throw null; } }
public Type ObjectType { get { throw null; } }
public object Value { get { throw null; } }
}

public sealed class SerializationInfo
{
[CLSCompliant(false)]
public SerializationInfo(Type type, IFormatterConverter converter) { }
public string AssemblyName { get { throw null; } set { } }
public string FullTypeName { get { throw null; } set { } }
public int MemberCount { get { throw null; } }
public Type ObjectType { get { throw null; } }
public void AddValue(string name, bool value) { }
public void AddValue(string name, byte value) { }
public void AddValue(string name, char value) { }
public void AddValue(string name, DateTime value) { }
public void AddValue(string name, decimal value) { }
public void AddValue(string name, double value) { }
public void AddValue(string name, short value) { }
public void AddValue(string name, int value) { }
public void AddValue(string name, long value) { }
public void AddValue(string name, object value) { }
public void AddValue(string name, object value, Type type) { }
[CLSCompliant(false)]
public void AddValue(string name, sbyte value) { }
public void AddValue(string name, float value) { }
[CLSCompliant(false)]
public void AddValue(string name, ushort value) { }
[CLSCompliant(false)]
public void AddValue(string name, uint value) { }
[CLSCompliant(false)]
public void AddValue(string name, ulong value) { }
public bool GetBoolean(string name) { throw null; }
public byte GetByte(string name) { throw null; }
public char GetChar(string name) { throw null; }
public DateTime GetDateTime(string name) { throw null; }
public decimal GetDecimal(string name) { throw null; }
public double GetDouble(string name) { throw null; }
public SerializationInfoEnumerator GetEnumerator() { throw null; }
public short GetInt16(string name) { throw null; }
public int GetInt32(string name) { throw null; }
public long GetInt64(string name) { throw null; }
[CLSCompliant(false)]
public sbyte GetSByte(string name) { throw null; }
public float GetSingle(string name) { throw null; }
public string GetString(string name) { throw null; }
[CLSCompliant(false)]
public ushort GetUInt16(string name) { throw null; }
[CLSCompliant(false)]
public uint GetUInt32(string name) { throw null; }
[CLSCompliant(false)]
public ulong GetUInt64(string name) { throw null; }
public object GetValue(string name, Type type) { throw null; }
public void SetType(Type type) { }
}

public sealed class SerializationInfoEnumerator : IEnumerator
{
private SerializationInfoEnumerator() { }
public SerializationEntry Current { get { throw null; } }
public string Name { get { throw null; } }
public Type ObjectType { get { throw null; } }
object System.Collections.IEnumerator.Current { get { throw null; } }
public object Value { get { throw null; } }
public bool MoveNext() { throw null; }
public void Reset() { throw null; }
}
}
namespace System.Runtime.Serialization
{
[System.CLSCompliantAttribute(false)]
Expand Down Expand Up @@ -248,22 +118,6 @@ public ObjectIDGenerator() { }
public virtual long GetId(object obj, out bool firstTime) { firstTime = default(bool); return default(long); }
public virtual long HasId(object obj, out bool firstTime) { firstTime = default(bool); return default(long); }
}
public partial class ObjectManager
{
public ObjectManager(System.Runtime.Serialization.ISurrogateSelector selector, System.Runtime.Serialization.StreamingContext context) { }
public virtual void DoFixups() { }
public virtual object GetObject(long objectID) { return default(object); }
public virtual void RaiseDeserializationEvent() { }
public void RaiseOnDeserializingEvent(object obj) { }
public virtual void RecordArrayElementFixup(long arrayToBeFixed, int index, long objectRequired) { }
public virtual void RecordArrayElementFixup(long arrayToBeFixed, int[] indices, long objectRequired) { }
public virtual void RecordDelayedFixup(long objectToBeFixed, string memberName, long objectRequired) { }
public virtual void RecordFixup(long objectToBeFixed, System.Reflection.MemberInfo member, long objectRequired) { }
public virtual void RegisterObject(object obj, long objectID) { }
public void RegisterObject(object obj, long objectID, System.Runtime.Serialization.SerializationInfo info) { }
public void RegisterObject(object obj, long objectID, System.Runtime.Serialization.SerializationInfo info, long idOfContainingObj, System.Reflection.MemberInfo member) { }
public void RegisterObject(object obj, long objectID, System.Runtime.Serialization.SerializationInfo info, long idOfContainingObj, System.Reflection.MemberInfo member, int[] arrayIndex) { }
}
public abstract partial class SerializationBinder
{
protected SerializationBinder() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
<PropertyGroup>
<AssemblyVersion>4.0.1.0</AssemblyVersion>
<OutputType>Library</OutputType>
<PackageTargetFramework>netstandard1.3</PackageTargetFramework>
<PackageTargetFramework>netstandard1.5</PackageTargetFramework>
<NuGetTargetMoniker>.NETStandard,Version=v1.3</NuGetTargetMoniker>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Runtime.Serialization.Formatters.cs" />
</ItemGroup>
<ItemGroup>
<None Include="project.json" />
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.Serialization.Primitives\ref\System.Runtime.Serialization.Primitives.csproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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.

[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.NonSerializedAttribute))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.SerializableAttribute))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.IDeserializationCallback))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.IFormatterConverter))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.ISerializable))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.SerializationEntry))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.SerializationInfo))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.Serialization.SerializationInfoEnumerator))]
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,26 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
<ItemGroup Condition="'$(TargetGroup)' == ''">
<Compile Include="System\NonSerializedAttribute.cs" />
<Compile Include="System\SerializableAttribute.cs" />
<Compile Include="System.Runtime.Serialization.Formatters.TypeForwards.cs" />
<Compile Include="System\TemporaryStubs.cs" />
<Compile Include="System\Runtime\Serialization\DeserializationEventHandler.cs" />
<Compile Include="System\Runtime\Serialization\Formatter.cs" />
<Compile Include="System\Runtime\Serialization\FormatterConverter.cs" />
<Compile Include="System\Runtime\Serialization\FormatterServices.cs" />
<Compile Include="System\Runtime\Serialization\IDeserializationCallback.cs" />
<Compile Include="System\Runtime\Serialization\IFormatter.cs" />
<Compile Include="System\Runtime\Serialization\IFormatterConverter.cs" />
<Compile Include="System\Runtime\Serialization\IObjectReference.cs" />
<Compile Include="System\Runtime\Serialization\ISerializable.cs" />
<Compile Include="System\Runtime\Serialization\ISerializationSurrogate.cs" />
<Compile Include="System\Runtime\Serialization\ISurrogateSelector.cs" />
<Compile Include="System\Runtime\Serialization\MemberHolder.cs" />
<Compile Include="System\Runtime\Serialization\ObjectIDGenerator.cs" />
<Compile Include="System\Runtime\Serialization\ObjectManager.cs" />
<Compile Include="System\Runtime\Serialization\SafeSerializationEventArgs.cs" />
<Compile Include="System\Runtime\Serialization\OptionalFieldAttribute.cs" />
<Compile Include="System\Runtime\Serialization\SerializationBinder.cs" />
<Compile Include="System\Runtime\Serialization\SerializationEventsCache.cs" />
<Compile Include="System\Runtime\Serialization\SerializationInfo.cs" />
<Compile Include="System\Runtime\Serialization\SerializationInfoEnumerator.cs" />
<Compile Include="System\Runtime\Serialization\SerializationObjectManager.cs" />
<Compile Include="System\Runtime\Serialization\SurrogateSelector.cs" />
<Compile Include="System\Runtime\Serialization\ValueTypeFixupInfo.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\CommonEnums.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\IFieldInfo.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryArray.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryAssembly.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryAssemblyInfo.cs" />
Expand Down Expand Up @@ -69,9 +64,8 @@
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectWriter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryParser.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryUtilClasses.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\CommonEnums.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\IFieldInfo.cs" />
<Compile Include="System\TemporaryStubs.cs" />
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.Serialization.Primitives\ref\System.Runtime.Serialization.Primitives.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="project.json" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public static object GetUninitializedObject(Type type)

public static object GetSafeUninitializedObject(Type type) => GetUninitializedObject(type);

internal static bool UnsafeTypeForwardersIsEnabled() => true;

internal static void SerializationSetValue(MemberInfo fi, object target, object value)
{
Debug.Assert(fi != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal void InitSerialize(object obj, ISurrogateSelector surrogateSelector, St
{
throw new SerializationException(SR.Format(SR.Serialization_NonSerType, _objectType.FullName, _objectType.GetTypeInfo().Assembly.FullName));
}
_si = new SerializationInfo(_objectType, converter, !FormatterServices.UnsafeTypeForwardersIsEnabled());
_si = new SerializationInfo(_objectType, converter);
((ISerializable)obj).GetObjectData(_si, context);
InitSiWrite();
CheckTypeForwardedFrom(_cache, _objectType, _binderAssemblyString);
Expand Down Expand Up @@ -154,7 +154,7 @@ internal void InitSerialize(Type objectType, ISurrogateSelector surrogateSelecto
}
else if (!ReferenceEquals(objectType, Converter.s_typeofObject) && Converter.s_typeofISerializable.IsAssignableFrom(objectType))
{
_si = new SerializationInfo(objectType, converter, !FormatterServices.UnsafeTypeForwardersIsEnabled());
_si = new SerializationInfo(objectType, converter);
_cache = new SerObjectInfoCache(objectType);
CheckTypeForwardedFrom(_cache, objectType, _binderAssemblyString);
_isSi = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1624,4 +1624,17 @@ internal TypeLoadExceptionHolder(string typeName)

internal string TypeName { get; }
}

// TODO: Temporary workaround. Remove this once SerializationInfo.UpdateValue is exposed
// from coreclr for use by ObjectManager.
internal static class SerializationInfoExtensions
{
private static readonly Action<SerializationInfo, string, object, Type> s_updateValue =
(Action<SerializationInfo, string, object, Type>)typeof(SerializationInfo)
.GetMethod("UpdateValue", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.CreateDelegate(typeof(Action<SerializationInfo, string, object, Type>));

public static void UpdateValue(this SerializationInfo si, string name, object value, Type type) =>
s_updateValue(si, name, value, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

namespace System
{
internal interface ICloneable // TODO: Replace with real ICloneable when it's available
{
object Clone();
}

internal class MarshalByRefObject { } // TODO: Replace with real MarshalByRefObject when it's available

internal sealed class TypedReference // TODO: Replace with System.TypedReference when available and functional
Expand Down Expand Up @@ -47,6 +42,7 @@ namespace System.Reflection
{
internal static class CustomReflectionExtensions
{
// TODO: Replace with FieldInfo.SetValueDirect when available and functional
internal static void SetValueDirect(this FieldInfo field, TypedReference obj, object value) // TODO: Replace with FieldInfo.SetValueDirect when available
{
if (field == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public static IEnumerable<object> SerializableObjects()
yield return (StructWithIntField?)new StructWithIntField() { X = 42 };

// Other core serializable types
yield return DateTime.Now;
yield return TimeSpan.FromDays(7);
yield return new Version(1, 2, 3, 4);
yield return Tuple.Create(1, "2", Tuple.Create(3.4));
Expand Down Expand Up @@ -192,7 +191,6 @@ public static IEnumerable<object> NonSerializableObjects()
yield return new object[] { 1, 2, 3, new NonSerializableClass() };

// TODO: Move these to the serializable category when enabled in the runtime
yield return DateTimeOffset.Now;
var e = new OperationCanceledException();
yield return e;
try { throw e; } catch { } yield return e;
Expand Down
Loading

0 comments on commit 90be359

Please sign in to comment.