Repository navigation
Closed
Labels
area-System.Reflection.Emitbugsize-reductionIssues impacting final app size primary for size sensitive workloads
Milestone
Description
When TypeBuilder encounters by-ref field it throws COMException. This seems to be the only reference to COMException and the behaviour is not consistent with other error handling for different type members.
runtime/src/libraries/System.Reflection.Emit/tests/TypeBuilder/TypeBuilderDefineField.cs
Lines 107 to 113 in cfb1335
| public void DefineField_ByRefFieldType_ThrowsCOMExceptionOnCreation() | |
| { | |
| TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); | |
| type.DefineField("Name", typeof(int).MakeByRefType(), FieldAttributes.Public); | |
| Assert.Throws<COMException>(() => type.CreateTypeInfo()); | |
| } |
It should be replaced with ArgumentException as it's done for all other type members. Here is an example with events
runtime/src/libraries/System.Reflection.Emit/tests/TypeBuilder/TypeBuilderDefineEvent.cs
Lines 109 to 115 in cfb1335
| [Fact] | |
| public void DefineEvent_ByRefEventType_ThrowsArgumentException() | |
| { | |
| TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); | |
| AssertExtensions.Throws<ArgumentException>(null, () => type.DefineEvent("Name", EventAttributes.None, typeof(int).MakeByRefType())); | |
| } |
Metadata
Metadata
Assignees
Labels
area-System.Reflection.Emitbugsize-reductionIssues impacting final app size primary for size sensitive workloads
Type
Projects
- StatusShow more project fieldsDone
Activity
AaronRobinsonMSFT commentedon Oct 14, 2020
The error here is
VLDTR_E_SIG_BYREFINFIELD(HRESULT0x801312e4) and is originating in a deep call stack.At the point of the throw, the exception is of type
HRException, however it is converted to anEEMessageExceptionwhich is classified askCOMException. This exception then gets propagated out to managed as aCOMException. I do not know the best way to fix this because I am sure it is going to be a breaking change for someone. Whether that breaking changes matters to anyone is a good question.@davidwrighton or @jkotas Any opinions or suggestions on how to change this exception type? I guess we could catch prior to the QCall exist and convert to a better managed type.
jkotas commentedon Oct 14, 2020
We have plans to enabled byref fields in .NET 6. If that happens, there will be no exception thrown for this repro that will take of this specific problem.
As for the more general problem of mapping HRESULTs to right exception types inside CoreCLR, I am not sure the best way to fix it either.
marek-safar commentedon Oct 14, 2020
Why not add suggested ArgumentException check to DefineField (that's what it'd done for everything else) ?
jkotas commentedon Oct 14, 2020
The check for ByRefs in GetTypeTokenWorkerNoLock is not right. I think we should remove it. It is too aggresive. For example, it will incorrectly fire for:
that is valid IL.
Reflection.Emit should not really be in the business of validating the emitted IL.
steveharter commentedon Nov 13, 2020
This issue can be re-purposed somewhat then to fix or remove the byref check and add tests.
krwq commentedon Jul 20, 2021
moving to 7.0 since this is non-essential for 6.0