c# 4.0 - How to Create Method and Property with Expression Tree Dynamically? -


this code works know whether solution? solution using expression tree considered better emit , opcodes?

var target = expression.lambda(             expression.block(                 new parameterexpression[] { },                 expression.call(                     typeof(messagebox).getmethod("show", new[] { typeof(string) }),                    expression.constant(1.tostring(), typeof(string))                 )             ),             new parameterexpression[] { }          ); assemblyname aname = new assemblyname("dynamicassemblyexample");  assemblybuilder ab =             appdomain.currentdomain.definedynamicassembly(                 aname,                 assemblybuilderaccess.runandsave);          modulebuilder mb =             ab.definedynamicmodule(aname.name, aname.name + ".dll"); typebuilder tb = mb.definetype("mydynamictype", typeattributes.public);         var method = tb.definemethod("dynamicmethod", methodattributes.public     |         methodattributes.static); target.compiletomethod(method); 

the code in example seems "example" code. don't see requiring expression trees. if read correctly merely creating constant expression () => messagebox.show(1.tostring());.)

expression trees great, among others, when actual dynamic code needs created i.e. when interpreting other language within program or operations otherwise require reflection (e.g. in programs such automapper.)

are better emitting op-codes directly. depends: can better job compiler? emitting op-codes writing assembly, time consuming , need know doing able write optimal code. performance difference between compiler , hand optimized code worth effort?


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -