You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An interesting one: consuming a DLL that exposes a type containing a static ImmutableArray<T> property. When Program.Main is async, you get a TypeLoadException at stateMachine.MoveNext(). When Program.Main is sync, the program simply never starts.
I've uploaded a minimum reproducible example. For preservation purposes, I've also attached a ZIP archive of the repository.
If you change the provided Test() method to directly print Struct.One (as below, and replace Console.WriteLine(Test()) to Test()), then both sync and async versions will run all the way until Test() (including printing "Start" to the console, even for the sync version).
Create a library that contains a type, MyType. That type should have a static property of type ImmutableArray<MyType>. Optionally, initialize it with a collection expression in a static constructor. Our original code does contain a collection expression in the static constructor, but, if I recall correctly, commenting out the static constructor does not solve the issue (so it's not an exception in .cctor).
Build the library and take the DLL. Manually reference the DLL in the consuming project and attempt to use the type. Notice that the type exists, and that the compiler does not complain. At runtime, observe a TypeLoadException.
Expected behavior
The program runs normally.
Actual behavior
A TypeLoadException at runtime once usage of the type is encountered.
Regression?
No response
Known Workarounds
Use a ReadOnlyCollection<MyType>. We were previously doing so with Array.AsReadOnly(new MyType[] {...}), but changing to use ImmutableArray<MyType> broke it.
Just ran into this one myself. I'd done some refactoring and replaced a MyStruct[] with an ImmutableArray<MyStruct>. I was able to reproduce it running unit tests. You also don't need to reference a library to reproduce this, just creating a brand new console app with the struct in causes the issue:
colejohnson66
changed the title
TypeLoadException when using a type containing a static ImmutableArray<T> property when Main is asyncTypeLoadException when using a struct containing a static ImmutableArray<T> field/auto-property
Jul 8, 2024
Description
An interesting one: consuming a DLL that exposes a type containing a static
ImmutableArray<T>
property. WhenProgram.Main
is async, you get aTypeLoadException
atstateMachine.MoveNext()
. WhenProgram.Main
is sync, the program simply never starts.I've uploaded a minimum reproducible example. For preservation purposes, I've also attached a ZIP archive of the repository.
If you change the provided
Test()
method to directly printStruct.One
(as below, and replaceConsole.WriteLine(Test())
toTest()
), then both sync and async versions will run all the way untilTest()
(including printing "Start" to the console, even for the sync version).Reproduction Steps
Create a library that contains a type,
MyType
. That type should have a static property of typeImmutableArray<MyType>
. Optionally, initialize it with a collection expression in a static constructor. Our original code does contain a collection expression in the static constructor, but, if I recall correctly, commenting out the static constructor does not solve the issue (so it's not an exception in.cctor
).Build the library and take the DLL. Manually reference the DLL in the consuming project and attempt to use the type. Notice that the type exists, and that the compiler does not complain. At runtime, observe a
TypeLoadException
.Expected behavior
The program runs normally.
Actual behavior
A
TypeLoadException
at runtime once usage of the type is encountered.Regression?
No response
Known Workarounds
Use a
ReadOnlyCollection<MyType>
. We were previously doing so withArray.AsReadOnly(new MyType[] {...})
, but changing to useImmutableArray<MyType>
broke it.Configuration
Other information
ImmutableArrayPropertyTypeLoadException-main.zip
The text was updated successfully, but these errors were encountered: