using System;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
using System.Linq;
using System.Globalization;
using File.PropertyProvider;
namespace AssemblyFileProperties
{
class AssemblyInspector : MarshalByRefObject
{
private class AssemblyReflectedPropertyReader : ReflectedPropertyReader
{
public AssemblyReflectedPropertyReader()
{
}
protected override bool StoreProperty(PropertyInfo p)
{
if (p.Name == "ReflectionOnly")
return false;
return base.StoreProperty(p);
}
}
private IDictionary<string, object> m_properties = new Dictionary<string, object>();
public AssemblyInspector()
{
}
public static IEnumerable<KeyValuePair<string, Type>> GetFields()
{
ReflectedPropertyReader reader = new AssemblyReflectedPropertyReader();
IEnumerable<KeyValuePair<string, Type>> fields = reader.GetFields(typeof(Assembly));
fields = fields.Concat(reader.GetFields(typeof(AssemblyName), "Name/"));
fields = fields.Concat(reader.GetFields(typeof(CultureInfo), "Culture/"));
IDictionary<string, Type> d = fields.ToDictionary(key => key.Key, value => value.Value);
d.Add("PublicKeyToken", typeof(string));
d.Add("Assembly.Type", typeof(IList<IDictionary<string, object>>));
return d;
}
public void InspectAssembly(string path, IEnumerable<string> selectProperties)
{
try
{
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
Assembly a = Assembly.ReflectionOnlyLoadFrom(path);
ReflectedPropertyReader reader = new AssemblyReflectedPropertyReader();
foreach (KeyValuePair<string, object> pair in reader.GetProperties(a.GetName(), null))
m_properties.Add("Name/" + pair.Key, pair.Value);
foreach (KeyValuePair<string, object> pair in reader.GetProperties(a, selectProperties))
m_properties.Add(pair.Key, pair.Value);
m_properties.Add("PublicKeyToken", BitConverter.ToString(a.GetName().GetPublicKeyToken()));
foreach (KeyValuePair<string, object> pair in reader.GetProperties(a.GetName().CultureInfo, null))
m_properties.Add("Culture/" + pair.Key, pair.Value);
TypePropertyReader typeReader = new TypePropertyReader();
IList<IDictionary<string, object>> list = new List<IDictionary<string, object>>();
foreach (Type t in a.GetTypes())
list.Add(typeReader.ReadObjectProperties(t));
m_properties.Add("Assembly.Type", list);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
finally
{
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
}
}
Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
{
return Assembly.ReflectionOnlyLoad(args.Name);
}
public IEnumerable<KeyValuePair<string, object>> Properties
{
get
{
return m_properties;
}
}
}
}