-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DictionaryTKeyEnumTValueConverter does not play nicely with Xamarin iOS/Android and AOT in general #16922
Comments
cc @steveharter |
Yes the published sample may not work in all AOT scenarios, but does support all Enum types globally. The provided example above works only when closing the generic ahead of time with either applying the class Program
{
static void Main(string[] args)
{
POCO poco = JsonSerializer.Deserialize<POCO>(@"{""D"":{""A"" : ""hello""}}");
Debug.Assert(poco.D[MyEnum.A] == "hello");
// or
JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new DictionaryEnumConverter<MyEnum, string>());
POCO2 poco2 = JsonSerializer.Deserialize<POCO2>(@"{""D"":{""A"" : ""world""}}", options);
Debug.Assert(poco2.D[MyEnum.A] == "hello");
}
}
public enum MyEnum
{
A = 1,
B = 2
}
public class POCO
{
[JsonConverter(typeof(DictionaryEnumConverter<MyEnum, string>))]
public Dictionary<MyEnum, string> D { get; set; }
}
public class POCO2
{
public Dictionary<MyEnum, string> D { get; set; }
} Also note for 5.0, Enums and other number types can be used as keys for dictionaries, so no work-around is needed. |
@tdykstra we could remove this sample, since it is no longer needed in the 5.0 timeframe since we added support for any @softlion can you clarify whether Xamarin or other AOT platform actually breaks with that sample. I'm aware of a Xamarin issue in 3.0\3.01 where the serializer, due to usage of CreateGenericType, had issues but that usage was much more complicated than in this example. |
This issue has been automatically closed due to no response from the original author. Please feel free to reopen it if you have more information that can help us investigate the issue further. |
AOT does not like reflection and factories building objects from reflection, as it needs to detect all types used in the app from the static analysis, to be able to strip unused classes and methods, and to precompile used classes and methods to machine code for performance.
The example you provide DictionaryTKeyEnumTValueConverter fall typically in this case.
The way to go is to use the inner class as the converter. So make i made it public, without requiring the json options, as it is unavailable there.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: