Post-Serialization cleanup fixes to Reflection (#12010)
authorAtsushi Kanamori <AtsushiKan@users.noreply.github.com>
Wed, 31 May 2017 23:40:04 +0000 (16:40 -0700)
committerGitHub <noreply@github.com>
Wed, 31 May 2017 23:40:04 +0000 (16:40 -0700)
This is based on the following principles:

- We should not be making changes to instance/virtual methods
in the abstract base types for Reflection. These are apis
in themselves to third party classes that subclass these types.
Even if the Runtime chooses not to make its own implementations
of these type serializable, third party classes have the right
to make that choice for themselves and build off the base class
serialization infrastructure that was shipped before.

RuntimeAssembly and RuntimeModule still override with PNSE
so we'll get the desired exception for those cases.

- The Runtime's own implementations of these types will not be
serializable. Removing all [Serializable] attributes and
serialization interfaces. Runtime types are internal types and
thus have no duty to implement ISerializable if the public
abstract base type does not.

src/mscorlib/shared/System/Reflection/Assembly.cs
src/mscorlib/shared/System/Reflection/Module.cs
src/mscorlib/shared/System/Reflection/ParameterInfo.cs
src/mscorlib/src/System/Reflection/MdFieldInfo.cs
src/mscorlib/src/System/Reflection/RuntimeConstructorInfo.cs
src/mscorlib/src/System/Reflection/RuntimeEventInfo.cs
src/mscorlib/src/System/Reflection/RuntimeFieldInfo.cs
src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs
src/mscorlib/src/System/Reflection/RuntimeParameterInfo.cs
src/mscorlib/src/System/Reflection/RuntimePropertyInfo.cs
src/mscorlib/src/System/RtType.cs

index b965c9f7fb5e9aa4e6cb2b496a38844d5757e09c..d35ffc7066b1f6dfeb924631e98da17be242d3a6 100644 (file)
@@ -125,10 +125,7 @@ namespace System.Reflection
         public virtual FileStream[] GetFiles() => GetFiles(getResourceModules: false);
         public virtual FileStream[] GetFiles(bool getResourceModules) { throw NotImplemented.ByDesign; }
 
-        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
+        public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; }
 
         public override string ToString()
         {
index 7822e9ff10396ec3662f492a746b7674c1f5d9cd..56f83c40d97c2c6326990aef5f067d0ac97083db 100644 (file)
@@ -110,10 +110,7 @@ namespace System.Reflection
         public Type ResolveType(int metadataToken) => ResolveType(metadataToken, null, null);
         public virtual Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; }
 
-        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
+        public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; }
 
         public override bool Equals(object o) => base.Equals(o);
         public override int GetHashCode() => base.GetHashCode();
index fd130e569b02e27606b05b5e3121330efacd1014..94bfffaa5384cf66934f1c717ce5fd7c007c85f1 100644 (file)
@@ -54,7 +54,46 @@ namespace System.Reflection
 
         public object GetRealObject(StreamingContext context)
         {
-            throw new PlatformNotSupportedException();
+            // Once all the serializable fields have come in we can set up the real
+            // instance based on just two of them (MemberImpl and PositionImpl).
+
+            if (MemberImpl == null)
+                throw new SerializationException(SR.Serialization_InsufficientState);
+
+            ParameterInfo[] args = null;
+
+            switch (MemberImpl.MemberType)
+            {
+                case MemberTypes.Constructor:
+                case MemberTypes.Method:
+                    if (PositionImpl == -1)
+                    {
+                        if (MemberImpl.MemberType == MemberTypes.Method)
+                            return ((MethodInfo)MemberImpl).ReturnParameter;
+                        else
+                            throw new SerializationException(SR.Serialization_BadParameterInfo);
+                    }
+                    else
+                    {
+                        args = ((MethodBase)MemberImpl).GetParametersNoCopy();
+
+                        if (args != null && PositionImpl < args.Length)
+                            return args[PositionImpl];
+                        else
+                            throw new SerializationException(SR.Serialization_BadParameterInfo);
+                    }
+
+                case MemberTypes.Property:
+                    args = ((PropertyInfo)MemberImpl).GetIndexParameters();
+
+                    if (args != null && PositionImpl > -1 && PositionImpl < args.Length)
+                        return args[PositionImpl];
+                    else
+                        throw new SerializationException(SR.Serialization_BadParameterInfo);
+
+                default:
+                    throw new SerializationException(SR.Serialization_NoParameterInfo);
+            }
         }
 
         public override string ToString() => ParameterType.FormatTypeName() + " " + Name;
index 9645b7fef282786bed7c2c99ad2561fa3805c64e..94416d7aa3efdaee99c93d948afb74cdad3cd91b 100644 (file)
@@ -4,12 +4,11 @@
 
 using System.Diagnostics;
 using System.Globalization;
-using System.Runtime.Serialization;
 using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
 
 namespace System.Reflection
 {
-    internal sealed unsafe class MdFieldInfo : RuntimeFieldInfo, ISerializable
+    internal sealed unsafe class MdFieldInfo : RuntimeFieldInfo
     {
         #region Private Data Members
         private int m_tkField;
index 72b2591e2eefa5d5c0a2b99e39baff09196a49c7..7870e0b91ea7e77cc68b5e10963b14f49cf17aa8 100644 (file)
@@ -6,12 +6,11 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.Globalization;
-using System.Runtime.Serialization;
 using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
 
 namespace System.Reflection
 {
-    internal sealed class RuntimeConstructorInfo : ConstructorInfo, ISerializable, IRuntimeMethodInfo
+    internal sealed class RuntimeConstructorInfo : ConstructorInfo, IRuntimeMethodInfo
     {
         #region Private Data Members
         private volatile RuntimeType m_declaringType;
@@ -462,18 +461,5 @@ namespace System.Reflection
             return RuntimeMethodHandle.InvokeMethod(null, null, sig, true);
         }
         #endregion
-
-        #region ISerializable Implementation
-        public void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
-
-        internal string SerializationToString()
-        {
-            // We don't need the return type for constructors.
-            return FormatNameAndSig(true);
-        }
-        #endregion
     }
 }
index 414b1efafe71edb96fa63a63d24141c64d87d4dc..beea874c9d01e08ac19da338e903212ec4fc8bb8 100644 (file)
@@ -5,12 +5,11 @@
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
-using System.Runtime.Serialization;
 using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
 
 namespace System.Reflection
 {
-    internal unsafe sealed class RuntimeEventInfo : EventInfo, ISerializable
+    internal unsafe sealed class RuntimeEventInfo : EventInfo
     {
         #region Private Data Members
         private int m_token;
@@ -156,13 +155,6 @@ namespace System.Reflection
         internal RuntimeModule GetRuntimeModule() { return m_declaringType.GetRuntimeModule(); }
         #endregion
 
-        #region ISerializable
-        public void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
-        #endregion
-
         #region EventInfo Overrides
         public override MethodInfo[] GetOtherMethods(bool nonPublic)
         {
index 3c6e9493ea39488d28cee9531ffe7a9cb8af1f08..9f1f6340935073e772b78a8972230df1833ba045 100644 (file)
@@ -4,12 +4,11 @@
 
 using System.Collections.Generic;
 using System.Diagnostics.Contracts;
-using System.Runtime.Serialization;
 using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
 
 namespace System.Reflection
 {
-    internal abstract class RuntimeFieldInfo : FieldInfo, ISerializable
+    internal abstract class RuntimeFieldInfo : FieldInfo
     {
         #region Private Data Members
         private BindingFlags m_bindingFlags;
@@ -122,12 +121,5 @@ namespace System.Reflection
         #region FieldInfo Overrides
         // All implemented on derived classes
         #endregion
-
-        #region ISerializable Implementation
-        public void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
-        #endregion
     }
 }
index 6d90bf7811ee7915f6eb6fa304b69de86e1b6c41..f05508de7b51c549b70e38a8b6c27d59a99ffe88 100644 (file)
@@ -6,7 +6,6 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.Globalization;
-using System.Runtime.Serialization;
 using System.Security;
 using System.Text;
 using System.Threading;
@@ -14,7 +13,7 @@ using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
 
 namespace System.Reflection
 {
-    internal sealed class RuntimeMethodInfo : MethodInfo, ISerializable, IRuntimeMethodInfo
+    internal sealed class RuntimeMethodInfo : MethodInfo, IRuntimeMethodInfo
     {
         #region Private Data Members
         private IntPtr m_handle;
@@ -770,18 +769,6 @@ namespace System.Reflection
         }
         #endregion
 
-        #region ISerializable Implementation
-        public void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
-
-        internal string SerializationToString()
-        {
-            return ReturnType.FormatTypeName(true) + " " + FormatNameAndSig(true);
-        }
-        #endregion
-
         #region Legacy Internal
         internal static MethodBase InternalGetCurrentMethod(ref StackCrawlMark stackMark)
         {
index d21af03649fd4f64f8c31488e46484e18293f5be..8c07d8f39742059b5edb14de52408178d62e9145 100644 (file)
@@ -5,13 +5,12 @@
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
-using System.Runtime.Serialization;
 using System.Runtime.CompilerServices;
 using MdToken = System.Reflection.MetadataToken;
 
 namespace System.Reflection
 {
-    internal unsafe sealed class RuntimeParameterInfo : ParameterInfo, ISerializable
+    internal unsafe sealed class RuntimeParameterInfo : ParameterInfo
     {
         #region Static Members
         internal unsafe static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
@@ -160,13 +159,6 @@ namespace System.Reflection
         }
         #endregion
 
-        #region VTS magic to serialize/deserialized to/from pre-Whidbey endpoints.
-        public void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
-        #endregion
-
         #region Constructor
         // used by RuntimePropertyInfo
         internal RuntimeParameterInfo(RuntimeParameterInfo accessor, RuntimePropertyInfo property)
index 8010789d757d443f99a91928805c40055488ee72..2d54d752241ce9b9355302026a2a2abe405ad533 100644 (file)
@@ -6,13 +6,12 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.Globalization;
-using System.Runtime.Serialization;
 using System.Text;
 using RuntimeTypeCache = System.RuntimeType.RuntimeTypeCache;
 
 namespace System.Reflection
 {
-    internal unsafe sealed class RuntimePropertyInfo : PropertyInfo, ISerializable
+    internal unsafe sealed class RuntimePropertyInfo : PropertyInfo
     {
         #region Private Data Members
         private int m_token;
@@ -448,17 +447,5 @@ namespace System.Reflection
         #endregion
 
         #endregion
-
-        #region ISerializable Implementation
-        public void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
-
-        internal string SerializationToString()
-        {
-            return FormatNameAndSig(true);
-        }
-        #endregion
     }
 }
index 8fdc85a4e36b03034f45d5607aae93203931122b..871a39db4b365adc2d8411853370e468f330f0bf 100644 (file)
@@ -19,7 +19,6 @@ using System.Diagnostics;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime;
-using System.Runtime.Serialization;
 using System.Runtime.CompilerServices;
 using System.Security;
 using System.Text;
@@ -72,7 +71,7 @@ namespace System
     }
 
     internal class RuntimeType :
-        System.Reflection.TypeInfo, ISerializable, ICloneable
+        System.Reflection.TypeInfo, ICloneable
     {
         #region Definitions
 
@@ -4444,13 +4443,6 @@ namespace System
         }
         #endregion
 
-        #region ISerializable
-        public void GetObjectData(SerializationInfo info, StreamingContext context)
-        {
-            throw new PlatformNotSupportedException();
-        }
-        #endregion
-
         #region ICustomAttributeProvider
         public override Object[] GetCustomAttributes(bool inherit)
         {