472,342 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Issue emitting an Int16 value in a DynamicMethod...

For some reason I keep getting an InvalidProgramException when trying to emit a short (Int16) value in a DynamicMethod.

The following code should replicate my problem.
private delegate object TestHandler();

private static void Main()
{
DynamicMethod method = new DynamicMethod("temp", typeof(object), null, typeof(Program));
ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldc_I4, (short)1000);
il.Emit(OpCodes.Box, typeof(short));
il.Emit(OpCodes.Ret);
TestHandler handler = (TestHandler)method.CreateDelegate(typeof(TestHand ler));
Console.WriteLine(handler());
}
Jul 11 '06 #1
2 1089
"Nathan Baulch" <na***********@gmail.comwrote:
For some reason I keep getting an InvalidProgramException when
trying to emit a short (Int16) value in a DynamicMethod.
il.Emit(OpCodes.Ldc_I4, (short)1000);
Ldc_I4 takes a 4-byte integer as its argument, but you are handing it a
2-byte integer. That's why you get an invalid program exception.

The runtime stack on the CLI doesn't know about shorts. Check out
partition I, 12.3.2.1 in the CLI spec (Ecma 335 3rd ed) for more
information about the types the runtime stack actually supports.

Check out III, 3.40 for the ldc instruction. You'll see there that you
should use:

il.Emit(OpCodes.Ldc_I4, 1000);
il.Emit(OpCodes.Conv_I2);

-- Barry

--
http://barrkel.blogspot.com/
Jul 11 '06 #2
Check out III, 3.40 for the ldc instruction. You'll see there that you
should use:

il.Emit(OpCodes.Ldc_I4, 1000);
il.Emit(OpCodes.Conv_I2);
Excellent, that worked just nicely!
For the benefit of the newsgroup, I also had a similar issue with ulong
(UInt64) values which I solved in a similar way:

il.Emit(OpCodes.Ldc_I8, (long)val);
il.Emit(OpCodes.Conv_U8);
Jul 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric Newton | last post by:
VB's more declarative nature of handling events is golden. I'm hoping C# will acquire this type of deal, in addition to the anonymous delegates. ...
1
by: Arjen | last post by:
Hi, Inside my database I need to add a short value. Inside my code I use int (int32), to convert the int to int16... is this the good/correct...
10
by: Vinay | last post by:
Hi, Is there a limit on maximum number of columns in listview control ? I am finding: In a listview control (Report Format) I am displaying a...
3
by: QuocSi | last post by:
Hi all, I have a bitsfield Structure MyBits a as boolean b as boolean c as boolean d as boolean end structure
4
by: hharry | last post by:
hello all, i have 2 datatables and am trying to transfer rows from datatable a to datatable b i use the datatable.importrow method. the...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried...
2
by: jobs | last post by:
What's messed up here is that i have close to identical code that is working perfectly, only difference is that code uses sqldatasource control...
1
by: Phoenix | last post by:
Hi Friends, Any help would be highly appreciated for the following problem : I have a vb 6 application which makes call to an API in some dll...
1
by: gleave | last post by:
Hi, below is the code i got to print a datagridview, the trouble is it also prints visible columns which i don't want to be printed. Does anyone have...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.