The following code throws an exception. TryConvert is not being called for the cast to interface. Why is this? Can I work around the problem?
using System.Dynamic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
dynamic test = new JsonNull();
var ok = (string)test;
// Next line throws:
// Unable to cast object of type 'ConsoleApplication1.JsonNull' to type 'ConsoleApplication1.IFoo'.
var fail = (IFoo)test;
}
}
class JsonNull : DynamicObject
{
public override bool TryConvert(ConvertBinder binder, out object result)
{
result = null;
return !binder.Type.IsValueType;
}
}
interface IFoo { }
}