private DisassembledIlClass CreateIlClassFromRawIlClass(RawILClass rawIlClass) { // Get the raw IL. var rawIL = rawIlClass.ILBuilder.ToString(); // Get the first and second line. string firstLine = null; string secondLine = null; using (var reader = new StringReader(rawIL)) { firstLine = reader.ReadLine(); if (firstLine != null) { secondLine = reader.ReadLine(); } } DisassembledIlClass ilClass = new DisassembledClass(); // We can immediately identify structures and interfaces. if (ILParseHelper.IsLineStructDeclaration(firstLine)) { ilClass = new DisassembledStructure(); } else if (ILParseHelper.IsLineInterfaceDeclaration(firstLine)) { ilClass = new DisassembledInterface(); } else { // Now check the second line. if (secondLine == null) { ilClass = new DisassembledClass(); } else { string baseType; if (!ILParseHelper.ReadExtendsLine(secondLine, out baseType)) { ilClass = new DisassembledClass(); } if (baseType == @"[mscorlib]System.Enum") { ilClass = new DisassembledEnumeration(); } else if (baseType == @"[mscorlib]System.MulticastDelegate") { ilClass = new DisassembledDelegate(); } } } // Set the IL. ilClass.RawIL = rawIL; ilClass.InitialiseFromIL(); // Add any children. foreach (var rawChild in rawIlClass.Children) { // Create the entity type from the entity IL. var childIlClass = CreateIlClassFromRawIlClass(rawChild); ilClass.AddChild(childIlClass); } return(ilClass); }
private List <RawILClass> ParseDisassembledIlClasses() { // We'll return a list of raw IL classes. var rootRawILClasses = new List <RawILClass>(); // Now start parsing the raw IL, line by line. using (var reader = new StringReader(RawIL)) { // IL classes can be nested, so when parsing them we must maintain // a stack (so we can nest too). var rawIlClassStack = new Stack <RawILClass>(); // Read each line string line; while ((line = reader.ReadLine()) != null) { if (line.Contains("<GetVisualChildren>")) { Console.WriteLine(); } // Is it a class start token? If so, we can start a new builder. if (ILParseHelper.IsLineAnyLevelIlClassDeclaration(line)) { string fullName, templateSpecification; ILParseHelper.GetClassNameFromClassDeclarationLine(line, out fullName, out templateSpecification); // Create a new raw IL class, parse its name, add it to the list if it's top level, add it to the stack. var rawIlClass = new RawILClass { FullName = fullName, TemplateSpecification = templateSpecification }; if (!rawIlClassStack.Any()) { rootRawILClasses.Add(rawIlClass); } else { rawIlClassStack.Peek().Children.Add(rawIlClass); } rawIlClassStack.Push(rawIlClass); } // If don't have a current class, skip. if (!rawIlClassStack.Any()) { continue; } // Add the line to the class builder. rawIlClassStack.Peek().ILBuilder.AppendLine(line); // Is it a class end token? If so, we've finished the class. if (ILParseHelper.IsLineClassEndDeclaration(line, rawIlClassStack.Peek().FullName)) { // Pop the stack. rawIlClassStack.Pop(); } } } // Return the set of class builders. return(rootRawILClasses); }