Skip to content

Commit b74cd2b

Browse files
committed
Remove validation that failing when setting constants with core assembly type
1 parent f61c672 commit b74cd2b

7 files changed

Lines changed: 34 additions & 99 deletions

File tree

src/libraries/System.Reflection.Emit/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,7 @@
303303
<data name="InvalidOperation_UnmatchingSymScope" xml:space="preserve">
304304
<value>Unmatching symbol scope.</value>
305305
</data>
306+
<data name="Argument_MustBeEnum" xml:space="preserve">
307+
<value>Type provided must be an Enum.</value>
308+
</data>
306309
</root>

src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/FieldBuilderImpl.cs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -40,78 +40,10 @@ internal FieldBuilderImpl(TypeBuilderImpl typeBuilder, string fieldName, Type ty
4040
protected override void SetConstantCore(object? defaultValue)
4141
{
4242
_typeBuilder.ThrowIfCreated();
43-
ValidateDefaultValueType(defaultValue, _fieldType);
4443
_defaultValue = defaultValue;
4544
_attributes |= FieldAttributes.HasDefault;
4645
}
4746

48-
internal static void ValidateDefaultValueType(object? defaultValue, Type destinationType)
49-
{
50-
if (defaultValue == null)
51-
{
52-
// nullable value types can hold null value.
53-
if (destinationType.IsValueType && !(destinationType.IsGenericType && destinationType.GetGenericTypeDefinition() == typeof(Nullable<>)))
54-
{
55-
throw new ArgumentException(SR.Argument_ConstantNull);
56-
}
57-
}
58-
else
59-
{
60-
Type sourceType = defaultValue.GetType();
61-
// We should allow setting a constant value on a ByRef parameter
62-
if (destinationType.IsByRef)
63-
{
64-
destinationType = destinationType.GetElementType()!;
65-
}
66-
67-
// Convert nullable types to their underlying type.
68-
destinationType = Nullable.GetUnderlyingType(destinationType) ?? destinationType;
69-
70-
if (destinationType.IsEnum)
71-
{
72-
Type underlyingType;
73-
if (destinationType is EnumBuilderImpl enumBldr)
74-
{
75-
underlyingType = enumBldr.GetEnumUnderlyingType();
76-
77-
if (sourceType != enumBldr._typeBuilder.UnderlyingSystemType &&
78-
sourceType != underlyingType &&
79-
// If the source type is an enum, should not throw when the underlying types match
80-
sourceType.IsEnum &&
81-
sourceType.GetEnumUnderlyingType() != underlyingType)
82-
{
83-
throw new ArgumentException(SR.Argument_ConstantDoesntMatch);
84-
}
85-
}
86-
else if (destinationType is TypeBuilderImpl typeBldr)
87-
{
88-
underlyingType = typeBldr.UnderlyingSystemType;
89-
90-
if (underlyingType == null || (sourceType != typeBldr.UnderlyingSystemType && sourceType != underlyingType))
91-
{
92-
throw new ArgumentException(SR.Argument_ConstantDoesntMatch);
93-
}
94-
}
95-
else
96-
{
97-
underlyingType = Enum.GetUnderlyingType(destinationType);
98-
99-
if (sourceType != destinationType && sourceType != underlyingType)
100-
{
101-
throw new ArgumentException(SR.Argument_ConstantDoesntMatch);
102-
}
103-
}
104-
}
105-
else
106-
{
107-
if (!destinationType.IsAssignableFrom(sourceType))
108-
{
109-
throw new ArgumentException(SR.Argument_ConstantDoesntMatch);
110-
}
111-
}
112-
}
113-
}
114-
11547
internal void SetData(byte[] data)
11648
{
11749
_rvaData = data;

src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ParameterBuilderImpl.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public ParameterBuilderImpl(MethodBuilderImpl methodBuilder, int sequence, Param
3232

3333
public override void SetConstant(object? defaultValue)
3434
{
35-
Type parameterType = _position == 0 ? _methodBuilder.ReturnType : _methodBuilder.ParameterTypes![_position - 1];
36-
FieldBuilderImpl.ValidateDefaultValueType(defaultValue, parameterType);
3735
_defaultValue = defaultValue;
3836
_attributes |= ParameterAttributes.HasDefault;
3937
}

src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/PropertyBuilderImpl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ protected override void AddOtherMethodCore(MethodBuilder mdBuilder)
5757
protected override void SetConstantCore(object? defaultValue)
5858
{
5959
_containingType.ThrowIfCreated();
60-
FieldBuilderImpl.ValidateDefaultValueType(defaultValue, _propertyType);
6160
_defaultValue = defaultValue;
6261
}
6362

src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/TypeBuilderImpl.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -616,23 +616,22 @@ public override Type GetGenericTypeDefinition()
616616
public override string? Namespace => _namespace;
617617
public override Assembly Assembly => _module.Assembly;
618618
public override Module Module => _module;
619-
public override Type UnderlyingSystemType
619+
public override Type UnderlyingSystemType => this;
620+
621+
public override Type GetEnumUnderlyingType()
620622
{
621-
get
623+
if (IsEnum)
622624
{
623-
if (IsEnum)
625+
if (_enumUnderlyingType == null)
624626
{
625-
if (_enumUnderlyingType == null)
626-
{
627-
throw new InvalidOperationException(SR.InvalidOperation_NoUnderlyingTypeOnEnum);
628-
}
629-
630-
return _enumUnderlyingType;
631-
}
632-
else
633-
{
634-
return this;
627+
throw new InvalidOperationException(SR.InvalidOperation_NoUnderlyingTypeOnEnum);
635628
}
629+
630+
return _enumUnderlyingType;
631+
}
632+
else
633+
{
634+
throw new ArgumentException(SR.Argument_MustBeEnum);
636635
}
637636
}
638637
public override bool IsSZArray => false;

src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveEnumBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static IEnumerable<object[]> DefineLiteral_TestData()
3737
yield return new object[] { typeof(uint), (uint)1 };
3838

3939
yield return new object[] { typeof(int), 0 };
40-
yield return new object[] { typeof(int), 1 };
40+
yield return new object[] { typeof(int), Test.Second };
4141

4242
yield return new object[] { typeof(ulong), (ulong)0 };
4343
yield return new object[] { typeof(ulong), (ulong)1 };

src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySavePropertyBuilderTests.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,24 @@ public void SetConstantVariousValues(Type returnType, object defaultValue)
163163
Assert.Equal(defaultValue, property.GetConstantValue());
164164
}
165165

166+
[Theory]
167+
[MemberData(nameof(SetConstant_TestData))]
168+
public void SetConstantVariousValuesMlcCoreAssembly(Type returnType, object defaultValue)
169+
{
170+
using (MetadataLoadContext mlc = new MetadataLoadContext(new CoreMetadataAssemblyResolver()))
171+
{
172+
PersistedAssemblyBuilder ab = new PersistedAssemblyBuilder(new AssemblyName("MyDynamicAssembly"), mlc.CoreAssembly);
173+
ModuleBuilder mb = ab.DefineDynamicModule("My Module");
174+
Type returnTypeFromCore = returnType != typeof(PropertyBuilderTest11.Colors) ? mlc.CoreAssembly.GetType(returnType.FullName, true) : returnType;
175+
TypeBuilder type = mb.DefineType("MyType", TypeAttributes.Public);
176+
177+
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, returnTypeFromCore, null);
178+
property.SetConstant(defaultValue);
179+
180+
Assert.Equal(defaultValue, property.GetConstantValue());
181+
}
182+
}
183+
166184
[Fact]
167185
public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructorInfo_ThrowsArgumentNullException()
168186
{
@@ -194,7 +212,6 @@ public void Set_WhenTypeAlreadyCreated_ThrowsInvalidOperationException()
194212
MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
195213
MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), null);
196214
method.GetILGenerator().Emit(OpCodes.Ret);
197-
AssertExtensions.Throws<ArgumentException>(() => property.SetConstant((decimal)10));
198215
CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor([typeof(int)]), [10]);
199216
type.CreateType();
200217

@@ -204,18 +221,5 @@ public void Set_WhenTypeAlreadyCreated_ThrowsInvalidOperationException()
204221
Assert.Throws<InvalidOperationException>(() => property.SetConstant(1));
205222
Assert.Throws<InvalidOperationException>(() => property.SetCustomAttribute(customAttrBuilder));
206223
}
207-
208-
[Fact]
209-
public void SetConstant_ValidationThrows()
210-
{
211-
AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type);
212-
FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
213-
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null);
214-
215-
AssertExtensions.Throws<ArgumentException>(() => property.SetConstant((decimal)10));
216-
AssertExtensions.Throws<ArgumentException>(() => property.SetConstant(null));
217-
type.CreateType();
218-
Assert.Throws<InvalidOperationException>(() => property.SetConstant(1));
219-
}
220224
}
221225
}

0 commit comments

Comments
 (0)