Remove TypedReference.MakeTypedReference readonly restriction (#21193)
authorStephen Toub <stoub@microsoft.com>
Mon, 26 Nov 2018 19:08:38 +0000 (14:08 -0500)
committerJan Kotas <jkotas@microsoft.com>
Mon, 26 Nov 2018 19:08:38 +0000 (11:08 -0800)
TypedReference.MakeTypedReference validates that the fields aren't readonly, which limits our ability to make fields readonly in core while maintaining BinaryFormatter compatibility.  This restriction isn't useful for a variety of reasons, though: reflection allows readonly fields to be set, but more importantly, TypedReference.SetTypedReference has always thrown NotSupportedException, so it's not even actually usable.

This change just removes the readonly check from MakeTypedReference.

src/System.Private.CoreLib/Resources/Strings.resx
src/System.Private.CoreLib/src/System/TypedReference.cs

index dcf61a0dd2d1847328bea77b6d1927d710c8760b..9fa2e10ce759bacb85e66c2eb3d6ddf24d6068ec 100644 (file)
     <value>The DaylightTransitionStart property must not equal the DaylightTransitionEnd property.</value>
   </data>
   <data name="Argument_TypedReferenceInvalidField" xml:space="preserve">
-    <value>Field '{0}' in TypedReferences cannot be static or init only.</value>
+    <value>Field '{0}' in TypedReferences cannot be static.</value>
   </data>
   <data name="Argument_TypeIsWinRTType" xml:space="preserve">
     <value>The type must not be a Windows Runtime type.</value>
index 3f5d5acd4758e03fff9c0acbec64618571cce0cf..152ba70c0d9dab06d3d61bb1407e394e33fd08a6 100644 (file)
@@ -41,7 +41,7 @@ namespace System
                 if (field == null)
                     throw new ArgumentException(SR.Argument_MustBeRuntimeFieldInfo);
 
-                if (field.IsInitOnly || field.IsStatic)
+                if (field.IsStatic)
                     throw new ArgumentException(SR.Format(SR.Argument_TypedReferenceInvalidField, field.Name));
 
                 if (targetType != field.GetDeclaringTypeInternal() && !targetType.IsSubclassOf(field.GetDeclaringTypeInternal()))