public void Raw_SampleMethod() { MethodInfo method = GetType().GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); var memoryStream = new MemoryStream(500); var streamWriter = new StreamWriter(memoryStream); var streamReader = new StreamReader(memoryStream); var v = new ReadableILStringVisitor(new RawILStringToTextWriter(streamWriter)); var reader = new ClrTest.Reflection.ILReader(method); reader.Accept(v); streamWriter.Flush(); memoryStream.Seek(0, SeekOrigin.Begin); string line = string.Empty; string[] values = new[] { "IL_0000: 00 |", "IL_0001: 1f | 19", "IL_0003: 0a |", "IL_0004: 1f | 23", "IL_0006: 0b |", "IL_0007: 06 |", "IL_0008: 07 |", "IL_0009: 58 |", "IL_000a: 0c |", "IL_000b: 2b | IL_000d", "IL_000d: 08 |", "IL_000e: 2a |" }; foreach (string value in values) { Assert.Contains(value, streamReader.ReadLine(), StringComparison.InvariantCulture); } }
public void Readable_SampleMethod() { MethodInfo method = GetType().GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); var memoryStream = new MemoryStream(500); var streamWriter = new StreamWriter(memoryStream); var streamReader = new StreamReader(memoryStream); var v = new ReadableILStringVisitor(new ReadableILStringToTextWriter(streamWriter)); var reader = new ClrTest.Reflection.ILReader(method); reader.Accept(v); streamWriter.Flush(); memoryStream.Seek(0, SeekOrigin.Begin); string line = string.Empty; string[] values = new[] { "IL_0000: nop", "IL_0001: ldc.i4.s 19", "IL_0003: stloc.0", "IL_0004: ldc.i4.s 23", "IL_0006: stloc.1", "IL_0007: ldloc.0", "IL_0008: ldloc.1", "IL_0009: add", "IL_000a: stloc.2", "IL_000b: br.s IL_000d", "IL_000d: ldloc.2", "IL_000e: ret" }; foreach (string value in values) { Assert.Contains(value, streamReader.ReadLine(), StringComparison.InvariantCulture); } }
/// <summary> /// Creates the specified method. /// </summary> /// <param name="method">The method.</param> /// <param name="offset">The offset.</param> /// <returns></returns> public static MethodBodyInfo Create(MethodBase method, int offset, IILStringCollector collector) { MethodBodyInfo mbi = new MethodBodyInfo { Identity = method.GetHashCode(), TypeName = method.GetType().Name, MethodToString = method.ToString() }; collector.Initialize(mbi, offset); ReadableILStringVisitor visitor = new ReadableILStringVisitor(collector, DefaultFormatProvider.Instance); ILReaderFactory.Create(method, offset).Accept(visitor); return(mbi); }
public static MethodBodyInfo Create(MethodBase method) { var mbi = new MethodBodyInfo { Identity = method.GetHashCode(), TypeName = method.GetType().Name, MethodToString = method.ToString() }; var visitor = new ReadableILStringVisitor( new MethodBodyInfoBuilder(mbi), DefaultFormatProvider.Instance); var reader = ILReaderFactory.Create(method); reader.Accept(visitor); return(mbi); }
public void Readable_ComplexMethod() { MethodInfo method = GetType().GetMethod("ComplexMethod", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); var memoryStream = new MemoryStream(500); var streamWriter = new StreamWriter(memoryStream); var streamReader = new StreamReader(memoryStream); var v = new ReadableILStringVisitor(new ReadableILStringToTextWriter(streamWriter)); var reader = new ClrTest.Reflection.ILReader(method); reader.Accept(v); streamWriter.Flush(); memoryStream.Seek(0, SeekOrigin.Begin); string line = string.Empty; string[] values = new[] { "IL_0000: nop", "IL_0001: ldstr \"Hello world\"", "IL_0006: call Void WriteLine(System.String)/System.Console", "IL_000b: nop", "IL_000c: ldarg.0", "IL_000d: call Int32 SampleMethod()/ILReader.Tests.Readfrom_ILReader", "IL_0012: pop", "IL_0013: ret" }; foreach (string value in values) { Assert.Contains(value, streamReader.ReadLine(), StringComparison.InvariantCulture); } //while ((line = streamReader.ReadLine()) != null) //{ // Console.WriteLine(line); //} }
protected void ToPrintableByteCodeIntern(Type type, StringBuilder sb) { if (type.BaseType != null && typeof(IEnhancedType).IsAssignableFrom(type.BaseType)) { // write parent classes first ToPrintableByteCodeIntern(type.BaseType, sb); sb.Append('\n'); } sb.Append(type.ToString()); StringWriter sw = new StringWriter(sb); ILInstructionVisitor visitor = new ReadableILStringVisitor(new ReadableILStringToTextWriter(sw)); if (type is TypeBuilder) { MethodInstance[] methods = BytecodeBehaviorState.State.GetAlreadyImplementedMethodsOnNewType(); foreach (MethodInstance method in methods) { sb.Append('\n'); PrintAnnotations(method.Method, sb); sb.Append(method.ToString()).Append('\n'); ILReader reader = new ILReader(method.Method); reader.Accept(visitor); } return; } { ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo[] methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); FieldInfo[] fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo field in fields) { sb.Append('\n'); PrintAnnotations(field, sb); sb.Append(field.ToString()).Append('\n'); } foreach (ConstructorInfo constructor in constructors) { sb.Append('\n'); PrintAnnotations(constructor, sb); sb.Append(constructor.ToString()).Append('\n'); ILReader reader = new ILReader(constructor); reader.Accept(visitor); } foreach (MethodInfo method in methods) { sb.Append('\n'); PrintAnnotations(method, sb); sb.Append(method.ToString()).Append('\n'); ILReader reader = new ILReader(method); reader.Accept(visitor); } } }