Browse Source

implement ITypeResolver based on Cecil to avoid loading of assemblies using reflection

pull/182/merge
Siegfried Pammer 14 years ago
parent
commit
7843a5b95f
  1. 2
      ILSpy.BamlDecompiler/BamlResourceEntryNode.cs
  2. 34
      ILSpy.BamlDecompiler/CecilDependencyPropertyDescriptor.cs
  3. 71
      ILSpy.BamlDecompiler/CecilType.cs
  4. 51
      ILSpy.BamlDecompiler/CecilTypeResolver.cs
  5. 3
      ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj

2
ILSpy.BamlDecompiler/BamlResourceEntryNode.cs

@ -51,7 +51,7 @@ namespace ILSpy.BamlDecompiler @@ -51,7 +51,7 @@ namespace ILSpy.BamlDecompiler
bamlStream.Position = 0;
XDocument xamlDocument;
using (XmlBamlReader reader = new XmlBamlReader(bamlStream))
using (XmlBamlReader reader = new XmlBamlReader(bamlStream, new CecilTypeResolver(asm)))
xamlDocument = XDocument.Load(reader);
output.Write(xamlDocument.ToString());

34
ILSpy.BamlDecompiler/CecilDependencyPropertyDescriptor.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.Linq;
using ICSharpCode.ILSpy;
using Mono.Cecil;
using Ricciolo.StylesExplorer.MarkupReflection;
namespace ILSpy.BamlDecompiler
{
public class CecilDependencyPropertyDescriptor : IDependencyPropertyDescriptor
{
string member;
TypeDefinition type;
public CecilDependencyPropertyDescriptor(string member, TypeDefinition type)
{
this.member = member;
this.type = type;
}
public bool IsAttached {
get {
return type.Methods.Any(m => m.Name == "Get" + member);
}
}
public override string ToString()
{
return string.Format("[CecilDependencyPropertyDescriptor Member={0}, Type={1}]", member, type);
}
}
}

71
ILSpy.BamlDecompiler/CecilType.cs

@ -0,0 +1,71 @@ @@ -0,0 +1,71 @@
// 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;
using ICSharpCode.ILSpy;
using Mono.Cecil;
using Ricciolo.StylesExplorer.MarkupReflection;
namespace ILSpy.BamlDecompiler
{
public class CecilType : IType
{
internal readonly TypeDefinition type;
public CecilType(TypeDefinition type)
{
this.type = type;
}
public string AssemblyQualifiedName {
get {
return type.FullName +
", " + type.Module.Assembly.FullName;
}
}
public bool IsSubclassOf(IType type)
{
if (type == null)
throw new ArgumentNullException("type");
if (!(type is CecilType))
throw new ArgumentException("type has to be a CecilType");
CecilType ct = (CecilType)type;
var t = ct.type;
while (t != null) {
if (t == ct.type)
return true;
foreach (var @interface in t.Interfaces) {
var resolved = @interface.Resolve();
if (resolved == ct.type)
return true;
}
if (t.BaseType == null)
break;
t = t.BaseType.Resolve();
}
return false;
}
public bool Equals(IType type)
{
if (type == null)
throw new ArgumentNullException("type");
if (!(type is CecilType))
throw new ArgumentException("type has to be a CecilType");
return this.type == ((CecilType)type).type;
}
public override string ToString()
{
return string.Format("[CecilType Type={0}]", type);
}
}
}

51
ILSpy.BamlDecompiler/CecilTypeResolver.cs

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
// 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;
using ICSharpCode.ILSpy;
using Mono.Cecil;
using Ricciolo.StylesExplorer.MarkupReflection;
namespace ILSpy.BamlDecompiler
{
/// <summary>
/// Description of CecilTypeResolver.
/// </summary>
public class CecilTypeResolver : ITypeResolver
{
LoadedAssembly assembly;
public CecilTypeResolver(LoadedAssembly assembly)
{
this.assembly = assembly;
}
public IType GetTypeByAssemblyQualifiedName(string name)
{
int comma = name.IndexOf(',');
if (comma == -1)
throw new ArgumentException("invalid name");
string fullName = name.Substring(0, comma);
string assemblyName = name.Substring(comma + 1).Trim();
var type = assembly.AssemblyDefinition.MainModule.GetType(fullName);
if (type == null) {
var otherAssembly = assembly.LookupReferencedAssembly(assemblyName);
type = otherAssembly.AssemblyDefinition.MainModule.GetType(fullName);
}
return new CecilType(type);
}
public IDependencyPropertyDescriptor GetDependencyPropertyDescriptor(string name, IType ownerType, IType targetType)
{
if (!(ownerType is CecilType))
throw new ArgumentException();
return new CecilDependencyPropertyDescriptor(name, ((CecilType)ownerType).type);
}
}
}

3
ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj

@ -66,6 +66,9 @@ @@ -66,6 +66,9 @@
<ItemGroup>
<Compile Include="BamlResourceNodeFactory.cs" />
<Compile Include="BamlResourceEntryNode.cs" />
<Compile Include="CecilDependencyPropertyDescriptor.cs" />
<Compile Include="CecilType.cs" />
<Compile Include="CecilTypeResolver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\AppDomainTypeResolver.cs" />
<Compile Include="Ricciolo.StylesExplorer.MarkupReflection\BamlAssembly.cs" />

Loading…
Cancel
Save