Permalink
Browse files

initial commit of files

  • Loading branch information...
royosherove committed Jun 25, 2011
0 parents commit ff6a6cc3031ed04e492faf1746fb2727b173dc32
Showing with 2,500 additions and 0 deletions.
  1. BIN .DS_Store
  2. BIN Bin/DmILReader.dll
  3. BIN Bin/DmILReader.pdb
  4. BIN Bin/DynamicMethodVisualizer.dll
  5. BIN Bin/DynamicMethodVisualizer.pdb
  6. BIN DMVisualizer.GIF
  7. BIN MethodInfoVisualizer.GIF
  8. BIN Src/.DS_Store
  9. BIN Src/DynamicMethodVisualizer/.DS_Store
  10. BIN Src/DynamicMethodVisualizer/DmILReader.dll
  11. BIN Src/DynamicMethodVisualizer/DynamicMethodVisualizer.dll
  12. +204 −0 Src/DynamicMethodVisualizer/ILReader/Base/ILReaderBase.cs
  13. +66 −0 Src/DynamicMethodVisualizer/ILReader/Base/ILReaderFactory.cs
  14. +61 −0 Src/DynamicMethodVisualizer/ILReader/Base/TokenResolverBase.cs
  15. +71 −0 Src/DynamicMethodVisualizer/ILReader/DynamicILReader.cs
  16. +70 −0 Src/DynamicMethodVisualizer/ILReader/ILReaderLib.csproj
  17. +101 −0 Src/DynamicMethodVisualizer/ILReader/Instructions/FormatProvider.cs
  18. +347 −0 Src/DynamicMethodVisualizer/ILReader/Instructions/ILInstruction.cs
  19. +20 −0 Src/DynamicMethodVisualizer/ILReader/Interfaces/IILFormatProvider.cs
  20. +12 −0 Src/DynamicMethodVisualizer/ILReader/Interfaces/IILReader.cs
  21. +18 −0 Src/DynamicMethodVisualizer/ILReader/Interfaces/ITokenResolver.cs
  22. +35 −0 Src/DynamicMethodVisualizer/ILReader/Properties/AssemblyInfo.cs
  23. +107 −0 Src/DynamicMethodVisualizer/ILReader/Resolvers/DynamicScopeTokenResolver.cs
  24. +118 −0 Src/DynamicMethodVisualizer/ILReader/Resolvers/StaticScopeTokenResolver.cs
  25. +39 −0 Src/DynamicMethodVisualizer/ILReader/StaticILReader.cs
  26. +98 −0 Src/DynamicMethodVisualizer/MethodVisualizer/DynamicMethodVisualizer.csproj
  27. +38 −0 Src/DynamicMethodVisualizer/MethodVisualizer/MethodBodyInfo.cs
  28. +38 −0 Src/DynamicMethodVisualizer/MethodVisualizer/MethodBodyObjectSource.cs
  29. +181 −0 Src/DynamicMethodVisualizer/MethodVisualizer/MethodBodyViewer.Designer.cs
  30. +126 −0 Src/DynamicMethodVisualizer/MethodVisualizer/MethodBodyViewer.cs
  31. +167 −0 Src/DynamicMethodVisualizer/MethodVisualizer/MethodBodyViewer.resx
  32. +48 −0 Src/DynamicMethodVisualizer/MethodVisualizer/MethodBodyVisualizer.cs
  33. +35 −0 Src/DynamicMethodVisualizer/MethodVisualizer/Properties/AssemblyInfo.cs
  34. +98 −0 Src/DynamicMethodVisualizer/MethodVisualizer/Properties/Settings.Designer.cs
  35. +24 −0 Src/DynamicMethodVisualizer/MethodVisualizer/Properties/Settings.settings
  36. +28 −0 Src/DynamicMethodVisualizer/MethodVisualizer/Settings.cs
  37. +30 −0 Src/DynamicMethodVisualizer/MethodVisualizer/app.config
  38. BIN Src/DynamicMethodVisualizer/MethodVisualizer/bin/Debug/DmILReader.dll
  39. BIN Src/DynamicMethodVisualizer/MethodVisualizer/bin/Debug/DmILReader.pdb
  40. BIN Src/DynamicMethodVisualizer/MethodVisualizer/bin/Debug/DynamicMethodVisualizer.dll
  41. +30 −0 Src/DynamicMethodVisualizer/MethodVisualizer/bin/Debug/DynamicMethodVisualizer.dll.config
  42. BIN Src/DynamicMethodVisualizer/MethodVisualizer/bin/Debug/DynamicMethodVisualizer.pdb
  43. BIN Src/DynamicMethodVisualizer/MethodVisualizer/dyn.ico
  44. BIN ...micMethodVisualizer/MethodVisualizer/obj/Debug/DynamicMethodVisualizer.MethodBodyViewer.resources
  45. BIN ...MethodVisualizer/MethodVisualizer/obj/Debug/DynamicMethodVisualizer.csproj.GenerateResource.Cache
  46. BIN Src/DynamicMethodVisualizer/MethodVisualizer/obj/Debug/DynamicMethodVisualizer.dll
  47. BIN Src/DynamicMethodVisualizer/MethodVisualizer/obj/Debug/DynamicMethodVisualizer.pdb
  48. BIN Src/DynamicMethodVisualizer/MethodVisualizer/obj/Debug/Refactor/DynamicMethodVisualizer.dll
  49. BIN Src/DynamicMethodVisualizer/MethodVisualizer/obj/Debug/ResolveAssemblyReference.cache
  50. +10 −0 Src/DynamicMethodVisualizer/MethodVisualizer/obj/DynamicMethodVisualizer.csproj.FileList.txt
  51. +3 −0 Src/DynamicMethodVisualizer/README.txt
  52. +32 −0 Src/DynamicMethodVisualizer/TestVisualizer.sln
  53. BIN Src/DynamicMethodVisualizer/TestVisualizer.suo
  54. +33 −0 Src/DynamicMethodVisualizer/TestVisualizer/Properties/AssemblyInfo.cs
  55. +129 −0 Src/DynamicMethodVisualizer/TestVisualizer/Test.cs
  56. +63 −0 Src/DynamicMethodVisualizer/TestVisualizer/TestVisualizer.csproj
  57. +20 −0 readme.txt
BIN .DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,204 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using ReflectionDemos.ILParser;
+using ReflectionDemos.ILParser.Resolvers;
+using System.Reflection.Emit;
+using System.Reflection;
+using ClrTest.Reflection;
+
+namespace ILReader
+{
+ public abstract class ILReaderBase:IILReader
+ {
+
+ public static string GetMethodAsString(MethodBase method)
+ {
+ StringBuilder stb = new StringBuilder();
+ IILReader reader = ILReaderFactory.GetReader(method);
+ foreach (ILInstruction ili in reader)
+ {
+ stb.AppendLine(ili.ToShortString());
+ }
+
+ return stb.ToString();
+ }
+
+ private Byte[] m_byteArray;
+ Int32 m_position;
+ ITokenResolver m_resolver;
+
+ static OpCode[] s_OneByteOpCodes = new OpCode[0x100];
+ static OpCode[] s_TwoByteOpCodes = new OpCode[0x100];
+
+ static ILReaderBase()
+ {
+ foreach (FieldInfo fi in typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static))
+ {
+ OpCode opCode = (OpCode)fi.GetValue(null);
+ UInt16 value = (UInt16)opCode.Value;
+ if (value < 0x100)
+ {
+ s_OneByteOpCodes[value] = opCode;
+ }
+ else if ((value & 0xff00) == 0xfe00)
+ {
+ s_TwoByteOpCodes[value & 0xff] = opCode;
+ }
+ }
+ }
+
+
+ protected object m_target = null;
+ public ILReaderBase(object target)
+ {
+ m_target = target;
+ this.m_resolver = getTokenResolver(target);
+ this.m_byteArray = getMethodBodyAsByteArray(target);
+
+ if (this.m_byteArray == null) this.m_byteArray = new byte[0];
+ this.m_position = 0;
+ }
+
+ protected abstract Byte[] getMethodBodyAsByteArray(object target);
+ protected abstract ITokenResolver getTokenResolver(object target);
+
+
+ public IEnumerator<ILInstruction> GetEnumerator()
+ {
+ while (m_position < m_byteArray.Length)
+ yield return Next();
+
+ m_position = 0;
+ yield break;
+ }
+
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); }
+
+ ILInstruction Next()
+ {
+ Int32 offset = m_position;
+ OpCode opCode = OpCodes.Nop;
+ Int32 token = 0;
+
+ // read first 1 or 2 bytes as opCode
+ Byte code = ReadByte();
+ if (code != 0xFE)
+ {
+ opCode = s_OneByteOpCodes[code];
+ }
+ else
+ {
+ code = ReadByte();
+ opCode = s_TwoByteOpCodes[code];
+ }
+
+ switch (opCode.OperandType)
+ {
+ case OperandType.InlineNone:
+ return new InlineNoneInstruction(m_resolver, offset, opCode);
+
+ //The operand is an 8-bit integer branch target.
+ case OperandType.ShortInlineBrTarget:
+ SByte shortDelta = ReadSByte();
+ return new ShortInlineBrTargetInstruction(m_resolver, offset, opCode, shortDelta);
+
+ //The operand is a 32-bit integer branch target.
+ case OperandType.InlineBrTarget:
+ Int32 delta = ReadInt32();
+ return new InlineBrTargetInstruction(m_resolver, offset, opCode, delta);
+
+ //The operand is an 8-bit integer: 001F ldc.i4.s, FE12 unaligned.
+ case OperandType.ShortInlineI:
+ Byte int8 = ReadByte();
+ return new ShortInlineIInstruction(m_resolver, offset, opCode, int8);
+
+ //The operand is a 32-bit integer.
+ case OperandType.InlineI:
+ Int32 int32 = ReadInt32();
+ return new InlineIInstruction(m_resolver, offset, opCode, int32);
+
+ //The operand is a 64-bit integer.
+ case OperandType.InlineI8:
+ Int64 int64 = ReadInt64();
+ return new InlineI8Instruction(m_resolver, offset, opCode, int64);
+
+ //The operand is a 32-bit IEEE floating point number.
+ case OperandType.ShortInlineR:
+ Single float32 = ReadSingle();
+ return new ShortInlineRInstruction(m_resolver, offset, opCode, float32);
+
+ //The operand is a 64-bit IEEE floating point number.
+ case OperandType.InlineR:
+ Double float64 = ReadDouble();
+ return new InlineRInstruction(m_resolver, offset, opCode, float64);
+
+ //The operand is an 8-bit integer containing the ordinal of a local variable or an argument
+ case OperandType.ShortInlineVar:
+ Byte index8 = ReadByte();
+ return new ShortInlineVarInstruction(m_resolver, offset, opCode, index8);
+
+ //The operand is 16-bit integer containing the ordinal of a local variable or an argument.
+ case OperandType.InlineVar:
+ UInt16 index16 = ReadUInt16();
+ return new InlineVarInstruction(m_resolver, offset, opCode, index16);
+
+ //The operand is a 32-bit metadata string token.
+ case OperandType.InlineString:
+ token = ReadInt32();
+ return new InlineStringInstruction(m_resolver, offset, opCode, token);
+
+ //The operand is a 32-bit metadata signature token.
+ case OperandType.InlineSig:
+ token = ReadInt32();
+ return new InlineSigInstruction(m_resolver, offset, opCode, token);
+
+ //The operand is a 32-bit metadata token.
+ case OperandType.InlineMethod:
+ token = ReadInt32();
+ return new InlineMethodInstruction(m_resolver, offset, opCode, token);
+
+ //The operand is a 32-bit metadata token.
+ case OperandType.InlineField:
+ token = ReadInt32();
+ return new InlineFieldInstruction(m_resolver, offset, opCode, token);
+
+ //The operand is a 32-bit metadata token.
+ case OperandType.InlineType:
+ token = ReadInt32();
+ return new InlineTypeInstruction(m_resolver, offset, opCode, token);
+
+ //The operand is a FieldRef, MethodRef, or TypeRef token.
+ case OperandType.InlineTok:
+ token = ReadInt32();
+ return new InlineTokInstruction(m_resolver, offset, opCode, token);
+
+ //The operand is the 32-bit integer argument to a switch instruction.
+ case OperandType.InlineSwitch:
+ Int32 cases = ReadInt32();
+ Int32[] deltas = new Int32[cases];
+ for (Int32 i = 0; i < cases; i++)
+ {
+ deltas[i] = ReadInt32();
+ }
+ return new InlineSwitchInstruction(m_resolver, offset, opCode, deltas);
+
+ default:
+ throw new BadImageFormatException("unexpected OperandType " + opCode.OperandType);
+ }
+ }
+
+ Byte ReadByte() { return (Byte)m_byteArray[m_position++]; }
+ SByte ReadSByte() { return (SByte)ReadByte(); }
+
+ UInt16 ReadUInt16() { m_position += 2; return BitConverter.ToUInt16(m_byteArray, m_position - 2); }
+ UInt32 ReadUInt32() { m_position += 4; return BitConverter.ToUInt32(m_byteArray, m_position - 4); }
+ UInt64 ReadUInt64() { m_position += 8; return BitConverter.ToUInt64(m_byteArray, m_position - 8); }
+
+ Int32 ReadInt32() { m_position += 4; return BitConverter.ToInt32(m_byteArray, m_position - 4); }
+ Int64 ReadInt64() { m_position += 8; return BitConverter.ToInt64(m_byteArray, m_position - 8); }
+
+ Single ReadSingle() { m_position += 4; return BitConverter.ToSingle(m_byteArray, m_position - 4); }
+ Double ReadDouble() { m_position += 8; return BitConverter.ToDouble(m_byteArray, m_position - 8); }
+ }
+}
@@ -0,0 +1,66 @@
+//By Roy Osherove
+//www.ISerializable.com
+//Use freely! (Roy@Osherove.com)
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Reflection;
+using ClrTest.Reflection;
+using System.Reflection.Emit;
+using System.Runtime.CompilerServices;
+
+namespace ReflectionDemos.ILParser
+{
+ public static class ILReaderFactory
+ {
+ static Type s_executionScope;
+ static FieldInfo s_lamdaField, s_lamdaMethodField;
+
+ static ILReaderFactory()
+ {
+ s_executionScope = typeof(ExecutionScope);
+ s_lamdaField = s_executionScope.GetField("Lambda", BindingFlags.Instance | BindingFlags.NonPublic);
+ s_lamdaMethodField = s_lamdaField.FieldType.GetField("Method", BindingFlags.Instance | BindingFlags.NonPublic);
+ }
+
+ public static IILReader GetReader(object target)
+ {
+ MethodBase method = target as MethodBase;
+ if (method == null)
+ {
+ if (target is MulticastDelegate)
+ {
+ // if this is an ExecutionScope then we probably have a lamda expression.
+ ExecutionScope scope = (((Delegate)target).Target as ExecutionScope);
+ if (scope == null)
+ return GetReader(method);
+
+ // if we get this far we DO have a lamda expression.
+ object lamdaValue = s_lamdaField.GetValue(scope);
+ if (lamdaValue == null)
+ return GetReader(method);
+
+ method = (s_lamdaMethodField.GetValue(lamdaValue) as MethodBase);
+ }
+ }
+ return GetReader(method);
+ }
+
+ public static IILReader GetReader(MethodBase method)
+ {
+ if (method ==null)
+ {
+ return null;
+ }
+
+ if (method is DynamicMethod)
+ {
+ return new DynamicILReader(method as DynamicMethod);
+ }
+ else
+ {
+ return new StaticILReader(method);
+ }
+ }
+ }
+}
@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using ReflectionDemos.ILParser.Resolvers;
+using System.Reflection;
+using System.Diagnostics;
+
+namespace ILReader.Base
+{
+ /// <summary>
+ /// Base class for Token Resolvers.
+ /// You still need to implement everything except for the 'AsMember' method
+ /// which simply calls the appropriate Type Method or Field getters based onthe member type.
+ /// </summary>
+ public abstract class TokenResolverBase:ITokenResolver
+ {
+ public abstract MethodBase AsMethod(int token);
+
+ public abstract FieldInfo AsField(int token);
+
+ public abstract Type AsType(int token);
+
+ public abstract string AsString(int token);
+
+ public abstract byte[] AsSignature(int token);
+
+ public MemberInfo AsMember(int token)
+ {
+ if (isTypeMemberToken(token))
+ return this.AsType(token);
+
+ if (isMethodMemberToken(token))
+ return this.AsMethod(token);
+
+ if (isFieldMemberToken(token))
+ return this.AsField(token);
+
+ Debug.Assert(false, string.Format("unexpected token type: {0:x8}", token));
+ return null;
+ }
+
+ protected static bool isFieldMemberToken(int token)
+ {
+ return (token & 0x04000000) == 0x04000000;
+ }
+
+ protected static bool isMethodMemberToken(int token)
+ {
+ return (token & 0x06000000) == 0x06000000;
+ }
+
+ protected static bool isTypeMemberToken(int token)
+ {
+ return (token & 0x02000000) == 0x02000000;
+ }
+
+
+
+
+ }
+}
Oops, something went wrong.

0 comments on commit ff6a6cc

Please sign in to comment.