Bug 44706 - Unable to Console.Write() any value with a dynamic object in it's ancestry.
Summary: Unable to Console.Write() any value with a dynamic object in it's ancestry.
Status: CONFIRMED
Alias: None
Product: Workbooks & Inspector
Classification: Xamarin
Component: Actually Unsure ()
Version: 0.9.0
Hardware: PC Windows
: --- normal
Target Milestone: Future
Assignee: xamarininteractive
URL:
Depends on:
Blocks:
 
Reported: 2016-09-23 21:15 UTC by Matthew B Hykes
Modified: 2016-10-20 21:55 UTC (History)
3 users (show)

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


Attachments
Example Workbook showing that dynamic objects mess with Console.Write (951 bytes, application/x-xamarin-workbook)
2016-09-23 21:15 UTC, Matthew B Hykes
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 for Bug 44706 on Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.
Related Links:
Status:
CONFIRMED

Description Matthew B Hykes 2016-09-23 21:15:19 UTC
Created attachment 17722 [details]
Example Workbook showing that dynamic objects mess with Console.Write

I was trying to write some code using dynamic objects, specifically ExpandoObject, when I discovered this bug.
It took me quite a while to work out that the issue was not in my own code (which was working perfectly with the dynamic expandos), but rather with the Console.Write and Console.WriteLine functions.

Any time that a variable which is or has been appended to a dynamic object is given to Console.Write, it throws a NullReferenceException.  This problem doesn't occur with simple var variables, only dynamic ones.  Appending the dynamic object to a string doesn't stop the NullReferenceException, and neither does calling .ToString on it.  Only casting it to a string or assigning it to a string variable allows the variable to work with Console.Write.

Of course, there should be no NullReferenceException.

I created a minimal workbook which demonstrates the issue, which I have attached.  The name of the file reflects the fact that I thought this was an expando problem when I created the workbook.
 
I have pasted the text version of the workbook of below:
-----------
---
uti: com.xamarin.workbook
platform: WPF
packages: []
---

```csharp
#r "./Microsoft.CSharp.dll"
using System.Dynamic;

/* NOTE: Dynamics won't work at all (compiler error) unless the Microsoft.CSharp.dll is referenced.
 * In this case I put it in the same folder as this file and referenced it from there.
 */

dynamic foo = "hello";
var bar = " world";
var baz = foo+bar;


```

Shows:

"hello world"

```csharp
Console.Write(foo);
```

Shows:

NullReferenceException: Object reference not set to an instance of an object.\
...

```csharp
Console.Write(bar);
```

Shows:

world

```csharp
Console.Write(baz);
```

Shows:

NullReferenceException: Object reference not set to an instance of an object.\
...

```csharp
Console.Write(baz.ToString());
```

Shows:

NullReferenceException: Object reference not set to an instance of an object.\
...

```csharp
Console.Write((string)baz);
```

Shows:

hello world
Comment 1 Arpit Jha 2016-09-26 11:33:27 UTC
I have checked this issue and I am able to reproduce this issue with the help of bug description.

Steps I have followed to reproduce this issue.
1. Copy  Microsoft.CSharp.dll from here  C:\Windows\WinSxS\msil_microsoft.csharp_b03f5f7f11d50a3a_4.0.9600.16384_none_052fcdde5b42fc67 and paste to a new folder.

2. Download attached workbook file and paste to the same directory.

3. Execute the wpf workbook from top to bottom as mentioned in bug description.

Screencast : 
Screencast : http://www.screencast.com/t/ppgTRhTNpKrO
Workbooks Logs : https://gist.github.com/Arpit360/08298f3c106942e33ee30e7683ec7fac
Comment 2 Arpit Jha 2016-09-26 11:35:22 UTC
An update to Comment 1:

3. Load the workbook file in to Xamarin Workbooks by double click

4. Execute the wpf workbook from top to bottom as mentioned in bug description.
Comment 3 Matthew B Hykes 2016-09-26 14:28:14 UTC
Thank you for the confirmation of reproducibility, Arpit Jha.

I should mention for anyone who finds this bug report before the bug is fixed that the workaround is to cast your variable to string while calling Console.Write or Console.WriteLine.

'''
Console.Write((string)dynamicVariable);
'''

All the actual code using dynamics seems to work perfectly, you just have to use this work around in order to confirm it using the Console.