Remove TypedReference.MakeTypedReference readonly restriction (dotnet/coreclr#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.

Commit migrated from https://github.com/dotnet/coreclr/commit/a6729dab35fde5d50fb12b06aeb77d3e1f3be872

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

index dcf61a0..9fa2e10 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 3f5d5ac..152ba70 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()))