Ejemplo n.º 1
0
		public string DecompileBaml(MemoryStream bamlCode, string containingAssemblyFile, ConnectMethodDecompiler connectMethodDecompiler, AssemblyResolver asmResolver)
		{
			this.asmResolver = asmResolver;
			AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
			
			bamlCode.Position = 0;
			TextWriter w = new StringWriter();
			
			Assembly assembly = Assembly.LoadFile(containingAssemblyFile);
			
			Baml2006Reader reader = new Baml2006Reader(bamlCode, new XamlReaderSettings() { ValuesMustBeString = true, LocalAssembly = assembly });
			var xamlDocument = Parse(reader);
			
			string bamlTypeName = xamlDocument.OfType<XamlObjectNode>().First().Type.UnderlyingType.FullName;
			
			var eventMappings = connectMethodDecompiler.DecompileEventMappings(bamlTypeName);

			foreach (var xamlNode in xamlDocument) {
				RemoveConnectionIds(xamlNode, eventMappings, reader.SchemaContext);
				AvoidContentProperties(xamlNode);
				MoveXKeyToFront(xamlNode);
			}
			
			XDocument doc = new XDocument();
			XamlXmlWriter writer = new XamlXmlWriter(doc.CreateWriter(), reader.SchemaContext, new XamlXmlWriterSettings { AssumeValidInput = true });
			foreach (var xamlNode in xamlDocument)
				xamlNode.WriteTo(writer);
			writer.Close();
			
			// Fix namespace references
			string suffixToRemove = ";assembly=" + assembly.GetName().Name;
			foreach (XAttribute attrib in doc.Root.Attributes()) {
				if (attrib.Name.Namespace == XNamespace.Xmlns) {
					if (attrib.Value.EndsWith(suffixToRemove, StringComparison.Ordinal)) {
						string newNamespace = attrib.Value.Substring(0, attrib.Value.Length - suffixToRemove.Length);
						ChangeXmlNamespace(doc, attrib.Value, newNamespace);
						attrib.Value = newNamespace;
					}
				}
			}

			return doc.ToString();
		}
Ejemplo n.º 2
0
        public string DecompileBaml(MemoryStream bamlCode, string containingAssemblyFile, ConnectMethodDecompiler connectMethodDecompiler, AssemblyResolver asmResolver)
        {
            this.asmResolver = asmResolver;
            AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;

            bamlCode.Position = 0;
            TextWriter w = new StringWriter();

            Assembly assembly = Assembly.LoadFile(containingAssemblyFile);

            Baml2006Reader reader = new Baml2006Reader(bamlCode, new XamlReaderSettings()
            {
                ValuesMustBeString = true, LocalAssembly = assembly
            });
            var xamlDocument = Parse(reader);

            string bamlTypeName = xamlDocument.OfType <XamlObjectNode>().First().Type.UnderlyingType.FullName;

            var eventMappings = connectMethodDecompiler.DecompileEventMappings(bamlTypeName);

            foreach (var xamlNode in xamlDocument)
            {
                RemoveConnectionIds(xamlNode, eventMappings, reader.SchemaContext);
                AvoidContentProperties(xamlNode);
                MoveXKeyToFront(xamlNode);
            }

            XDocument     doc    = new XDocument();
            XamlXmlWriter writer = new XamlXmlWriter(doc.CreateWriter(), reader.SchemaContext, new XamlXmlWriterSettings {
                AssumeValidInput = true
            });

            foreach (var xamlNode in xamlDocument)
            {
                xamlNode.WriteTo(writer);
            }
            writer.Close();

            // Fix namespace references
            string suffixToRemove = ";assembly=" + assembly.GetName().Name;

            foreach (XAttribute attrib in doc.Root.Attributes())
            {
                if (attrib.Name.Namespace == XNamespace.Xmlns)
                {
                    if (attrib.Value.EndsWith(suffixToRemove, StringComparison.Ordinal))
                    {
                        string newNamespace = attrib.Value.Substring(0, attrib.Value.Length - suffixToRemove.Length);
                        ChangeXmlNamespace(doc, attrib.Value, newNamespace);
                        attrib.Value = newNamespace;
                    }
                }
            }

            return(doc.ToString());
        }