Bug 22226 - Xamarin Android return Microsoft.CSharp.RuntimeBinder.RuntimeBinderException when adding a property to a dynamic class
Summary: Xamarin Android return Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ...
Status: RESOLVED FEATURE
Alias: None
Product: Class Libraries
Classification: Mono
Component: General ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-08-20 09:38 UTC by e.vanseters@esperantoxl.nl
Modified: 2014-08-21 05:15 UTC (History)
3 users (show)

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

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 GitHub or Developer Community 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 FEATURE

Description e.vanseters@esperantoxl.nl 2014-08-20 09:38:03 UTC
I have created a plugin system which used the dynamic keyword to create an object which should be converted to rest. 

Creating the object works like this: 

dynamic filledIn = new ExpandoObject();
filledIn.A = "string";
filledIn.B = DateTime.Now;

In my plugin I want to check if my dynamic already has a property (List) by the name X.
I pass the filledInObject to the method via the ref keyword. 

public void DoSomething ( ref dynamic objectToUpdate ) {

  if (objectToUpdate.X == null) {
    // Create our dynamic Object, which can be converted into REST. 
    objectToUpdate.X = new System.Dynamic.ExpandoObject ();			
  }
}

This will result in the following exception;  

ex	{Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: `System.Dynamic.ExpandoObject' does not contain a definition for `X' at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object) <IL 0x00068, 0x00200> at System.Dynamic.UpdateDelegates.UpdateAndExecute1<object, object> (System.Runtime.CompilerServices.CallSite,object) <0x0098f> 

I tested the same code via some unit tests and there I get no errors. So it seems like a bug in Mono.
Comment 1 Marek Safar 2014-08-20 10:34:57 UTC
The error is correct. You cannot check for property existence this way. One of the ways how to d o it http://stackoverflow.com/questions/2839598/how-to-detect-if-a-property-exists-on-an-expandoobject
Comment 2 e.vanseters@esperantoxl.nl 2014-08-20 15:26:56 UTC
Mmm thanks Marek. 

I also noticed another difference. When I change the following code :

dynamic filledIn = new ExpandoObject();
filledIn.A = "string";
filledIn.B = DateTime.Now;

to: 

dynamic filledIn = new JObject(); // A NewtonSoft JSON object
filledIn.A = "string";
filledIn.B = DateTime.Now;

It also generated the same compiler error in Xamarin Studio while running it in an Android app. However, in Visual Studio my Unit Tests are accepting this syntax and my tests are running without any problem.
Comment 3 Marek Safar 2014-08-21 05:15:20 UTC
This works for me using Json.NET 6.0.4