c# - Calling unsafe method using expression trees -


i need call unsafe method takes raw pointers.

for need construct expression represents pointer value represented variableexpression or parameterexpression.

how that?

my usual approach expression stuff c# compiler build expression me, wonderful lambda-parsing ability, inspect makes in debugger. however, scenario describe, run problem straight away:

new project, set 'allow unsafe' on.

method takes raw pointers:

class myclass {     public unsafe int mymethod(int* p)     {         return 0;     } } 

code builds expression:

class program {     unsafe static void main(string[] args)     {         var mi = typeof (myclass).getmethods().first(m => m.name == "mymethod");          int q = 5;          expression<func<myclass, int, int>> expr = (c, i) => c.mymethod(&i);      } } 

my intent run , see expr looked in debugger; however, when compiled got

error cs1944: expression tree may not contain unsafe pointer operation

reviewing the docs error, looks "need construct expression represents pointer value" can never satisfied:

an expression tree may not contain unsafe pointer operation

expression trees not support pointer types because expression<tdelegate>.compile method allowed produce verifiable code. see comments. [there not appear comments!]

to correct error

  • do not use pointer types when trying create expression tree.

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -