protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { var commandHandler = base.CreateInstance(context, constructor, arguments); var result = chainBuilder.BuildChain(commandHandler); return result; }
protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { object component = base.CreateInstance(context, constructor, arguments); AssignViewModel(component, arguments); return component; }
protected virtual object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { object instance = null; var implType = Model.Implementation; var createProxy = Kernel.ProxyFactory.ShouldCreateProxy(Model); if (createProxy == false && Model.Implementation.IsAbstract) { throw new ComponentRegistrationException( string.Format( "Type {0} is abstract.{2} As such, it is not possible to instansiate it as implementation of service '{1}'. Did you forget to proxy it?", Model.Implementation.FullName, Model.Name, Environment.NewLine)); } var createInstance = true; if (createProxy) { createInstance = Kernel.ProxyFactory.RequiresTargetInstance(Kernel, Model); } if (createInstance) { instance = CreateInstanceCore(constructor, arguments, implType); } if (createProxy) { try { instance = Kernel.ProxyFactory.Create(Kernel, instance, Model, context, arguments); } catch (Exception ex) { if (arguments != null) { foreach (var argument in arguments) { Kernel.ReleaseComponent(argument); } } throw new ComponentActivatorException("ComponentActivator: could not proxy " + Model.Implementation.FullName, ex, Model); } } return instance; }
/// <summary> /// Adds the specified candidate. /// </summary> /// <param name="candidate">The candidate.</param> public void Add(ConstructorCandidate candidate) { if (fewerArgumentsCandidate == null) { fewerArgumentsCandidate = candidate; } else { if (candidate.Constructor.GetParameters().Length < fewerArgumentsCandidate.Constructor.GetParameters().Length) { fewerArgumentsCandidate = candidate; } } InnerList.Add(candidate); }
private bool CheckCtorCandidate(ConstructorCandidate candidate, CreationContext context, out int candidatePoints) { candidatePoints = 0; foreach (var dep in candidate.Dependencies) { if (CanSatisfyDependency(context, dep)) { candidatePoints += 100; } else if (dep.HasDefaultValue) { candidatePoints += 1; } else { candidatePoints = 0; return false; } } return true; }
private static object FastCreateInstance(Type implType, object[] arguments, ConstructorCandidate constructor) { if (constructor == null || constructor.Constructor == null) { throw new ComponentActivatorException( string.Format( "Could not find a public constructor for type {0}. Windsor can not instantiate types that don't expose public constructors. To expose the type as a service add public constructor, or use custom component activator.", implType)); } var instance = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(implType); constructor.Constructor.Invoke(instance, arguments); return instance; }
protected override object CreateInstance(Castle.MicroKernel.Context.CreationContext context, Castle.Core.ConstructorCandidate constructor, object[] arguments) { var component = base.CreateInstance(context, constructor, arguments); AssignViewModel(component, arguments); return(component); }
private void CreateConstructorArgumentsCore(ConstructorCandidate constructor, object[] arguments, CreationContext context) { for (int i = 0; i < constructor.Dependencies.Length; i++) { var dependency = constructor.Dependencies[i]; object value; using (new DependencyTrackingScope(context, Model, constructor.Constructor, dependency)) { value = Kernel.Resolver.Resolve(context, context.Handler, Model, dependency); } arguments[i] = value; } }
/// <summary> /// Adds constructor dependency to this <see cref = "ComponentModel" /> /// </summary> /// <param name = "constructor"></param> public void AddConstructor(ConstructorCandidate constructor) { Constructors.Add(constructor); constructor.Dependencies.ForEach(Dependencies.Add); }
private void CreateConstructorArgumentsCore(ConstructorCandidate constructor, object[] arguments, CreationContext context, Type[] signature) { var index = 0; foreach (var dependency in constructor.Dependencies) { object value; using (new DependencyTrackingScope(context, Model, constructor.Constructor, dependency)) { value = Kernel.Resolver.Resolve(context, context.Handler, Model, dependency); } arguments[index] = value; signature[index++] = dependency.TargetType; } }
protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { // Do like the MVC framework. var instance = Activator.CreateInstance(context.RequestedType); return instance; }
protected virtual object[] CreateConstructorArguments( ConstructorCandidate constructor, CreationContext context, out Type[] signature) { signature = null; if (constructor == null) return null; object[] arguments = new object[constructor.Constructor.GetParameters().Length]; if (arguments.Length == 0) { return null; } signature = new Type[arguments.Length]; int index = 0; foreach (DependencyModel dependency in constructor.Dependencies) { object value; using (new DependencyTrackingScope(context, Model, constructor.Constructor, dependency)) { value = Kernel.Resolver.Resolve(context, context.Handler, Model, dependency); } arguments[index] = value; signature[index++] = dependency.TargetType; } return arguments; }
private static bool BestScoreSoFar(int candidatePoints, int winnerPoints, ConstructorCandidate winnerCandidate) { return winnerCandidate == null || winnerPoints < candidatePoints; }
private static bool BestPossibleScore(ConstructorCandidate candidate, int candidatePoints) { return candidatePoints == candidate.Dependencies.Length*100; }
private object FastCreateInstance(Type implType, object[] arguments, ConstructorCandidate constructor) { if (constructor == null || constructor.Constructor == null) { throw new ComponentActivatorException( string.Format( "Could not find a public constructor for type {0}.{1}" + "Windsor by default cannot instantiate types that don't expose public constructors.{1}" + "To expose the type as a service add public constructor, or use custom component activator.", implType, Environment.NewLine), Model); } var instance = FormatterServices.GetUninitializedObject(implType); constructor.Constructor.Invoke(instance, arguments); return instance; }
protected object CreateInstanceCore(ConstructorCandidate constructor, object[] arguments, Type implType) { object instance; try { #if (SILVERLIGHT) instance = implType.CreateInstance<object>(arguments); #else if (useFastCreateInstance) { instance = FastCreateInstance(implType, arguments, constructor); } else { instance = implType.CreateInstance<object>(arguments); } #endif } catch (Exception ex) { if (arguments != null) { foreach (var argument in arguments) { Kernel.ReleaseComponent(argument); } } if (ex is ComponentActivatorException) { throw; } throw new ComponentActivatorException( "ComponentActivator: could not instantiate " + Model.Implementation.FullName, ex, Model); } return instance; }
protected override object CreateInstance(Castle.MicroKernel.Context.CreationContext context, ConstructorCandidate constructor, object[] arguments) { var component = base.CreateInstance(context, constructor, arguments); AssignDataContext(component, arguments); return component; }
protected virtual object[] CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context) { if (constructor == null) { return null; } var dependencyCount = constructor.Dependencies.Length; if (dependencyCount == 0) { return null; } var arguments = new object[dependencyCount]; try { for (var i = 0; i < dependencyCount; i++) { arguments[i] = Kernel.Resolver.Resolve(context, context.Handler, Model, constructor.Dependencies[i]); } return arguments; } catch { foreach (var argument in arguments) { Kernel.ReleaseComponent(argument); } throw; } }
protected virtual object[] CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context) { if (constructor == null) { return null; } var arguments = new object[constructor.Dependencies.Length]; if (arguments.Length == 0) { return null; } try { CreateConstructorArgumentsCore(constructor, arguments, context); } catch { foreach (var argument in arguments) { if (argument == null) { break; } Kernel.ReleaseComponent(argument); } throw; } return arguments; }
/// <summary> /// Creates the instance. /// </summary> /// <param name = "context"> The context. </param> /// <param name = "constructor"> </param> /// <param name = "arguments"> The arguments. </param> /// <returns> </returns> protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { object instance = null; var implType = Model.Implementation; var createProxy = Model.HasInterceptors; var createInstance = true; if (createProxy) { createInstance = Kernel.ProxyFactory.RequiresTargetInstance(Kernel, Model); } if (createInstance) { try { var currentContext = HttpContext.Current; if (currentContext == null) { throw new InvalidOperationException( "System.Web.HttpContext.Current is null. WebUserControlComponentActivator can only be used in an ASP.Net environment."); } var currentPage = currentContext.Handler as Page; if (currentPage == null) { throw new InvalidOperationException("System.Web.HttpContext.Current.Handler is not of type System.Web.UI.Page"); } var virtualPath = Model.Configuration.Attributes["VirtualPath"]; if (!string.IsNullOrEmpty(virtualPath)) { instance = currentPage.LoadControl(virtualPath); } else { instance = currentPage.LoadControl(implType, arguments); } } catch (Exception ex) { throw new ComponentActivatorException( "WebUserControlComponentActivator: could not instantiate " + Model.Implementation.FullName, ex, Model); } } if (createProxy) { try { instance = Kernel.ProxyFactory.Create(Kernel, instance, Model, context, arguments); } catch (Exception ex) { throw new ComponentActivatorException("ComponentActivator: could not proxy " + Model.Implementation.FullName, ex, Model); } } return instance; }
protected virtual object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { object instance = null; var implType = Model.Implementation; var createProxy = Kernel.ProxyFactory.ShouldCreateProxy(Model); var createInstance = true; if (createProxy == false && Model.Implementation.IsAbstract) { throw new ComponentRegistrationException( string.Format( "Type {0} is abstract.{2} As such, it is not possible to instansiate it as implementation of service '{1}'.", Model.Implementation.FullName, Model.Name, Environment.NewLine)); } if (createProxy) { createInstance = Kernel.ProxyFactory.RequiresTargetInstance(Kernel, Model); } if (createInstance) { try { #if (SILVERLIGHT) instance = ReflectionUtil.CreateInstance<object>(implType, arguments); #else if (useFastCreateInstance) { instance = FastCreateInstance(implType, arguments, constructor); } else { instance = implType.CreateInstance<object>(arguments); } #endif } catch (Exception ex) { if (arguments != null) { foreach (var argument in arguments) { Kernel.ReleaseComponent(argument); } } throw new ComponentActivatorException( "ComponentActivator: could not instantiate " + Model.Implementation.FullName, ex); } } if (createProxy) { try { instance = Kernel.ProxyFactory.Create(Kernel, instance, Model, context, arguments); } catch (Exception ex) { if (arguments != null) { foreach (var argument in arguments) { Kernel.ReleaseComponent(argument); } } throw new ComponentActivatorException("ComponentActivator: could not proxy " + Model.Implementation.FullName, ex); } } return instance; }
internal void SetParentConstructor(ConstructorCandidate ctor) { constructor = ctor; }
protected virtual object[] CreateConstructorArguments( ConstructorCandidate constructor, CreationContext context, out Type[] signature) { signature = null; if (constructor == null) { return null; } var arguments = new object[constructor.Constructor.GetParameters().Length]; if (arguments.Length == 0) { return null; } try { signature = new Type[arguments.Length]; CreateConstructorArgumentsCore(constructor, arguments, context, signature); } catch { foreach (var argument in arguments) { if (argument == null) { break; } Kernel.ReleaseComponent(argument); } throw; } return arguments; }
protected bool CanSatisfyConstructor(ConstructorCandidate constructor) { return constructor.Dependencies.All(CanSatisfyDependency); }