[release/9.0] Fix Enum field type bug found when underlying type is set from assembly loaded with MLC #106513
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport of #106375 to release/9.0
/cc @buyaa-n
Customer Impact
This is found while fixing a customer reported bug. When MLC used for setting the core assembly and a type loaded from that assembly used as underlying type when creating EnumBuilder, defining a field for that EnumBuilder would create a field with wrong type (The field type will be generated with underlying type instead of the EnumBuilder type). Plus this PR removes the validation that causing issue for setting constants for such field.
Root cause:
The
EnumBuilder.UnderlyingSystemType
, currently returnsGetEnumUnderlyingType()
which returns_underlyingField.FieldType
underneath. Because Type.Equals checksUnderlyingSystemType
for equality when theEnumBuilder
underlying type is set from MLC Core assembly it is causing issue for enum fields defined, for example:In this case the
field
type should beTestEnum
, but it is evaluated to be equal toSystem.Int32
when writing the field signature on Save. We should not return underlying field type forEnumBuilder.UnderlyingSystemType
, it should return the Enum itself instead (runtime enums returns the enum type itself). Though theEnumBuilder.GetEnumUnderlyingType()
method should keep returning the underlying field type. Related to #105903Regression
It is a bug in a new PersistedAssemblyBuilder added in .NET 9
Testing
A unit test added that reproes the issue
Risk
Low, the fix is straight forward and clean, will not cause a regression