Trouble with Reflection.Emit, emitting bad MSIL please help.

A

allfyre

Ok, to make a long story short, I'm Emitting some MSIL from a c#
application. I know that THIS is the MSIL I want to be emitting:

//**************************************************************
..method public hidebysig instance class
[System.Data]System.Data.SqlClient.SqlCommand
Format1(class Delta.DictionaryDispatch dispatch) cil managed
{
.custom instance void Delta.DispatchNameAttribute::.ctor(string) = (
01 00 0B 43 4F 4D 4D 41 4E 44 5F 4F 4E 45 00 00 ) // ...COMMAND_ONE..
// Code size 27 (0x1b)
.maxstack 4
.locals init ([0] class [System.Data]System.Data.SqlClient.SqlCommand
rtn,
[1] class [System.Data]System.Data.SqlClient.SqlCommand
CS$1$0000)
IL_0000: nop
IL_0001: newobj instance void
[System.Data]System.Data.SqlClient.SqlCommand::.ctor()
IL_0006: stloc.0
IL_0007: ldarg.0
IL_0008: ldloc.0
IL_0009: ldarg.1
IL_000a: ldstr "COMMAND_ONE"
IL_000f: callvirt instance void class
Delta.DispatchFormatter`2<class
[System.Data]System.Data.SqlClient.SqlCommand,class
Delta.DictionaryDispatch>::ApplyDispatchBinding(!0,


class Delta.DictionaryDispatch,


string)
IL_0014: nop
IL_0015: ldloc.0
IL_0016: stloc.1
IL_0017: br.s IL_0019
IL_0019: ldloc.1
IL_001a: ret
} // end of method TestFormatter::Format1
//**************************************************************

But THIS is the closest I can get using Reflection.Emit:
//**************************************************************
..method public instance class
[System.Data]System.Data.SqlClient.SqlCommand
Format1(class [Delta]Delta.DictionaryDispatch A_1) cil managed
{
.custom instance void
[Delta]Delta.DispatchNameAttribute::.ctor(string) = ( 01 00 0B 63 6F 6D
6D 61 6E 64 20 6F 6E 65 00 00 ) // ...command one..
// Code size 27 (0x1b)
.maxstack 5
.locals init (class [System.Data]System.Data.SqlClient.SqlCommand
V_0)
IL_0000: nop
IL_0001: newobj instance void
[System.Data]System.Data.SqlClient.SqlCommand::.ctor()
IL_0006: stloc.0
IL_0007: ldarg.0
IL_0008: ldloc.0
IL_0009: ldarg.1
IL_000a: ldstr "COMMAND_ONE"
IL_000f: callvirt instance void
[Delta]Delta.SqlDispatchFormatter::ApplyDispatchBinding(class
[System.Data]System.Data.SqlClient.SqlCommand,

class [Delta]Delta.DictionaryDispatch,

string)
IL_0014: nop
IL_0015: ldloc.0
IL_0016: stloc.1
IL_0017: br.s IL_0019
IL_0019: ldloc.1
IL_001a: ret
} // end of method Formatter::Format1
//**************************************************************

Which throws an "InvalidProgramException" when I try to call it. Can
anybody help me here? I'm starting to lose sleep over this.
 
B

Barry Kelly

allfyre said:
Ok, to make a long story short, I'm Emitting some MSIL from a c#
application. I know that THIS is the MSIL I want to be emitting:
.locals init ([0] class [System.Data]System.Data.SqlClient.SqlCommand
rtn,
[1] class [System.Data]System.Data.SqlClient.SqlCommand
CS$1$0000)

There are two locals in this method.
But THIS is the closest I can get using Reflection.Emit:
.locals init (class [System.Data]System.Data.SqlClient.SqlCommand
V_0)

There is only one local in this method.
IL_0015: ldloc.0
IL_0016: stloc.1

And here you assign to a second local, which isn't defined. Is there an
appropriate LocalBuilder for it?

It's hard to say more without more information - I'm afraid you'd have
to try and reduce your code to a complete, compiling sample, showing the
Reflection.Emit calls you're using.

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top