JsonSerializationException 'Unable to find a constructor' on Xamarin.Android

后端 未结 2 1776
别那么骄傲
别那么骄傲 2020-12-10 13:32

I have this very odd problem with my code, and it\'s a pretty new problem, considering I didn\'t have it half a year ago. Long story short, I\'ve made an app in Xamarin, and

相关标签:
2条回答
  • 2020-12-10 14:26

    The Preserve attribute is a more focused way to ensure a member is not removed by the linker if you still prefer it to generally do it's thing with your code.

    Example:

    [Preserve]
    [JsonConstructor]
    private AlertRequest(bool fake_arg)
    {
        // fake_arg is to have a unique ctor that we exclusively
        // use in JSON de-serialization via JsonConstructor attribute.
        // Preserve attribute ensures Xamarin linker does not remove,
        // as there are no direct uses of this ctor in the code base
    }
    
    0 讨论(0)
  • 2020-12-10 14:27

    In the 'Android options' tab of the project properties, there is a 'linker' tab. Is the selected option in the 'Linking' dropdown "Sdk Assemblies only" or is it "Sdk and user assemblies"?

    If it is the latter, the parameterless constructor is skipped when linking, because no use is detected. So change it to "Sdk Assemblies only".

    0 讨论(0)
提交回复
热议问题