/// <summary> /// Returns the handler instance from the dependency resolver if there is one registered /// else returns the activator that calls the default constructor for the give handlerType. /// </summary> /// <param name="request"> /// The request. /// </param> /// <param name="descriptor"> /// The handler descriptor. /// </param> /// <param name="activator"> /// The activator. /// </param> /// <returns> /// The handler. /// </returns> private THandler GetInstanceOrActivator(HandlerRequest request, HandlerDescriptor descriptor, out Func <THandler> activator) { Contract.Requires(request != null); Contract.Requires(descriptor != null); // If dependency resolver returns handler object then use it. THandler instance = (THandler)request.GetDependencyScope().GetService(descriptor.HandlerType); if (instance != null) { activator = null; return(instance); } switch (descriptor.Lifetime) { case HandlerLifetime.Singleton: activator = this.CreateSingletonActivator(descriptor.HandlerType); return(null); case HandlerLifetime.PerRequest: activator = CreatePerRequestActivator(request, descriptor); return(null); case HandlerLifetime.Transient: activator = TypeActivator.Create <THandler>(descriptor.HandlerType); return(null); default: throw Error.InvalidEnumArgument("descriptor", (int)descriptor.Lifetime, typeof(HandlerLifetime)); } }
internal static IActionResultConverter GetResultConverter(Type type) { if (type != null && type.IsGenericParameter) { // This can happen if somebody declares an action method as: // public T Get<T>() { } throw Error.InvalidOperation( SRResources.HttpActionDescriptor_NoConverterForGenericParamterTypeExists, type ); } if (type == null) { return(_voidResultConverter); } else if (typeof(HttpResponseMessage).IsAssignableFrom(type)) { return(_responseMessageResultConverter); } else if (typeof(IHttpActionResult).IsAssignableFrom(type)) { return(null); } else { Type valueConverterType = typeof(ValueResultConverter <>).MakeGenericType(type); return(TypeActivator.Create <IActionResultConverter>(valueConverterType).Invoke()); } }
private static Func <THandler> CreateDelegatingActivator(Type handlerType) { Contract.Requires(handlerType != null); Func <THandler> activator = TypeActivator.Create <THandler>(handlerType); THandler instance = activator(); return(() => instance); }
/// <summary> /// 从程序集中查找实现INbRegistryService接口的服务 /// </summary> /// <param name="assemblies"></param> /// <returns></returns> public IList <INbRegistryService <T> > FindAllServices(params Assembly[] assemblies) { var nbRegistryServices = new List <INbRegistryService <T> >(); var findAllImplTypes = FindAllImplTypes(assemblies); foreach (var findAllImplType in findAllImplTypes) { Func <INbRegistryService <T> > func = TypeActivator.Create <INbRegistryService <T> >(findAllImplType); nbRegistryServices.Add(func()); } return(nbRegistryServices); }
public static IDAC CreateDAC(DBMSType dbmsType, string connectionString, ILogger logger = null) { switch (dbmsType) { case DBMSType.SQLServer: return((IDAC)TypeActivator.Create("Sphere10.Framework.Data.MSSQLDAC", "Sphere10.Framework.Data.MSSQL", connectionString, logger)); case DBMSType.Sqlite: return((IDAC)TypeActivator.Create("Sphere10.Framework.Data.SqliteDAC", "Sphere10.Framework.Data.Sqlite", connectionString, logger)); case DBMSType.Firebird: case DBMSType.FirebirdFile: return((IDAC)TypeActivator.Create("Sphere10.Framework.Data.FirebirdDAC", "Sphere10.Framework.Data.Firebird", connectionString, logger)); default: throw new NotSupportedException(dbmsType.ToString()); } }
public static object Create(Type targetType, params object[] args) { return(TypeActivator.Create(targetType, args)); }
public static object Create(string typeName, params object[] args) { return(TypeActivator.Create(typeName, args)); }