Browse Source

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

pull/252/head
Siegfried Pammer 15 years ago
parent
commit
2c6e34ef07
  1. 11
      ILSpy.BamlDecompiler/CecilTypeResolver.cs
  2. 56
      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
public bool IsLocalAssembly(string name) 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) public IType GetTypeByAssemblyQualifiedName(string name)

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

@ -10,18 +10,18 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{ {
internal class XmlBamlProperty : XmlBamlNode internal class XmlBamlProperty : XmlBamlNode
{ {
private PropertyDeclaration propertyDeclaration; PropertyType propertyType;
private PropertyType propertyType;
private object value;
public XmlBamlProperty(PropertyType propertyType) public XmlBamlProperty(XmlBamlElement parent, PropertyType propertyType)
{ {
this.Parent = parent;
this.propertyType = propertyType; 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; this.propertyType = propertyType;
} }
@ -29,45 +29,19 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{ {
return this.PropertyDeclaration.Name; return this.PropertyDeclaration.Name;
} }
public XmlBamlElement Parent { get; set; }
public PropertyDeclaration PropertyDeclaration { get; set; }
public PropertyDeclaration PropertyDeclaration public PropertyType PropertyType {
{ get { return this.propertyType; }
get
{
return this.propertyDeclaration;
}
set
{
this.propertyDeclaration = value;
}
} }
public PropertyType PropertyType public object Value { get; set; }
{
get
{
return this.propertyType;
}
}
public object Value public override XmlNodeType NodeType {
{ get { return XmlNodeType.Attribute; }
get
{
return this.value;
}
set
{
this.value = value;
}
}
public override XmlNodeType NodeType
{
get
{
return XmlNodeType.Attribute;
}
} }
} }

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

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

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

@ -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 @@
// 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 @@
<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> <Grid>
<Button x:Name="test" /> <Button Name="test" />
<Button Name="test2" /> <mc:MyControl x:Name="test2" />
</Grid> </Grid>
</Window> </Window>

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

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

Loading…
Cancel
Save