Skip to content

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

@jdluzen

Description

@jdluzen

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions