Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5f696b0

Browse files
atsushikandanmoseley
authored andcommitted
Fix regression in third party ParameterInfo serialization. (#12038)
Fix https://github.com/dotnet/corefx/issues/20574 The decision to make the runtime's implementation of Reflection objects non-serializable went too far and changed the behaviors of methods inherited by all Reflection implementors. In particular, by changing ParameterInfo.GetRealObject() to throw PNSE, all third-party implementations of ParameterInfo become undeserializable, not just the runtime's implementation. The intended change was to make the runtime's implementation non-serializable which is already accomplished by removing [Serializable] from RuntimeParameterInfo. The methods on the abstract base classes for Reflection are "apis" to all Reflection implementors and their long-standing behaviors should not change based on decisions made by the runtime's particular implementation. This commit rolls back those changes that were introduced as part of #12020.
1 parent f476e38 commit 5f696b0

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/mscorlib/shared/System/Reflection/Assembly.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ public virtual object CreateInstance(string typeName, bool ignoreCase, BindingFl
125125
public virtual FileStream[] GetFiles() => GetFiles(getResourceModules: false);
126126
public virtual FileStream[] GetFiles(bool getResourceModules) { throw NotImplemented.ByDesign; }
127127

128-
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
129-
{
130-
throw new PlatformNotSupportedException();
131-
}
128+
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; }
132129

133130
public override string ToString()
134131
{

src/mscorlib/shared/System/Reflection/Module.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
110110
public Type ResolveType(int metadataToken) => ResolveType(metadataToken, null, null);
111111
public virtual Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; }
112112

113-
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
114-
{
115-
throw new PlatformNotSupportedException();
116-
}
113+
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; }
117114

118115
public override bool Equals(object o) => base.Equals(o);
119116
public override int GetHashCode() => base.GetHashCode();

src/mscorlib/shared/System/Reflection/ParameterInfo.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,46 @@ public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
5454

5555
public object GetRealObject(StreamingContext context)
5656
{
57-
throw new PlatformNotSupportedException();
57+
// Once all the serializable fields have come in we can set up the real
58+
// instance based on just two of them (MemberImpl and PositionImpl).
59+
60+
if (MemberImpl == null)
61+
throw new SerializationException(SR.Serialization_InsufficientState);
62+
63+
ParameterInfo[] args = null;
64+
65+
switch (MemberImpl.MemberType)
66+
{
67+
case MemberTypes.Constructor:
68+
case MemberTypes.Method:
69+
if (PositionImpl == -1)
70+
{
71+
if (MemberImpl.MemberType == MemberTypes.Method)
72+
return ((MethodInfo)MemberImpl).ReturnParameter;
73+
else
74+
throw new SerializationException(SR.Serialization_BadParameterInfo);
75+
}
76+
else
77+
{
78+
args = ((MethodBase)MemberImpl).GetParametersNoCopy();
79+
80+
if (args != null && PositionImpl < args.Length)
81+
return args[PositionImpl];
82+
else
83+
throw new SerializationException(SR.Serialization_BadParameterInfo);
84+
}
85+
86+
case MemberTypes.Property:
87+
args = ((PropertyInfo)MemberImpl).GetIndexParameters();
88+
89+
if (args != null && PositionImpl > -1 && PositionImpl < args.Length)
90+
return args[PositionImpl];
91+
else
92+
throw new SerializationException(SR.Serialization_BadParameterInfo);
93+
94+
default:
95+
throw new SerializationException(SR.Serialization_NoParameterInfo);
96+
}
5897
}
5998

6099
public override string ToString() => ParameterType.FormatTypeName() + " " + Name;

0 commit comments

Comments
 (0)