|
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection.Emit;
namespace AutoDiagrammer
{
public class ILInstruction
{
// Fields
private OpCode code;
private object operand;
private byte[] operandData;
private int offset;
// Properties
public OpCode Code
{
get { return code; }
set { code = value; }
}
public object Operand
{
get { return operand; }
set { operand = value; }
}
public byte[] OperandData
{
get { return operandData; }
set { operandData = value; }
}
public int Offset
{
get { return offset; }
set { offset = value; }
}
/// <summary>
/// Returns a friendly strign representation of this instruction
/// </summary>
/// <returns></returns>
public string GetCode()
{
string result = "";
result += GetExpandedOffset(offset) + " : " + code;
if (operand != null)
{
switch (code.OperandType)
{
case OperandType.InlineField:
System.Reflection.FieldInfo fOperand = ((System.Reflection.FieldInfo)operand);
result += " " + MethodBodyReader.ProcessSpecialTypes(fOperand.FieldType.ToString()) + " " +
MethodBodyReader.ProcessSpecialTypes(fOperand.ReflectedType.ToString()) +
"::" + fOperand.Name + "";
break;
case OperandType.InlineMethod:
try
{
System.Reflection.MethodInfo mOperand = (System.Reflection.MethodInfo)operand;
result += " ";
if (!mOperand.IsStatic) result += "instance ";
result += MethodBodyReader.ProcessSpecialTypes(mOperand.ReturnType.ToString()) +
" " + MethodBodyReader.ProcessSpecialTypes(mOperand.ReflectedType.ToString()) +
"::" + mOperand.Name + "()";
}
catch
{
try
{
System.Reflection.ConstructorInfo mOperand = (System.Reflection.ConstructorInfo)operand;
result += " ";
if (!mOperand.IsStatic) result += "instance ";
result += "void " +
MethodBodyReader.ProcessSpecialTypes(mOperand.ReflectedType.ToString()) +
"::" + mOperand.Name + "()";
}
catch
{
}
}
break;
case OperandType.ShortInlineBrTarget:
case OperandType.InlineBrTarget:
result += " " + GetExpandedOffset((int)operand);
break;
case OperandType.InlineType:
result += " " + MethodBodyReader.ProcessSpecialTypes(operand.ToString());
break;
case OperandType.InlineString:
if (operand.ToString() == "\r\n") result += " \"\\r\\n\"";
else result += " \"" + operand.ToString() + "\"";
break;
case OperandType.ShortInlineVar:
result += operand.ToString();
break;
case OperandType.InlineI:
case OperandType.InlineI8:
case OperandType.InlineR:
case OperandType.ShortInlineI:
case OperandType.ShortInlineR:
result += operand.ToString();
break;
case OperandType.InlineTok:
if (operand is Type)
result += ((Type)operand).FullName;
else
result += "not supported";
break;
default: result += "not supported"; break;
}
}
return result;
}
/// <summary>
/// Add enough zeros to a number as to be represented on 4 characters
/// </summary>
/// <param name="offset">
/// The number that must be represented on 4 characters
/// </param>
/// <returns>
/// </returns>
private string GetExpandedOffset(long offset)
{
string result = offset.ToString();
for (int i = 0; result.Length < 4; i++)
{
result = "0" + result;
}
return result;
}
public ILInstruction()
{
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog