none
IL Manipulation : What's OpCode ?

    Question

  • Hi
    I'm working with Mono.Cecil for IL Manipulation and find some articls like this, but i didn't found any useful article about what's OpCodes and explain each OpCode item.

    my main problem is that, in target app class, i have a property which have getter accesor, i want to replace it with return another value of that class.
    Here is my original class :

    public class Form1 : Form
    {
    	private bool _item1;
    	private bool _item2;
    
    	public bool Item
    	{
    		get
    		{
    			if(someCondition)
    			{
    				return _item1;
    			}
    			return _item2;
    		}
    	}
    }
    and i want to change above class to this :

    public class Form1 : Form
    {
    	private bool _item1;
    	private bool _item2;
    
    	public bool Item
    	{
    		get
    		{
    			return _item2;
    		}
    	}
    }


    can anybody help me ?
    thanks

    this is my Signature
    Tuesday, July 28, 2009 10:15 PM

Answers

  • OpCode is the operation code that identifies an MSIL instruction.

    See MS Partition III.pdf here: http://msdn.microsoft.com/en-us/netframework/aa569283.aspx
    Tuesday, July 28, 2009 10:26 PM
  • An OpCode is basically one operation.  For example, "Add" or "Multiply" are single instructions represented by OpCode values.  Other OpCodes are things like "Call" (call a method) or "CallVirt" (call a method virtually), etc.  Learning some basic assembly language might help here - you're basically working with the intermediate language instructions generated by the C# or VB.NET compiler.

    Any time you want to create types at runtime, you can use Reflection.Emit to create them.  Mono.Cecil is better if you want to modify an existing assembly.  For learning how to create dynamic types, I'd search for Reflection.Emit, and potentially look at this article as well.
    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, July 29, 2009 11:14 PM
    Moderator

  • Learning some basic assembly language might help here

    Would u plz give me a useful artcicle to demonstrate this purpose ?

    Mono.Cecil is better if you want to modify an existing assembly

    You mean that System.Reflection.Emit is not appropriate for changing an existing assembly ? because Mono.cecil have not a comprehensive documentation, instead System.Reflection.Emit have some useful documentation and i think it's easier to find my solution than Mono.Cecil (of course, if it supports changing an existing assembly). does System.Reflection.Emit support this operation ?

    I'd read up here: http://en.wikipedia.org/wiki/Assembly_language

    It will help you understand op codes a bit better, too.

    As for Reflection.Emit - it's really oriented to making a new assembly, not changing an existing one.  Mono.Cecil would be a better option if you're trying to modify an existing assembly.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Hamed_1983 Thursday, July 30, 2009 9:59 PM
    Thursday, July 30, 2009 5:52 PM
    Moderator

All replies

  • OpCode is the operation code that identifies an MSIL instruction.

    See MS Partition III.pdf here: http://msdn.microsoft.com/en-us/netframework/aa569283.aspx
    Tuesday, July 28, 2009 10:26 PM
  • OpCode is the operation code that identifies an MSIL instruction.

    See MS Partition III.pdf here: http://msdn.microsoft.com/en-us/netframework/aa569283.aspx

    Many Thanks BinaryCoder
    It's very useful but have not any example and source code, would u plz give me another resource thta have some examples and source code ?
    Thanks
    this is my Signature
    Wednesday, July 29, 2009 4:59 PM
  • Just use Ildasm.exe on a sample C# program.  It shows you the opcodes.
    Hans Passant.
    Wednesday, July 29, 2009 5:02 PM
    Moderator
  • There are some good articles exploring Reflection.Emit, which do much to explain this.  For example, this article shows how you can construct a dynamic type entirely at runtime using the individual OpCodes required to build it on the fly.

    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, July 29, 2009 5:09 PM
    Moderator
  • There are some good articles exploring Reflection.Emit, which do much to explain this.  For example, this article shows how you can construct a dynamic type entirely at runtime using the individual OpCodes required to build it on the fly.

    Reed Copsey, Jr. - http://reedcopsey.com

    Many Thanks Reed
    It's very helpful, but still i have problem with What's OpCodes.
    My another question is, that link which u give me, teach creating Dynamic Assembly, can i use this NameSpace (System.Reflection.Emit) to manipulate assemblies (like mono.cecil)?
    can u provide me more examples and articles like that ?
    So Many thanks.
    this is my Signature
    Wednesday, July 29, 2009 10:55 PM
  • An OpCode is basically one operation.  For example, "Add" or "Multiply" are single instructions represented by OpCode values.  Other OpCodes are things like "Call" (call a method) or "CallVirt" (call a method virtually), etc.  Learning some basic assembly language might help here - you're basically working with the intermediate language instructions generated by the C# or VB.NET compiler.

    Any time you want to create types at runtime, you can use Reflection.Emit to create them.  Mono.Cecil is better if you want to modify an existing assembly.  For learning how to create dynamic types, I'd search for Reflection.Emit, and potentially look at this article as well.
    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, July 29, 2009 11:14 PM
    Moderator
  • An OpCode is basically one operation.  For example, "Add" or "Multiply" are single instructions represented by OpCode values.  Other OpCodes are things like "Call" (call a method) or "CallVirt" (call a method virtually), etc.  Learning some basic assembly language might help here - you're basically working with the intermediate language instructions generated by the C# or VB.NET compiler.

    Any time you want to create types at runtime, you can use Reflection.Emit to create them.  Mono.Cecil is better if you want to modify an existing assembly.  For learning how to create dynamic types, I'd search for Reflection.Emit, and potentially look at this article as well.
    Reed Copsey, Jr. - http://reedcopsey.com

    Thanks again Reed

    Learning some basic assembly language might help here

    Would u plz give me a useful artcicle to demonstrate this purpose ?

    Mono.Cecil is better if you want to modify an existing assembly

    You mean that System.Reflection.Emit is not appropriate for changing an existing assembly ? because Mono.cecil have not a comprehensive documentation, instead System.Reflection.Emit have some useful documentation and i think it's easier to find my solution than Mono.Cecil (of course, if it supports changing an existing assembly). does System.Reflection.Emit support this operation ?
    thanks
    this is my Signature
    Thursday, July 30, 2009 9:03 AM

  • Learning some basic assembly language might help here

    Would u plz give me a useful artcicle to demonstrate this purpose ?

    Mono.Cecil is better if you want to modify an existing assembly

    You mean that System.Reflection.Emit is not appropriate for changing an existing assembly ? because Mono.cecil have not a comprehensive documentation, instead System.Reflection.Emit have some useful documentation and i think it's easier to find my solution than Mono.Cecil (of course, if it supports changing an existing assembly). does System.Reflection.Emit support this operation ?

    I'd read up here: http://en.wikipedia.org/wiki/Assembly_language

    It will help you understand op codes a bit better, too.

    As for Reflection.Emit - it's really oriented to making a new assembly, not changing an existing one.  Mono.Cecil would be a better option if you're trying to modify an existing assembly.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Hamed_1983 Thursday, July 30, 2009 9:59 PM
    Thursday, July 30, 2009 5:52 PM
    Moderator