mirror of https://github.com/icsharpcode/ILSpy.git
7 changed files with 0 additions and 484 deletions
@ -1,183 +0,0 @@ |
|||||||
// Copyright (c) Cristian Civera (cristian@aspitalia.com)
|
|
||||||
// This code is distributed under the MS-PL (for details please see \doc\MS-PL.txt)
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Collections.Generic; |
|
||||||
using System.ComponentModel; |
|
||||||
using System.Reflection; |
|
||||||
using System.Text; |
|
||||||
using System.IO; |
|
||||||
using System.Linq; |
|
||||||
using Microsoft.Win32; |
|
||||||
using System.Threading; |
|
||||||
using System.Security.Permissions; |
|
||||||
using System.Security; |
|
||||||
|
|
||||||
namespace Ricciolo.StylesExplorer.MarkupReflection |
|
||||||
{ |
|
||||||
public delegate void AssemblyResolveEventHandler(object s, AssemblyResolveEventArgs e); |
|
||||||
|
|
||||||
public class AppDomainTypeResolver : MarshalByRefObject, ITypeResolver |
|
||||||
{ |
|
||||||
private readonly AppDomain _domain; |
|
||||||
private string baseDir; |
|
||||||
|
|
||||||
public event AssemblyResolveEventHandler AssemblyResolve; |
|
||||||
|
|
||||||
public static AppDomainTypeResolver GetIntoNewAppDomain(string baseDir) |
|
||||||
{ |
|
||||||
AppDomainSetup info = new AppDomainSetup(); |
|
||||||
info.ApplicationBase = Environment.CurrentDirectory; |
|
||||||
AppDomain domain = AppDomain.CreateDomain("AppDomainTypeResolver", null, info, new PermissionSet(PermissionState.Unrestricted)); |
|
||||||
AppDomainTypeResolver resolver = (AppDomainTypeResolver)domain.CreateInstanceAndUnwrap(typeof(AppDomainTypeResolver).Assembly.FullName, |
|
||||||
typeof(AppDomainTypeResolver).FullName, false, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, new object[] { domain, baseDir }, null, null, null); |
|
||||||
|
|
||||||
return resolver; |
|
||||||
} |
|
||||||
|
|
||||||
Assembly domain_AssemblyResolve(object sender, ResolveEventArgs args) |
|
||||||
{ |
|
||||||
// Cerco di risolvere automaticamente
|
|
||||||
AssemblyName name = new AssemblyName(args.Name); |
|
||||||
string fileName = Path.Combine(this.baseDir, name.Name + ".exe"); |
|
||||||
if (!File.Exists(fileName)) |
|
||||||
fileName = Path.Combine(this.baseDir, name.Name + ".dll"); |
|
||||||
|
|
||||||
// Carico il percorso autocalcolato
|
|
||||||
if (File.Exists(fileName)) |
|
||||||
return Assembly.LoadFile(fileName); |
|
||||||
|
|
||||||
if (AssemblyResolve != null) |
|
||||||
{ |
|
||||||
AssemblyResolveEventArgs e = new AssemblyResolveEventArgs(args.Name, this.baseDir); |
|
||||||
AssemblyResolve(this, e); |
|
||||||
if (!String.IsNullOrEmpty(e.Location) && File.Exists(e.Location)) |
|
||||||
return Assembly.LoadFile(e.Location); |
|
||||||
} |
|
||||||
|
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
public static void DestroyResolver(AppDomainTypeResolver resolver) |
|
||||||
{ |
|
||||||
if (resolver == null) throw new ArgumentNullException("resolver"); |
|
||||||
|
|
||||||
ThreadPool.QueueUserWorkItem(delegate |
|
||||||
{ |
|
||||||
AppDomain.Unload(resolver.Domain); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
protected AppDomainTypeResolver(AppDomain domain, string baseDir) |
|
||||||
{ |
|
||||||
_domain = domain; |
|
||||||
this.baseDir = baseDir; |
|
||||||
|
|
||||||
domain.AssemblyResolve += new ResolveEventHandler(domain_AssemblyResolve); |
|
||||||
} |
|
||||||
|
|
||||||
public BamlAssembly LoadAssembly(AssemblyName asm) |
|
||||||
{ |
|
||||||
//return new BamlAssembly(Assembly.Load(asm));
|
|
||||||
return new BamlAssembly(_domain.Load(asm)); |
|
||||||
} |
|
||||||
|
|
||||||
public BamlAssembly LoadAssembly(string location) |
|
||||||
{ |
|
||||||
Assembly asm = Assembly.LoadFile(location); |
|
||||||
return new BamlAssembly(asm); |
|
||||||
//return _domain.Load(System.IO.File.ReadAllBytes(location));
|
|
||||||
//return Assembly.LoadFrom(location);
|
|
||||||
} |
|
||||||
|
|
||||||
public BamlAssembly[] GetReferencedAssemblies(BamlAssembly asm) |
|
||||||
{ |
|
||||||
AssemblyName[] list = asm.Assembly.GetReferencedAssemblies(); |
|
||||||
|
|
||||||
return (from an in list |
|
||||||
select this.LoadAssembly(an)).ToArray(); |
|
||||||
} |
|
||||||
|
|
||||||
public AppDomain Domain |
|
||||||
{ |
|
||||||
get { return _domain; } |
|
||||||
} |
|
||||||
|
|
||||||
#region ITypeResolver Members
|
|
||||||
|
|
||||||
public IType GetTypeByAssemblyQualifiedName(string name) |
|
||||||
{ |
|
||||||
return new DotNetType(name); |
|
||||||
} |
|
||||||
|
|
||||||
public IDependencyPropertyDescriptor GetDependencyPropertyDescriptor(string name, IType ownerType, IType targetType) |
|
||||||
{ |
|
||||||
if (name == null) throw new ArgumentNullException("name"); |
|
||||||
if (ownerType == null) throw new ArgumentNullException("ownerType"); |
|
||||||
if (targetType == null) throw new ArgumentNullException("targetType"); |
|
||||||
|
|
||||||
Type dOwnerType = ((DotNetType)ownerType).Type; |
|
||||||
Type dTargetType = ((DotNetType)targetType).Type; |
|
||||||
|
|
||||||
try |
|
||||||
{ |
|
||||||
DependencyPropertyDescriptor propertyDescriptor = DependencyPropertyDescriptor.FromName(name, dOwnerType, dTargetType); |
|
||||||
if (propertyDescriptor != null) |
|
||||||
return new WpfDependencyPropertyDescriptor(propertyDescriptor); |
|
||||||
return null; |
|
||||||
} |
|
||||||
catch (Exception) |
|
||||||
{ |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public bool IsLocalAssembly(string name) |
|
||||||
{ |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
public string RuntimeVersion { |
|
||||||
get { |
|
||||||
throw new NotImplementedException(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public override object InitializeLifetimeService() |
|
||||||
{ |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public class AssemblyResolveEventArgs : MarshalByRefObject |
|
||||||
{ |
|
||||||
|
|
||||||
private string _location; |
|
||||||
private string _name; |
|
||||||
private string _baseDir; |
|
||||||
|
|
||||||
public AssemblyResolveEventArgs(string name, string baseDir) |
|
||||||
{ |
|
||||||
_name = name; |
|
||||||
_baseDir = baseDir; |
|
||||||
} |
|
||||||
|
|
||||||
public string Location |
|
||||||
{ |
|
||||||
get { return _location; } |
|
||||||
set { _location = value; } |
|
||||||
} |
|
||||||
|
|
||||||
public string Name |
|
||||||
{ |
|
||||||
get { return _name; } |
|
||||||
} |
|
||||||
|
|
||||||
public string BaseDir |
|
||||||
{ |
|
||||||
get { return _baseDir; } |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,112 +0,0 @@ |
|||||||
// Copyright (c) Cristian Civera (cristian@aspitalia.com)
|
|
||||||
// This code is distributed under the MS-PL (for details please see \doc\MS-PL.txt)
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Collections; |
|
||||||
using System.Collections.Generic; |
|
||||||
using System.Collections.ObjectModel; |
|
||||||
using System.IO; |
|
||||||
using System.Reflection; |
|
||||||
using System.Resources; |
|
||||||
using System.Text; |
|
||||||
|
|
||||||
namespace Ricciolo.StylesExplorer.MarkupReflection |
|
||||||
{ |
|
||||||
public class BamlAssembly : MarshalByRefObject |
|
||||||
{ |
|
||||||
private readonly string _filePath; |
|
||||||
private Assembly _assembly; |
|
||||||
private BamlFileList _bamlFile; |
|
||||||
|
|
||||||
public BamlAssembly(Assembly assembly) |
|
||||||
{ |
|
||||||
_assembly = assembly; |
|
||||||
_filePath = assembly.CodeBase; |
|
||||||
|
|
||||||
ReadBaml(); |
|
||||||
} |
|
||||||
|
|
||||||
public BamlAssembly(string filePath) |
|
||||||
{ |
|
||||||
this._filePath = Path.GetFullPath(filePath); |
|
||||||
this._assembly = Assembly.LoadFile(this.FilePath); |
|
||||||
if (String.Compare(this.Assembly.CodeBase, this.FilePath, true) != 0) |
|
||||||
throw new ArgumentException("Cannot load filePath because Assembly is already loaded", "filePath"); |
|
||||||
|
|
||||||
ReadBaml(); |
|
||||||
} |
|
||||||
|
|
||||||
private void ReadBaml() |
|
||||||
{ |
|
||||||
// Get available names
|
|
||||||
string[] resources = this.Assembly.GetManifestResourceNames(); |
|
||||||
foreach (string res in resources) |
|
||||||
{ |
|
||||||
// Solo le risorse
|
|
||||||
if (String.Compare(Path.GetExtension(res), ".resources", true) != 0) continue; |
|
||||||
|
|
||||||
// Get stream
|
|
||||||
using (Stream stream = this.Assembly.GetManifestResourceStream(res)) |
|
||||||
{ |
|
||||||
try |
|
||||||
{ |
|
||||||
ResourceReader reader = new ResourceReader(stream); |
|
||||||
foreach (DictionaryEntry entry in reader) |
|
||||||
{ |
|
||||||
if (String.Compare(Path.GetExtension(entry.Key.ToString()), ".baml", true) == 0 && entry.Value is Stream) |
|
||||||
{ |
|
||||||
BamlFile bm = new BamlFile(GetAssemblyResourceUri(entry.Key.ToString()), (Stream)entry.Value); |
|
||||||
this.BamlFiles.Add(bm); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
catch (ArgumentException) |
|
||||||
{} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
private Uri GetAssemblyResourceUri(string resourceName) |
|
||||||
{ |
|
||||||
AssemblyName asm = this.Assembly.GetName(); |
|
||||||
byte[] data = asm.GetPublicKeyToken(); |
|
||||||
StringBuilder token = new StringBuilder(data.Length * 2); |
|
||||||
for (int x = 0; x < data.Length; x++) |
|
||||||
{ |
|
||||||
token.Append(data[x].ToString("x", System.Globalization.CultureInfo.InvariantCulture)); |
|
||||||
} |
|
||||||
|
|
||||||
return new Uri(String.Format(@"{0};V{1};{2};component\{3}", asm.Name, asm.Version, token, Path.ChangeExtension(resourceName, ".xaml")), UriKind.RelativeOrAbsolute); |
|
||||||
} |
|
||||||
|
|
||||||
public string FilePath |
|
||||||
{ |
|
||||||
get { return _filePath; } |
|
||||||
} |
|
||||||
|
|
||||||
public Assembly Assembly |
|
||||||
{ |
|
||||||
get { return _assembly; } |
|
||||||
} |
|
||||||
|
|
||||||
public BamlFileList BamlFiles |
|
||||||
{ |
|
||||||
get |
|
||||||
{ |
|
||||||
if (_bamlFile == null) |
|
||||||
_bamlFile = new BamlFileList(); |
|
||||||
return _bamlFile; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override object InitializeLifetimeService() |
|
||||||
{ |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
[Serializable()] |
|
||||||
public class BamlFileList : Collection<BamlFile> |
|
||||||
{} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,80 +0,0 @@ |
|||||||
// Copyright (c) Cristian Civera (cristian@aspitalia.com)
|
|
||||||
// This code is distributed under the MS-PL (for details please see \doc\MS-PL.txt)
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Collections; |
|
||||||
using System.Collections.Generic; |
|
||||||
using System.ComponentModel; |
|
||||||
using System.IO; |
|
||||||
using System.Resources; |
|
||||||
using System.Text; |
|
||||||
using System.Windows; |
|
||||||
|
|
||||||
namespace Ricciolo.StylesExplorer.MarkupReflection |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// Rappresenta un singole file Baml all'interno di un assembly
|
|
||||||
/// </summary>
|
|
||||||
public class BamlFile : Component |
|
||||||
{ |
|
||||||
private Uri _uri; |
|
||||||
private readonly Stream _stream; |
|
||||||
|
|
||||||
public BamlFile(Uri uri, Stream stream) |
|
||||||
{ |
|
||||||
if (uri == null) |
|
||||||
new ArgumentNullException("uri"); |
|
||||||
if (stream == null) |
|
||||||
throw new ArgumentNullException("stream"); |
|
||||||
|
|
||||||
_uri = uri; |
|
||||||
_stream = stream; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Carica il Baml attraverso il motore di WPF con Application.LoadComponent
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public object LoadContent() |
|
||||||
{ |
|
||||||
try |
|
||||||
{ |
|
||||||
return Application.LoadComponent(this.Uri); |
|
||||||
} |
|
||||||
catch (Exception e) |
|
||||||
{ |
|
||||||
throw new InvalidOperationException("Invalid baml file.", e); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
protected override void Dispose(bool disposing) |
|
||||||
{ |
|
||||||
base.Dispose(disposing); |
|
||||||
|
|
||||||
if (disposing) |
|
||||||
this.Stream.Dispose(); |
|
||||||
} |
|
||||||
|
|
||||||
public override object InitializeLifetimeService() |
|
||||||
{ |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Restituisce lo stream originale contenente il Baml
|
|
||||||
/// </summary>
|
|
||||||
public Stream Stream |
|
||||||
{ |
|
||||||
get { return _stream; } |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Restituisce l'indirizzo secondo lo schema pack://
|
|
||||||
/// </summary>
|
|
||||||
public Uri Uri |
|
||||||
{ |
|
||||||
get { return _uri; } |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,64 +0,0 @@ |
|||||||
// Copyright (c) Cristian Civera (cristian@aspitalia.com)
|
|
||||||
// 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; |
|
||||||
|
|
||||||
namespace Ricciolo.StylesExplorer.MarkupReflection |
|
||||||
{ |
|
||||||
public class DotNetType : MarshalByRefObject, IType |
|
||||||
{ |
|
||||||
private readonly string _assemblyQualifiedName; |
|
||||||
private Type _type; |
|
||||||
|
|
||||||
public DotNetType(string assemblyQualifiedName) |
|
||||||
{ |
|
||||||
if (assemblyQualifiedName == null) throw new ArgumentNullException("assemblyQualifiedName"); |
|
||||||
|
|
||||||
_assemblyQualifiedName = assemblyQualifiedName; |
|
||||||
_type = Type.GetType(assemblyQualifiedName, false, true); |
|
||||||
} |
|
||||||
|
|
||||||
#region IType Members
|
|
||||||
|
|
||||||
public string AssemblyQualifiedName |
|
||||||
{ |
|
||||||
get { return _assemblyQualifiedName; } |
|
||||||
} |
|
||||||
|
|
||||||
public bool IsSubclassOf(IType type) |
|
||||||
{ |
|
||||||
if (type == null) throw new ArgumentNullException("type"); |
|
||||||
if (!(type is DotNetType)) throw new ArgumentException("type"); |
|
||||||
if (_type == null) return false; |
|
||||||
return this._type.IsSubclassOf(((DotNetType)type).Type); |
|
||||||
} |
|
||||||
|
|
||||||
public bool Equals(IType type) |
|
||||||
{ |
|
||||||
if (type == null) throw new ArgumentNullException("type"); |
|
||||||
if (!(type is DotNetType)) throw new ArgumentException("type"); |
|
||||||
if (_type == null) return false; |
|
||||||
return this._type.Equals(((DotNetType)type).Type); |
|
||||||
} |
|
||||||
|
|
||||||
public IType BaseType { |
|
||||||
get { |
|
||||||
return new DotNetType(this._type.BaseType.AssemblyQualifiedName); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public Type Type |
|
||||||
{ |
|
||||||
get { return _type; } |
|
||||||
} |
|
||||||
|
|
||||||
public override object InitializeLifetimeService() |
|
||||||
{ |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,35 +0,0 @@ |
|||||||
// Copyright (c) Cristian Civera (cristian@aspitalia.com)
|
|
||||||
// This code is distributed under the MS-PL (for details please see \doc\MS-PL.txt)
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Collections.Generic; |
|
||||||
using System.ComponentModel; |
|
||||||
using System.Text; |
|
||||||
|
|
||||||
namespace Ricciolo.StylesExplorer.MarkupReflection |
|
||||||
{ |
|
||||||
public class WpfDependencyPropertyDescriptor : MarshalByRefObject, IDependencyPropertyDescriptor |
|
||||||
{ |
|
||||||
private readonly DependencyPropertyDescriptor _propertyDescriptor; |
|
||||||
|
|
||||||
public WpfDependencyPropertyDescriptor(DependencyPropertyDescriptor propertyDescriptor) |
|
||||||
{ |
|
||||||
if (propertyDescriptor == null) throw new ArgumentNullException("propertyDescriptor"); |
|
||||||
_propertyDescriptor = propertyDescriptor; |
|
||||||
} |
|
||||||
|
|
||||||
#region IDependencyPropertyDescriptor Members
|
|
||||||
|
|
||||||
public bool IsAttached |
|
||||||
{ |
|
||||||
get { return _propertyDescriptor.IsAttached; } |
|
||||||
} |
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public override object InitializeLifetimeService() |
|
||||||
{ |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue