Using DynamicObject when multi-targeting in NetStandard

132 views Asked by At

I have the following implementation of DynamicObject:

public sealed class DynamicDictionary : DynamicObject
{
    private readonly Dictionary<string, string> _dictionary = new Dictionary<string, string>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        string tmpResult;
        if (!_dictionary.TryGetValue(binder.Name, out tmpResult))
        {
            result = null;
            return true;
        }

        result = tmpResult;
        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _dictionary[binder.Name] = value.ToString();
        return true;
    }
}

When this is built in a project targeting the full framework e.g. net451 or in a netstandard1.6 project, the following code runs with no problem:

dynamic dic = new DynamicDictionary();
dic.Foo = "bar";    
string bar = dic.Foo;

However when I try to build this in a multi-target netstandard project (supporting both Net451 and netstandard1.6) using the following project.json:

{
  "frameworks": {
    "net451": {      
    },
    "netstandard1.6": {
      "dependencies": {
        "NETStandard.Library": "1.6.1",        
        "System.Dynamic.Runtime": "4.3.0",
        "Microsoft.CSharp": "4.3.0"
      },
      "buildOptions": {
        "define": [ "NET_STANDARD" ]
      }
    }
  },
  "buildOptions": {
    "warningsAsErrors": true
  }
}

The project compiles with no error or warning and produces two dlls, one for each framework (net451 and netstandard1.6).

Referencing the netstandard1.6 version in a Console app running on netcoreapp1.1 executes the sample code above with no problems however when I reference the net451 dll on a Console app built against net451 executing the sample code throws: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll

As far as I am aware this exception is only thrown when either of TryGetMember() or TrySetMember() methods in the DynamicDictionary class return false indicating the value could not be retrieved or set on the underlying dictionary.

Putting a break point on either of these methods revealed that they are never invoked even though I can see the value of "bar" in the underlying dictionary.

I suspect some dll have not been referenced correctly when building the multi-target project against net451.

Any ideas?

0

There are 0 answers