Reflector
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Linq.Expressions; using System.IO; using System.Web; namespace PK.Utilities.Reflection { public class Reflector { private delegate T ObjectActivator<T>(params object[] args); /// <summary> /// Create an instance from an object using reflection and linq expressions. Works for every constructor /// </summary> /// <typeparam name="T">The type of the object to instance</typeparam> /// <param name="assemblyName">The name of the assembly who stores the type</param> /// <param name="typeFullName">The fullname of the type to instanciate</param> /// <param name="args">The arguments of the constructor</param> /// <returns>An instance of the given type</returns> public static T GetInstance<T>(string assemblyName, string typeFullName, params object[] args) { T res = default(T); //var completeAssemblyName =Path.Combine(GetRunningPath(), assemblyName + (assemblyName.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) ? "" : ".dll")); Assembly assembly = Assembly.Load(GetAssemblyName(assemblyName)); Type type = assembly.GetType(typeFullName); ConstructorInfo ctor = GetConstructor(args, type); ObjectActivator<T> createdActivator = GetActivator<T>(ctor); res = createdActivator(args); return res; } private static AssemblyName GetAssemblyName(string assemblyName) { var name = new AssemblyName(assemblyName); return name; } private static ConstructorInfo GetConstructor(object[] args, Type type) { if (args == null) args = new object[0]; ConstructorInfo ctor = type.GetConstructor(args.Select(o => o.GetType()).ToArray()); return ctor; } /// <summary> /// Create an instance from an object using reflection and linq expressions. Works for every constructor /// </summary> /// <typeparam name="T">The type of the object to instance</typeparam> /// <param name="type">A Type object containing the object type to instantiate</param> /// <param name="args">The arguments of the constructor</param> /// <returns>An instance of the given type</returns> public static T GetInstance<T>(Type type, params object[] args) { T res = default(T); ConstructorInfo ctor = GetConstructor(args, type); ObjectActivator<T> createdActivator = GetActivator<T>(ctor); res = createdActivator(args); return res; } /// <summary> /// Create an instance from an object using reflection and linq expressions. Uses the default constructor. /// </summary> /// <typeparam name="T">The type of the object to instance</typeparam> /// <param name="assemblyName">The name of the assembly who stores the type</param> /// <param name="typeFullName">The fullname of the type to instanciate</param> /// <returns>An instance of the given type</returns> public static T GetInstance<T>(string assemblyName, string typeFullName) { return GetInstance<T>(assemblyName, typeFullName, new object[] { }); } /// <summary> /// Create an instance from an object using reflection and linq expressions. Uses the default constructor. /// </summary> /// <typeparam name="T">The type of the object to instance</typeparam> /// <param name="fullTypeName">The string with full type name and asembly, in format "fullName, Assembly"</param> /// <returns>An instance of the given type</returns> public static T GetInstance<T>(string fullTypeName) { var values = fullTypeName.Split(new char[]{','}, 2 ); return GetInstance<T>(values[1].Trim(), values[0].Trim(), new object[] { }); } /// <summary> /// Creates an ObjectActivator specifyc for an object type and a constructor type /// </summary> /// <typeparam name="T">Type of the object to get an instance for</typeparam> /// <param name="ctor">ContructorInfo containing detail information for an specicyc contrustor</param> /// <returns>An delegate of the compiled ObjectActivator method</returns> private static ObjectActivator<T> GetActivator<T>(ConstructorInfo ctor) { ParameterInfo[] paramsInfo = ctor.GetParameters(); //create a single param of type object[] ParameterExpression param = Expression.Parameter(typeof(object[]), "args"); Expression[] argsExp = new Expression[paramsInfo.Length]; //pick each arg from the params array //and create a typed expression of them for (int i = 0; i < paramsInfo.Length; i++) { Expression index = Expression.Constant(i); Type paramType = paramsInfo[i].ParameterType; Expression paramAccessorExp = Expression.ArrayIndex(param, index); Expression paramCastExp = Expression.Convert(paramAccessorExp, paramType); argsExp[i] = paramCastExp; } //make a NewExpression that calls the //ctor with the args we just created NewExpression newExp = Expression.New(ctor, argsExp); //create a lambda with the New //Expression as body and our param object[] as arg LambdaExpression lambda = Expression.Lambda(typeof(ObjectActivator<T>), newExp, param); //compile it ObjectActivator<T> compiled = (ObjectActivator<T>)lambda.Compile(); return compiled; } } }