ekonbenefits / impromptu-interface

Static interface to dynamic implementation (duck casting). Uses the DLR combined with Reflect.Emit.
Apache License 2.0
655 stars 67 forks source link

RuntimeBinderException trying to set a property after Activator.CreateInstance #37

Open jdluzen opened 3 years ago

jdluzen commented 3 years ago

Hello and thanks for this great project!

I am currently using this to generate a class from an interface at runtime:

var theInterface = Impromptu.ActLike<IImage>(new { });
var iimage = (IImage)Activator.CreateInstance(theInterface.GetType());
iimage.Id = "hi";

Unfortunately, I get an Exception on the 3rd line: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot perform runtime binding on a null reference' Stack trace on Win10 x64 Core3.1:

   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at ActLike_IImage_ae9204a78c7144d991df6b0084d4e2da.set_Id(String value)
   ...

Stack trace on Blazor Chrome:

at (wrapper dynamic-method) System.Object.CallSite.Target(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,string)
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet] (System.Runtime.CompilerServices.CallSite site, T0 arg0, T1 arg1) <0x2fbe2e8 + 0x00248> in <filename unknown>:0 
at ActLike_IImage_d38621b4b95445c38e41839a8b0a07cc.set_Id (System.String value) <0x2fbadd0 + 0x00098> in <filename unknown>:0
...

Is there a certain way I should be doing that?

LuvForAirplanes commented 3 years ago

@jdluzen I just noticed you said Blazor Chrome. Could you expand on that? Are you working with Blazor Client-Side?? I would dearly like to get this project working on WASM, but I can't seem to.