private static void BailOutIfEqualNumberOfArguments(Type type, ConstructorCandidate first, ConstructorCandidate second) { if (first.Dependencies.Count == second.Dependencies.Count) { throw new InvalidOperationException("Two constructors with equal number of arguments on " + type); } }
private static List<ConstructorCandidate> DetermineCandidates(Type type) { List<ConstructorCandidate> candidates = new List<ConstructorCandidate>(); foreach (ConstructorInfo ctor in type.GetConstructors()) { ConstructorCandidate candidate = new ConstructorCandidate(ctor); foreach (ParameterInfo parameter in ctor.GetParameters()) { candidate.AddParameterDependency(new ServiceDependency(parameter.ParameterType, DependencyType.Constructor, parameter.Name)); } candidates.Add(candidate); } return candidates; }
protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { // Do like the MVC framework. var instance = Activator.CreateInstance(context.RequestedType); return instance; }
static AddConstructorParametersCodeAction GetOptionalContructorParametersCodeAction(Document document, ConstructorCandidate constructorCandidate, INamedTypeSymbol containingType, bool useSubMenuName) { var missingOptionalParameters = constructorCandidate.MissingParameters.SelectAsArray( p => CodeGenerationSymbolFactory.CreateParameterSymbol( attributes: default,
protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { return(base.CreateInstance(context, constructor, arguments)); }
private static bool BestPossibleScore(ConstructorCandidate candidate, int candidatePoints) { return(candidatePoints == candidate.Dependencies.Length * 100); }
private static bool BestScoreSoFar(int candidatePoints, int winnerPoints, ConstructorCandidate winnerCandidate) { return(winnerCandidate == null || winnerPoints < candidatePoints); }
private static ConstructorCandidate ChooseOneWithMoreArguments(ConstructorCandidate first, ConstructorCandidate second) { return first.Dependencies.Count > second.Dependencies.Count ? first : second; }
/// <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}'. Did you forget to proxy it?", 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); }
protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { return base.CreateInstance(context, constructor, arguments); }
protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) { var component = base.CreateInstance(context, constructor, arguments); return(component); }
public virtual object CreateObject(ConstructorCandidate constructor, object[] parameters) { return constructor.RuntimeInfo.Invoke(parameters); }