Bug 31176 - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException `Android.Text.Html' does not contain a definition for `FromHtml'
Summary: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException `Android.Text.Html' doe...
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 5.1
Hardware: Macintosh Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-06-16 18:15 UTC by Marc Obaldo
Modified: 2015-06-17 10:35 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Screenshot from Xamarin Insights (32.02 KB, image/png)
2015-06-16 18:16 UTC, Marc Obaldo
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Marc Obaldo 2015-06-16 18:15:18 UTC
This is my first time submitting a bug. I am getting a weird behavior where I occasionally get `Android.Text.Html' does not contain a definition for `FromHtml' during -RUNTIME-.

The code compiles fine, and it works on a few devices. Now, I can consistently replicate it on my Samsung Galaxy S6. If I deploy to the device using the Release configuration, this exception happens. If I deploy using Debug, it works fine.

The exception is:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException `Android.Text.Html' does not contain a definition for `FromHtml'

I am unable to retrieve a full stack trace because I cannot replicate the bug while in Debug mode and it only happens in Release.

Please see the attached screenshot from Xamarin Insights.
Comment 1 Marc Obaldo 2015-06-16 18:16:08 UTC
Created attachment 11632 [details]
Screenshot from Xamarin Insights
Comment 2 Jonathan Pryor 2015-06-17 09:46:39 UTC
The exception and stack trace imply that you're using `dynamic`.

`dynamic` is frequently a form of Reflection.

Reflection is "invisible" to the linker:

http://developer.xamarin.com/guides/android/advanced_topics/linking/#Control

I hope you can see where I'm going with this. :-)

In all likelihood, the Html.FromHtml() method is being linked away in release builds, and thus *does not exist*, which is why the C# RuntimeBinder can't find it (it doesn't exist) and throws the exception.

To fix the issue, you need to somehow ensure that Html.FromHtml() is preserved by the linker. For example, don't use `dynamic` to invoke the method, and instead use a runtime check + cast.
Comment 3 Marc Obaldo 2015-06-17 10:35:01 UTC
Thank you. This works.

The problem was I was using data from dynamic deserialized from JSON using JSON.NET.

dynamic obj = JsonConvert.DeserializeObject(metadata);
Html.FromHtml(obj["Message"].Value);

Doing the following worked:

dynamic obj = JsonConvert.DeserializeObject(metadata);
string message = obj["Message"].Value;
Html.FromHtml(message);