Browse Source

Merge branch 'master' of https://github.com/icsharpcode/ILSpy

pull/234/merge
Ed Harvey 14 years ago
parent
commit
83b565ac7d
  1. 57
      ILSpy.BamlDecompiler/BamlResourceEntryNode.cs
  2. 10
      ILSpy.BamlDecompiler/CecilType.cs
  3. 25
      ILSpy.BamlDecompiler/CecilTypeResolver.cs
  4. 22
      ILSpy.BamlDecompiler/Extensions.cs
  5. 2
      ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj
  6. 3
      ILSpy.BamlDecompiler/Properties/AssemblyInfo.cs
  7. 11
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/AppDomainTypeResolver.cs
  8. 6
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/DotNetType.cs
  9. 3
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/IType.cs
  10. 2
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/ITypeResolver.cs
  11. 36
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KnownInfo.cs
  12. 58
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs
  13. 34
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlSimpleProperty.cs
  14. 3
      ILSpy.BamlDecompiler/Tests/Cases/Simple.xaml
  15. 26
      ILSpy.BamlDecompiler/Tests/Cases/Simple.xaml.cs
  16. 1
      ILSpy.BamlDecompiler/Tests/Cases/SimpleDictionary.xaml
  17. 112
      ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj
  18. 31
      ILSpy.BamlDecompiler/Tests/Properties/AssemblyInfo.cs
  19. 73
      ILSpy.BamlDecompiler/Tests/TestRunner.cs
  20. 12
      ILSpy.sln
  21. 5
      ILSpy/LoadedAssembly.cs

57
ILSpy.BamlDecompiler/BamlResourceEntryNode.cs

@ -3,14 +3,17 @@ @@ -3,14 +3,17 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TextView;
using ICSharpCode.ILSpy.TreeNodes;
using Mono.Cecil;
using Ricciolo.StylesExplorer.MarkupReflection;
namespace ILSpy.BamlDecompiler
@ -45,17 +48,55 @@ namespace ILSpy.BamlDecompiler @@ -45,17 +48,55 @@ namespace ILSpy.BamlDecompiler
bool LoadBaml(AvalonEditTextOutput output)
{
var asm = this.Ancestors().OfType<AssemblyTreeNode>().FirstOrDefault().LoadedAssembly;
MemoryStream bamlStream = new MemoryStream();
Data.Position = 0;
Data.CopyTo(bamlStream);
bamlStream.Position = 0;
XDocument xamlDocument = LoadIntoDocument(asm.GetAssemblyResolver(), asm.AssemblyDefinition, Data);
output.Write(xamlDocument.ToString());
return true;
}
internal static XDocument LoadIntoDocument(IAssemblyResolver resolver, AssemblyDefinition asm, Stream stream)
{
XDocument xamlDocument;
using (XmlBamlReader reader = new XmlBamlReader(bamlStream, new CecilTypeResolver(asm)))
using (XmlBamlReader reader = new XmlBamlReader(stream, new CecilTypeResolver(resolver, asm)))
xamlDocument = XDocument.Load(reader);
ConvertToEmptyElements(xamlDocument.Root);
MoveNamespacesToRoot(xamlDocument);
return xamlDocument;
}
static void MoveNamespacesToRoot(XDocument xamlDocument)
{
var additionalXmlns = new List<XAttribute> {
new XAttribute("xmlns", XmlBamlReader.DefaultWPFNamespace),
new XAttribute(XName.Get("x", XNamespace.Xmlns.NamespaceName), XmlBamlReader.XWPFNamespace)
};
output.Write(xamlDocument.ToString());
return true;
foreach (var element in xamlDocument.Root.DescendantsAndSelf()) {
if (element.Name.NamespaceName != XmlBamlReader.DefaultWPFNamespace && !additionalXmlns.Any(ka => ka.Value == element.Name.NamespaceName)) {
string newPrefix = new string(element.Name.LocalName.Where(c => char.IsUpper(c)).ToArray()).ToLowerInvariant();
int current = additionalXmlns.Count(ka => ka.Name.Namespace == XNamespace.Xmlns && ka.Name.LocalName.TrimEnd(ch => char.IsNumber(ch)) == newPrefix);
if (current > 0)
newPrefix += (current + 1).ToString();
XName defaultXmlns = XName.Get(newPrefix, XNamespace.Xmlns.NamespaceName);
if (element.Name.NamespaceName != XmlBamlReader.DefaultWPFNamespace)
additionalXmlns.Add(new XAttribute(defaultXmlns, element.Name.NamespaceName));
}
}
foreach (var xmlns in additionalXmlns.Except(xamlDocument.Root.Attributes())) {
xamlDocument.Root.Add(xmlns);
}
}
static void ConvertToEmptyElements(XElement element)
{
foreach (var el in element.Elements()) {
if (!el.IsEmpty && !el.HasElements && el.Value == "") {
el.RemoveNodes();
continue;
}
ConvertToEmptyElements(el);
}
}
}
}

10
ILSpy.BamlDecompiler/CecilType.cs

@ -67,5 +67,15 @@ namespace ILSpy.BamlDecompiler @@ -67,5 +67,15 @@ namespace ILSpy.BamlDecompiler
{
return string.Format("[CecilType Type={0}]", type);
}
public IType BaseType {
get {
TypeDefinition td = type.BaseType.Resolve();
if (td == null)
throw new Exception("could not resolve '" + type.BaseType.FullName + "'!");
return new CecilType(td);
}
}
}
}

25
ILSpy.BamlDecompiler/CecilTypeResolver.cs

@ -14,11 +14,18 @@ namespace ILSpy.BamlDecompiler @@ -14,11 +14,18 @@ namespace ILSpy.BamlDecompiler
/// </summary>
public class CecilTypeResolver : ITypeResolver
{
LoadedAssembly assembly;
IAssemblyResolver resolver;
AssemblyDefinition thisAssembly;
public CecilTypeResolver(LoadedAssembly assembly)
public CecilTypeResolver(IAssemblyResolver resolver, AssemblyDefinition asm)
{
this.assembly = assembly;
this.resolver = resolver;
this.thisAssembly = asm;
}
public bool IsLocalAssembly(string name)
{
return name == this.thisAssembly.Name.Name;
}
public IType GetTypeByAssemblyQualifiedName(string name)
@ -31,12 +38,12 @@ namespace ILSpy.BamlDecompiler @@ -31,12 +38,12 @@ namespace ILSpy.BamlDecompiler
string fullName = name.Substring(0, comma);
string assemblyName = name.Substring(comma + 1).Trim();
var type = assembly.AssemblyDefinition.MainModule.GetType(fullName);
var type = thisAssembly.MainModule.GetType(fullName);
if (type == null) {
var otherAssembly = assembly.LookupReferencedAssembly(assemblyName);
var otherAssembly = resolver.Resolve(assemblyName);
if (otherAssembly == null)
throw new Exception("could not resolve '" + assemblyName + "'!");
type = otherAssembly.AssemblyDefinition.MainModule.GetType(fullName);
type = otherAssembly.MainModule.GetType(fullName);
}
return new CecilType(type);
@ -49,5 +56,11 @@ namespace ILSpy.BamlDecompiler @@ -49,5 +56,11 @@ namespace ILSpy.BamlDecompiler
return new CecilDependencyPropertyDescriptor(name, ((CecilType)ownerType).type);
}
public string RuntimeVersion {
get {
return thisAssembly.MainModule.Runtime.ToString();
}
}
}
}

22
ILSpy.BamlDecompiler/Extensions.cs

@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
// This code is distributed under the MS-PL (for details please see \doc\MS-PL.txt)
using System;
using System.Linq;
namespace ILSpy.BamlDecompiler
{
public static class Extensions
{
public static string TrimEnd(this string target, Func<char, bool> predicate)
{
if (target == null)
throw new ArgumentNullException("target");
while (predicate(target.LastOrDefault()))
target = target.Remove(target.Length - 1);
return target;
}
}
}

2
ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj

@ -77,6 +77,7 @@ @@ -77,6 +77,7 @@
<Compile Include="CecilDependencyPropertyDescriptor.cs" />
<Compile Include="CecilType.cs" />
<Compile Include="CecilTypeResolver.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\AppDomainTypeResolver.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\BamlAssembly.cs" />
@ -97,6 +98,7 @@ @@ -97,6 +98,7 @@
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlProperty.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlPropertyElement.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlReader.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlSimpleProperty.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlBamlText.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlNamespace.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\XmlPIMapping.cs" />

3
ILSpy.BamlDecompiler/Properties/AssemblyInfo.cs

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
#endregion
@ -22,6 +23,8 @@ using System.Runtime.InteropServices; @@ -22,6 +23,8 @@ using System.Runtime.InteropServices;
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible(false)]
[assembly: InternalsVisibleTo("ILSpy.BamlDecompiler.Tests")]
// The assembly version has following format :
//
// Major.Minor.Build.Revision

11
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/AppDomainTypeResolver.cs

@ -131,6 +131,17 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -131,6 +131,17 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
return null;
}
}
public bool IsLocalAssembly(string name)
{
return false;
}
public string RuntimeVersion {
get {
throw new NotImplementedException();
}
}
#endregion

6
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/DotNetType.cs

@ -42,6 +42,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -42,6 +42,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
if (_type == null) return false;
return this._type.Equals(((DotNetType)type).Type);
}
public IType BaseType {
get {
return new DotNetType(this._type.BaseType.AssemblyQualifiedName);
}
}
#endregion

3
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/IType.cs

@ -8,10 +8,11 @@ using System.Text; @@ -8,10 +8,11 @@ using System.Text;
namespace Ricciolo.StylesExplorer.MarkupReflection
{
/// <summary>
/// Interface rappresenting a DotNet type
/// Interface representing a DotNet type
/// </summary>
public interface IType
{
IType BaseType { get; }
string AssemblyQualifiedName { get; }
bool IsSubclassOf(IType type);
bool Equals(IType type);

2
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/ITypeResolver.cs

@ -9,6 +9,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -9,6 +9,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{
public interface ITypeResolver
{
string RuntimeVersion { get; }
bool IsLocalAssembly(string name);
IType GetTypeByAssemblyQualifiedName(string name);
IDependencyPropertyDescriptor GetDependencyPropertyDescriptor(string name, IType ownerType, IType targetType);
}

36
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KnownInfo.cs

@ -34,12 +34,16 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -34,12 +34,16 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
public KnownInfo(ITypeResolver resolver)
{
KnownAssemblyTable = new string[5];
KnownAssemblyTable[0] = "PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
KnownAssemblyTable[1] = "PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
KnownAssemblyTable[2] = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
KnownAssemblyTable[3] = "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
KnownAssemblyTable[4] = "WindowBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
switch (resolver.RuntimeVersion) {
case "Net_2_0":
LoadKnownAssemblies30();
break;
case "Net_4_0":
LoadKnownAssemblies40();
break;
default:
throw new NotSupportedException();
}
KnownTypeTable = new TypeDeclaration[760];
KnownTypeTable[0] = new TypeDeclaration(resolver, string.Empty, string.Empty, 0);
@ -1305,6 +1309,26 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1305,6 +1309,26 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
KnownResourceTable.Add(0xa9, new ResourceName("SystemParameters.WorkArea"));
}
void LoadKnownAssemblies30()
{
KnownAssemblyTable = new string[5];
KnownAssemblyTable[0] = "PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
KnownAssemblyTable[1] = "PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
KnownAssemblyTable[2] = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
KnownAssemblyTable[3] = "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
KnownAssemblyTable[4] = "WindowBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
}
void LoadKnownAssemblies40()
{
KnownAssemblyTable = new string[5];
KnownAssemblyTable[0] = "PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
KnownAssemblyTable[1] = "PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
KnownAssemblyTable[2] = "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
KnownAssemblyTable[3] = "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
KnownAssemblyTable[4] = "WindowBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
}
#endregion
public bool IsKnownType(string type)

58
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs

@ -8,6 +8,7 @@ using System.ComponentModel; @@ -8,6 +8,7 @@ using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
@ -54,6 +55,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -54,6 +55,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
private readonly TypeDeclaration XamlTypeDeclaration;
private readonly XmlNameTable _nameTable = new NameTable();
private IDictionary<string, string> _rootNamespaces;
public const string XWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml";
public const string DefaultWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
#endregion
@ -159,7 +163,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -159,7 +163,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
public override bool MoveToFirstAttribute()
{
intoAttribute = false;
if (nodes.Count > 0 && nodes.Peek() is XmlBamlProperty)
if (nodes.Count > 0 && (nodes.Peek() is XmlBamlProperty || nodes.Peek() is XmlBamlSimpleProperty))
{
_currentNode = nodes.Dequeue();
return true;
@ -178,7 +182,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -178,7 +182,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
public override bool MoveToNextAttribute()
{
intoAttribute = false;
if (nodes.Count > 0 && nodes.Peek() is XmlBamlProperty)
if (nodes.Count > 0 && (nodes.Peek() is XmlBamlProperty || nodes.Peek() is XmlBamlSimpleProperty))
{
_currentNode = nodes.Dequeue();
return true;
@ -196,7 +200,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -196,7 +200,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///
public override bool MoveToElement()
{
while (nodes.Peek() is XmlBamlProperty)
while (nodes.Peek() is XmlBamlProperty || nodes.Peek() is XmlBamlSimpleProperty)
{
nodes.Dequeue();
}
@ -284,13 +288,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -284,13 +288,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
else
currentType = (BamlRecordType)type;
if (currentType.ToString().EndsWith("End"))
Debug.Unindent();
Debug.WriteLine(currentType);
if (currentType.ToString().StartsWith("Start"))
Debug.Indent();
// Debug.WriteLine(currentType);
}
private bool SetNextNode()
@ -300,6 +298,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -300,6 +298,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
_currentNode = nodes.Dequeue();
if ((_currentNode is XmlBamlProperty)) continue;
if ((_currentNode is XmlBamlSimpleProperty)) continue;
if (this.NodeType == XmlNodeType.EndElement)
{
@ -451,7 +450,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -451,7 +450,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
break;
default:
throw new NotImplementedException("UnsupportedNode: " + currentType);
break;
}
}
@ -586,7 +584,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -586,7 +584,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
String localName = String.Empty;
XmlBamlNode node = this.CurrentNode;
if (node is XmlBamlProperty)
if (node is XmlBamlSimpleProperty) {
var simpleNode = (XmlBamlSimpleProperty)node;
localName = simpleNode.LocalName;
} else if (node is XmlBamlProperty)
{
PropertyDeclaration pd = ((XmlBamlProperty)node).PropertyDeclaration;
localName = FormatPropertyDeclaration(pd, false, true, true);
@ -1057,9 +1058,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1057,9 +1058,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
private void ReadPropertyComplexEnd()
{
if (!(elements.Peek() is XmlBamlPropertyElement))
throw new InvalidCastException();
XmlBamlPropertyElement propertyElement = (XmlBamlPropertyElement) elements.Peek();
CloseElement();
@ -1191,10 +1189,22 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1191,10 +1189,22 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
}
else
element = new XmlBamlElement();
// the type is defined in the local assembly, i.e., the main assembly
// and this is the root element
TypeDeclaration oldDeclaration = null;
if (_resolver.IsLocalAssembly(declaration.Assembly) && parentElement == null) {
oldDeclaration = declaration;
declaration = GetKnownTypeDeclarationByName(declaration.Type.BaseType.AssemblyQualifiedName);
}
element.TypeDeclaration = declaration;
elements.Push(element);
nodes.Enqueue(element);
if (oldDeclaration != null) {
nodes.Enqueue(new XmlBamlSimpleProperty(XWPFNamespace, "Class", string.Format("{0}.{1}", oldDeclaration.Namespace, oldDeclaration.Name)));
}
if (parentElement != null && complexPropertyOpened == 0)
{
@ -1572,6 +1582,16 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1572,6 +1582,16 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
return declaration;
}
TypeDeclaration GetKnownTypeDeclarationByName(string name)
{
foreach (var type in KnownInfo.KnownTypeTable) {
if (name == string.Format("{0}.{1}, {2}", type.Namespace, type.Name, type.Assembly))
return type;
}
throw new NotSupportedException();
}
internal string GetAssembly(short identifier)
{
@ -1602,7 +1622,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1602,7 +1622,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
TypeDeclaration declaration;
XmlBamlNode node = this.CurrentNode;
if (node is XmlBamlProperty)
if (node is XmlBamlSimpleProperty)
return ((XmlBamlSimpleProperty)node).NamespaceName;
else if (node is XmlBamlProperty)
{
declaration = ((XmlBamlProperty)node).PropertyDeclaration.DeclaringType;
TypeDeclaration elementDeclaration = this.readingElements.Peek().TypeDeclaration;
@ -1678,7 +1700,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1678,7 +1700,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
get
{
XmlBamlNode node = this.CurrentNode;
if (node is XmlBamlProperty)
if (node is XmlBamlSimpleProperty)
return ((XmlBamlSimpleProperty)node).Value;
else if (node is XmlBamlProperty)
return ((XmlBamlProperty)node).Value.ToString();
else if (node is XmlBamlText)
return ((XmlBamlText)node).Text;

34
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlSimpleProperty.cs

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
// This code is distributed under the MS-PL (for details please see \doc\MS-PL.txt)
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Ricciolo.StylesExplorer.MarkupReflection
{
class XmlBamlSimpleProperty : XmlBamlNode
{
public string NamespaceName { get; private set; }
public string LocalName { get; private set; }
public string Value { get; private set; }
public XmlBamlSimpleProperty(string namespaceName, string localName, string value)
{
if (string.IsNullOrWhiteSpace(namespaceName))
throw new ArgumentException("namespaceName");
if (string.IsNullOrWhiteSpace(localName))
throw new ArgumentException("localName");
if (value == null)
throw new ArgumentNullException("value");
this.NamespaceName = namespaceName;
this.LocalName = localName;
this.Value = value;
}
public override XmlNodeType NodeType {
get { return XmlNodeType.Attribute; }
}
}
}

3
ILSpy.BamlDecompiler/Tests/Cases/Simple.xaml

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
<Window x:Class="ILSpy.BamlDecompiler.Tests.Cases.Simple" Title="ILSpy.BamlDecompiler.Tests.Cases" Height="300" Width="300" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid />
</Window>

26
ILSpy.BamlDecompiler/Tests/Cases/Simple.xaml.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace ILSpy.BamlDecompiler.Tests.Cases
{
/// <summary>
/// Interaction logic for Simple.xaml
/// </summary>
public partial class Simple : Window
{
public Simple()
{
InitializeComponent();
}
}
}

1
ILSpy.BamlDecompiler/Tests/Cases/SimpleDictionary.xaml

@ -0,0 +1 @@ @@ -0,0 +1 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></ResourceDictionary>

112
ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj

@ -0,0 +1,112 @@ @@ -0,0 +1,112 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{1169E6D1-1899-43D4-A500-07CE4235B388}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<OutputType>Library</OutputType>
<RootNamespace>ILSpy.BamlDecompiler.Tests</RootNamespace>
<AssemblyName>ILSpy.BamlDecompiler.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x86' ">
<PlatformTarget>x86</PlatformTarget>
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>..\bin\Debug\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>..\bin\Release\</OutputPath>
<DebugSymbols>false</DebugSymbols>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="DiffLib">
<HintPath>..\..\packages\DiffLib.1.0.0.55\lib\net35-Client\DiffLib.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\..\ICSharpCode.Decompiler\Tests\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationCore">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="PresentationFramework" />
<Reference Include="PresentationFramework">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="WindowsBase">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Cases\Simple.xaml.cs">
<DependentUpon>Simple.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestRunner.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\ICSharpCode.Decompiler\Tests\ICSharpCode.Decompiler.Tests.csproj">
<Project>{FEC0DA52-C4A6-4710-BE36-B484A20C5E22}</Project>
<Name>ICSharpCode.Decompiler.Tests</Name>
</ProjectReference>
<ProjectReference Include="..\..\ILSpy\ILSpy.csproj">
<Project>{1E85EFF9-E370-4683-83E4-8A3D063FF791}</Project>
<Name>ILSpy</Name>
</ProjectReference>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">
<Project>{D68133BD-1E63-496E-9EDE-4FBDBF77B486}</Project>
<Name>Mono.Cecil</Name>
</ProjectReference>
<ProjectReference Include="..\..\SharpTreeView\ICSharpCode.TreeView.csproj">
<Project>{DDE2A481-8271-4EAC-A330-8FA6A38D13D1}</Project>
<Name>ICSharpCode.TreeView</Name>
</ProjectReference>
<ProjectReference Include="..\ILSpy.BamlDecompiler.csproj">
<Project>{A6BAD2BA-76BA-461C-8B6D-418607591247}</Project>
<Name>ILSpy.BamlDecompiler</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Cases" />
<Folder Include="Properties" />
</ItemGroup>
<ItemGroup>
<Page Include="Cases\Simple.xaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Page>
<Page Include="Cases\SimpleDictionary.xaml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project>

31
ILSpy.BamlDecompiler/Tests/Properties/AssemblyInfo.cs

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
#region Using directives
using System;
using System.Reflection;
using System.Runtime.InteropServices;
#endregion
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ILSpy.BamlDecompiler.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ILSpy.BamlDecompiler.Tests")]
[assembly: AssemblyCopyright("Copyright 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible(false)]
// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]

73
ILSpy.BamlDecompiler/Tests/TestRunner.cs

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Resources;
using System.Xml.Linq;
using ICSharpCode.Decompiler.Tests.Helpers;
using ICSharpCode.ILSpy;
using Mono.Cecil;
using NUnit.Framework;
using Ricciolo.StylesExplorer.MarkupReflection;
namespace ILSpy.BamlDecompiler.Tests
{
[TestFixture]
public class TestRunner
{
[Test]
public void Simple()
{
RunTest("cases/simple");
}
[Test]
public void SimpleDictionary()
{
RunTest("cases/simpledictionary");
}
void RunTest(string name)
{
string asmPath = typeof(TestRunner).Assembly.Location;
var assembly = AssemblyDefinition.ReadAssembly(asmPath);
Resource res = assembly.MainModule.Resources.First();
Stream bamlStream = LoadBaml(res, name + ".baml");
Assert.IsNotNull(bamlStream);
XDocument document = BamlResourceEntryNode.LoadIntoDocument(new DefaultAssemblyResolver(), assembly, bamlStream);
string path = Path.Combine("..\\..\\Tests", name + ".xaml");
CodeAssert.AreEqual(document.ToString(), File.ReadAllText(path));
}
Stream LoadBaml(Resource res, string name)
{
EmbeddedResource er = res as EmbeddedResource;
if (er != null) {
Stream s = er.GetResourceStream();
s.Position = 0;
ResourceReader reader;
try {
reader = new ResourceReader(s);
}
catch (ArgumentException) {
return null;
}
foreach (DictionaryEntry entry in reader.Cast<DictionaryEntry>().OrderBy(e => e.Key.ToString())) {
if (entry.Key.ToString() == name) {
if (entry.Value is Stream)
return (Stream)entry.Value;
if (entry.Value is byte[])
return new MemoryStream((byte[])entry.Value);
}
}
}
return null;
}
}
}

12
ILSpy.sln

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
# SharpDevelop 4.0.1.7146
# SharpDevelop 4.1.0.7466-alpha
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "doc", "doc", "{F45DB999-7E72-4000-B5AD-3A7B485A0896}"
ProjectSection(SolutionItems) = postProject
doc\Command Line.txt = doc\Command Line.txt
@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Cecil.Pdb", "Mono.Ceci @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Cecil.Pdb", "Mono.Ceci
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILSpy.BamlDecompiler", "ILSpy.BamlDecompiler\ILSpy.BamlDecompiler.csproj", "{A6BAD2BA-76BA-461C-8B6D-418607591247}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILSpy.BamlDecompiler.Tests", "ILSpy.BamlDecompiler\Tests\ILSpy.BamlDecompiler.Tests.csproj", "{1169E6D1-1899-43D4-A500-07CE4235B388}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -115,6 +117,14 @@ Global @@ -115,6 +117,14 @@ Global
{A6BAD2BA-76BA-461C-8B6D-418607591247}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6BAD2BA-76BA-461C-8B6D-418607591247}.Release|x86.Build.0 = Release|x86
{A6BAD2BA-76BA-461C-8B6D-418607591247}.Release|x86.ActiveCfg = Release|x86
{1169E6D1-1899-43D4-A500-07CE4235B388}.Debug|Any CPU.Build.0 = Debug|x86
{1169E6D1-1899-43D4-A500-07CE4235B388}.Debug|Any CPU.ActiveCfg = Debug|x86
{1169E6D1-1899-43D4-A500-07CE4235B388}.Debug|x86.Build.0 = Debug|x86
{1169E6D1-1899-43D4-A500-07CE4235B388}.Debug|x86.ActiveCfg = Debug|x86
{1169E6D1-1899-43D4-A500-07CE4235B388}.Release|Any CPU.Build.0 = Release|x86
{1169E6D1-1899-43D4-A500-07CE4235B388}.Release|Any CPU.ActiveCfg = Release|x86
{1169E6D1-1899-43D4-A500-07CE4235B388}.Release|x86.Build.0 = Release|x86
{1169E6D1-1899-43D4-A500-07CE4235B388}.Release|x86.ActiveCfg = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

5
ILSpy/LoadedAssembly.cs

@ -171,6 +171,11 @@ namespace ICSharpCode.ILSpy @@ -171,6 +171,11 @@ namespace ICSharpCode.ILSpy
}
}
public IAssemblyResolver GetAssemblyResolver()
{
return new MyAssemblyResolver(this);
}
public LoadedAssembly LookupReferencedAssembly(string fullName)
{
foreach (LoadedAssembly asm in assemblyList.GetAssemblies()) {

Loading…
Cancel
Save