Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of Emit.OpCodes in .NET for Windows Store apps API?

I am considering porting a third-party library to .NET for Windows Store apps. The library makes excessive use of System.Reflection.Emit.OpCodes via calls to the ILGenerator.Emit method overloads.

In the .NET for Windows Store Apps API, the OpCode structure and OpCodes class are included, but there is no ILGenerator class, and as far as I have been able to find out no replacement either.

I am obviously missing something, but: without the ILGenerator class, what is the purpose of including System.Reflection.Emit.OpCode and OpCodes in the .NET for Windows Store apps API?

like image 827
Anders Gustafsson Avatar asked Feb 20 '13 06:02

Anders Gustafsson


2 Answers

Good question. While I cannot say for sure, there are only two reasons I can think of that use OpCode / Opcodes (and I cannot really imagine it's used for something else):

  1. Assemble a piece of code using ILGenerator
  2. Disassemble a piece of code using f.ex. MethodInfo.GetMethodBody and then parsing the bytes containing the IL code

Since it's not the former, I guess they're using the latter. The application for this is static code analysis; I myself use it f.ex. to implement some Mocking behavior and figuring out what the lambda expression was that was passed to it (in this case Expressions couldn't be used). Another applications of code analysis that might be more likely in this scenario is checking if certain classes/methods/constructs are used which are disallowed (I can imagine that they don't want you to use certain functionality).

like image 181
atlaste Avatar answered Oct 16 '22 00:10

atlaste


You may find Mono Cecil useful to replace some of the functions of System.Reflection, officially there is no port of it for WinRT, but unofficially, there is a port available here, just note that is based in the Silverlight version of Mono Cecil.

Also be aware that you can't modify dlls and exes outside your app package, this is a security measure of the WinRT sandbox.

like image 30
Rafael Avatar answered Oct 15 '22 23:10

Rafael