コード例 #1
0
        internal static object CreateInstance([NotNull] Type type, [NotNull] ResolutionFunction resolutionFunction)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (resolutionFunction == null)
            {
                throw new ArgumentNullException(nameof(resolutionFunction));
            }

            if (TryInstantiateViaBakedData(type, resolutionFunction, out var bakedDataInstance))
            {
                return(bakedDataInstance);
            }

            if (DiSettings.TryGetInstance(out var settings) && settings.UseBakedData)
            {
                object PocoResolutionFunction(Type resolvedType)
                {
                    if (resolutionFunction(resolvedType, out var resolvedObject))
                    {
                        return(resolvedObject);
                    }
                    throw new DependencyNotRegisteredException(resolvedType);
                }

                if (BakedInjection.TryInstantiate(type, PocoResolutionFunction, out var bakedInstance))
                {
                    return(bakedInstance);
                }
            }

            if (!TryGetInjectableConstructorParameters(type, out var parameters))
            {
                throw new ArgumentException($"Type {type} does not have an injectable constructor.");
            }

            var arguments = Injection.RentArgumentsArray(parameters.Count);

            for (var index = 0; index < parameters.Count; index++)
            {
                var parameterType = parameters[index].ParameterType;
                if (resolutionFunction(parameterType, out var dependency))
                {
                    arguments[index] = dependency;
                }
                else
                {
                    Injection.ReturnArgumentsArray(arguments);
                    throw new DependencyNotRegisteredException(parameterType);
                }
            }

            var instance = Activator.CreateInstance(type, arguments);

            Injection.ReturnArgumentsArray(arguments);
            return(instance);
        }
コード例 #2
0
ファイル: BakedInjection.cs プロジェクト: Delt06/di-framework
        /// <summary>
        /// Try inject using baked data.
        /// </summary>
        /// <param name="component">Component to inject dependencies to.</param>
        /// <param name="resolutionFunction">Resolution provider.</param>
        /// <returns>True if baked and injected, false otherwise.</returns>
        /// <exception cref="ArgumentNullException">If any of arguments are null.</exception>
        public static bool TryInject([NotNull] MonoBehaviour component, [NotNull] ResolutionFunction resolutionFunction)
        {
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }
            if (resolutionFunction == null)
            {
                throw new ArgumentNullException(nameof(resolutionFunction));
            }

            if (!BakedInjectionFunctions.TryGetValue(component.GetType(), out var injectionFunction))
            {
                return(false);
            }

            injectionFunction(component, resolutionFunction);
            return(true);
        }
コード例 #3
0
        private static bool TryInstantiateViaBakedData([NotNull] Type type,
                                                       [NotNull] ResolutionFunction resolutionFunction, out object instance)
        {
            if (DiSettings.TryGetInstance(out var settings) && settings.UseBakedData)
            {
                object PocoResolutionFunction(Type resolvedType)
                {
                    if (resolutionFunction(resolvedType, out var resolvedObject))
                    {
                        return(resolvedObject);
                    }
                    throw new DependencyNotRegisteredException(resolvedType);
                }

                if (BakedInjection.TryInstantiate(type, PocoResolutionFunction, out instance))
                {
                    return(true);
                }
            }

            instance = default;
            return(false);
        }
コード例 #4
0
    /// <summary>
    /// Get curve points to draw an interpolation curve between the abscissa values xlo and xhi.
    /// It calls the virtual methods MpCurveBase::GetXOfU() and GetYOfU() to obtain the
    /// interpolation values. Note, that before method DrawCurve() can be called
    /// the method Interpolate() must have been called. Otherwise, not interpolation
    /// is available.
    /// </summary>
    /// <param name="xlo">Lower bound of the drawing range.</param>
    /// <param name="xhi">Upper bound of the drawing range.</param>
    /// <param name="getresolution">A delegate that must provide the points necessary to draw a smooth curve between to points.</param>
    /// <param name="setpoint">A delegate which is called with each calculated point. Can be used to draw the curve. </param>
    public void GetCurvePoints (double xlo, double xhi, ResolutionFunction getresolution, PointSink setpoint)
    {
      // nothing to draw if zero or one element
      if (x.Length < 2)
        return;

      // Find index of the element in the abscissa vector x, that is smaller
      // than the lower (upper) value xlo (xhi) of the drawing range. If xlo is
      // smaller than the lowest abscissa value the lowest index minus one is
      // returned.
      int i_lo = FindInterval(xlo,x),
        i_hi = FindInterval(xhi,x);

      // Interpolation values for the boundaries of the drawing range [xlo,xhi]
      double ylo = GetYOfU(xlo),
        yhi = GetYOfU(xhi);

      int k;
      double x0,t,delta;

      setpoint(xlo,ylo, false);
      k = getresolution(xlo,ylo, x[i_lo+1],y[i_lo+1]);
      delta = (x[i_lo+1] - xlo) / k;
      for (int j = 0; j < k; j++) 
      {
        t = xlo + j * delta;
        setpoint( GetXOfU(t), GetYOfU(t), false );
      }

      for (int i = i_lo+1; i < i_hi; i++) 
      {
        x0 = x[i];
        k = getresolution(x0,y[i],x[i+1],y[i+1]);
        delta = (x[i+1]-x0) / k;
        for (int j = 0; j < k; j++) 
        {
          t = x0 + j * delta;
          setpoint( GetXOfU(t), GetYOfU(t), false );
        }
      }

      x0 = x[i_hi];
      k = getresolution(x0,y[i_hi],xhi,yhi);
      delta = (xhi - x0) / k;
      for (int j = 0; j < k; j++) 
      {
        t = x0 + j * delta;
        setpoint( GetXOfU(t), GetYOfU(t), false );
      }  

      // don't forget last point
      setpoint(xhi,yhi, true);
    }
コード例 #5
0
 internal ContainerBuilder([NotNull] ResolutionFunction resolutionFunction) => _resolutionFunction =