Browse Source

decompile Name attributes on type of current assembly to x:Name

pull/252/head
Siegfried Pammer 14 years ago
parent
commit
2c6e34ef07
  1. 11
      ILSpy.BamlDecompiler/CecilTypeResolver.cs
  2. 54
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlProperty.cs
  3. 30
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs
  4. 7
      ILSpy.BamlDecompiler/Tests/Cases/MyControl.xaml
  5. 26
      ILSpy.BamlDecompiler/Tests/Cases/MyControl.xaml.cs
  6. 6
      ILSpy.BamlDecompiler/Tests/Cases/SimpleNames.xaml
  7. 5
      ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj

11
ILSpy.BamlDecompiler/CecilTypeResolver.cs

@ -25,7 +25,16 @@ namespace ILSpy.BamlDecompiler @@ -25,7 +25,16 @@ namespace ILSpy.BamlDecompiler
public bool IsLocalAssembly(string name)
{
return name == this.thisAssembly.Name.Name;
return MakeShort(name) == this.thisAssembly.Name.Name;
}
string MakeShort(string name)
{
int endOffset = name.IndexOf(',');
if (endOffset == -1)
return name;
return name.Substring(0, endOffset);
}
public IType GetTypeByAssemblyQualifiedName(string name)

54
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlProperty.cs

@ -10,18 +10,18 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -10,18 +10,18 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{
internal class XmlBamlProperty : XmlBamlNode
{
private PropertyDeclaration propertyDeclaration;
private PropertyType propertyType;
private object value;
PropertyType propertyType;
public XmlBamlProperty(PropertyType propertyType)
public XmlBamlProperty(XmlBamlElement parent, PropertyType propertyType)
{
this.Parent = parent;
this.propertyType = propertyType;
}
public XmlBamlProperty(PropertyType propertyType, PropertyDeclaration propertyDeclaration)
public XmlBamlProperty(XmlBamlElement parent, PropertyType propertyType, PropertyDeclaration propertyDeclaration)
{
this.propertyDeclaration = propertyDeclaration;
this.Parent = parent;
this.PropertyDeclaration = propertyDeclaration;
this.propertyType = propertyType;
}
@ -30,44 +30,18 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -30,44 +30,18 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
return this.PropertyDeclaration.Name;
}
public PropertyDeclaration PropertyDeclaration
{
get
{
return this.propertyDeclaration;
}
set
{
this.propertyDeclaration = value;
}
}
public XmlBamlElement Parent { get; set; }
public PropertyType PropertyType
{
get
{
return this.propertyType;
}
}
public PropertyDeclaration PropertyDeclaration { get; set; }
public object Value
{
get
{
return this.value;
}
set
{
this.value = value;
}
public PropertyType PropertyType {
get { return this.propertyType; }
}
public override XmlNodeType NodeType
{
get
{
return XmlNodeType.Attribute;
}
public object Value { get; set; }
public override XmlNodeType NodeType {
get { return XmlNodeType.Attribute; }
}
}

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

@ -777,7 +777,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -777,7 +777,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
return;
}
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Value, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Value, pd);
property.Value = value;
nodes.Enqueue(property);
@ -907,7 +907,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -907,7 +907,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
throw new NotSupportedException("Unknown property with extension");
}
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Value, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Value, pd);
property.Value = value;
nodes.Enqueue(property);
@ -919,7 +919,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -919,7 +919,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
string text = reader.ReadString();
PropertyDeclaration pd = this.GetPropertyDeclaration(identifier);
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Value, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Value, pd);
property.Value = text;
nodes.Enqueue(property);
@ -932,7 +932,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -932,7 +932,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
reader.ReadInt16();
PropertyDeclaration pd = this.GetPropertyDeclaration(identifier);
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Value, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Value, pd);
property.Value = text;
nodes.Enqueue(property);
@ -945,8 +945,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -945,8 +945,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
reader.ReadByte();
string name = reader.ReadString();
TypeDeclaration declaringType = this.GetTypeDeclaration(identifier);
PropertyDeclaration declaration2 = new PropertyDeclaration(name, declaringType);
this.propertyTable.Add(key, declaration2);
PropertyDeclaration property = new PropertyDeclaration(name, declaringType);
this.propertyTable.Add(key, property);
}
void ReadDefAttributeKeyType()
@ -985,7 +985,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -985,7 +985,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
break;
}
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Key, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Key, pd);
property.Value = text;
nodes.Enqueue(property);
@ -1250,7 +1250,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1250,7 +1250,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
void AddKeyToElement(string key)
{
PropertyDeclaration pd = new PropertyDeclaration("Key", XamlTypeDeclaration);
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Key, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Key, pd);
property.Value = key;
@ -1393,7 +1393,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1393,7 +1393,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
PropertyDeclaration pd = new PropertyDeclaration(this.stringTable[valueIdentifier].ToString());
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Value, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Value, pd);
property.Value = text;
}
@ -1405,7 +1405,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1405,7 +1405,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
PropertyDeclaration pd = this.GetPropertyDeclaration(identifier);
string value = this.GetTypeExtension(typeIdentifier);
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Value, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Value, pd);
property.Value = value;
nodes.Enqueue(property);
@ -1524,7 +1524,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1524,7 +1524,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
string prefix = this.LookupPrefix(XmlPIMapping.PresentationNamespace, false);
string value = String.Format("{{{0}{1}StaticResource {2}}}", prefix, (String.IsNullOrEmpty(prefix)) ? String.Empty : ":", staticResource);
XmlBamlProperty property = new XmlBamlProperty(PropertyType.Value, pd);
XmlBamlProperty property = new XmlBamlProperty(elements.Peek(), PropertyType.Value, pd);
property.Value = value;
nodes.Enqueue(property);
@ -1637,13 +1637,17 @@ namespace Ricciolo.StylesExplorer.MarkupReflection @@ -1637,13 +1637,17 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
XmlBamlNode node = this.CurrentNode;
if (node is XmlBamlSimpleProperty)
return ((XmlBamlSimpleProperty)node).NamespaceName;
else if (node is XmlBamlProperty)
{
else if (node is XmlBamlProperty) {
declaration = ((XmlBamlProperty)node).PropertyDeclaration.DeclaringType;
TypeDeclaration elementDeclaration = this.readingElements.Peek().TypeDeclaration;
XmlPIMapping propertyMapping = FindByClrNamespaceAndAssemblyId(declaration) ?? XmlPIMapping.Presentation;
XmlPIMapping elementMapping = FindByClrNamespaceAndAssemblyId(elementDeclaration) ?? XmlPIMapping.Presentation;
if (((XmlBamlProperty)node).PropertyDeclaration.Name == "Name" &&
_resolver.IsLocalAssembly(((XmlBamlProperty)node).Parent.TypeDeclaration.Assembly))
return XWPFNamespace;
if (String.CompareOrdinal(propertyMapping.XmlNamespace, elementMapping.XmlNamespace) == 0
|| (elementDeclaration.Type != null && declaration.Type != null && elementDeclaration.Type.IsSubclassOf(declaration.Type)))
return String.Empty;

7
ILSpy.BamlDecompiler/Tests/Cases/MyControl.xaml

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
<UserControl x:Class="ILSpy.BamlDecompiler.Tests.Cases.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
</Grid>
</UserControl>

26
ILSpy.BamlDecompiler/Tests/Cases/MyControl.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 MyControl.xaml
/// </summary>
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
}
}

6
ILSpy.BamlDecompiler/Tests/Cases/SimpleNames.xaml

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
<Window x:Class="ILSpy.BamlDecompiler.Tests.Cases.SimpleNames" 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">
<Window x:Class="ILSpy.BamlDecompiler.Tests.Cases.SimpleNames" 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" xmlns:mc="clr-namespace:ILSpy.BamlDecompiler.Tests.Cases">
<Grid>
<Button x:Name="test" />
<Button Name="test2" />
<Button Name="test" />
<mc:MyControl x:Name="test2" />
</Grid>
</Window>

5
ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj

@ -70,6 +70,10 @@ @@ -70,6 +70,10 @@
<DependentUpon>AttachedEvent.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Cases\MyControl.xaml.cs">
<DependentUpon>MyControl.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Cases\Resources.xaml.cs">
<DependentUpon>Resources.xaml</DependentUpon>
<SubType>Code</SubType>
@ -117,6 +121,7 @@ @@ -117,6 +121,7 @@
<Page Include="Cases\AttachedEvent.xaml" />
<Page Include="Cases\AvalonDockBrushes.xaml" />
<Page Include="Cases\AvalonDockCommon.xaml" />
<Page Include="Cases\MyControl.xaml" />
<Page Include="Cases\Resources.xaml" />
<Page Include="Cases\Simple.xaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

Loading…
Cancel
Save