Return/consume dynamic anonymous type across assembly boundaries

后端 未结 5 779
北海茫月
北海茫月 2020-11-29 08:15

The code below works great. If the Get and Use methods are in different assemblies, the code fails with a RuntimeBinderException. This is because t

5条回答
  •  北海茫月
    2020-11-29 08:51

    Here's a poor man's workaround; Newtonsoft.Json to the rescue, as serialization roundtrip generates dynamic type instances visible to your/working assembly.

    public static class TypeExt
    {
      // roundtrip json serialization to enable access to dynamic members and properties originating from another assembly
      public static T JClone( this T source ) { return JsonConvert.DeserializeObject( JsonConvert.SerializeObject( source ) ); }
    }
    

提交回复
热议问题