Пример #1
0
        protected override object VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
        {
            object[] parameterValues;
            if (constructorCallSite.ParameterCallSites.Length == 0)
            {
                parameterValues = Array.Empty <object>();
            }
            else
            {
                parameterValues = new object[constructorCallSite.ParameterCallSites.Length];
                for (var index = 0; index < parameterValues.Length; index++)
                {
                    parameterValues[index] = VisitCallSite(constructorCallSite.ParameterCallSites[index], context);
                }
            }

#if NETCOREAPP3_0
            return(constructorCallSite.ConstructorInfo.Invoke(BindingFlags.DoNotWrapExceptions, binder: null, parameters: parameterValues, culture: null));
#else
            try
            {
                return(constructorCallSite.ConstructorInfo.Invoke(parameterValues));
            }
            catch (Exception ex) when(ex.InnerException != null)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                // The above line will always throw, but the compiler requires we throw explicitly.
                throw;
            }
#endif
        }
Пример #2
0
        protected override Expression VisitConstructor(ConstructorCallSite callSite, object?context)
        {
            ParameterInfo[] parameters = callSite.ConstructorInfo.GetParameters();
            Expression[]    parameterExpressions;
            if (callSite.ParameterCallSites.Length == 0)
            {
                parameterExpressions = Array.Empty <Expression>();
            }
            else
            {
                parameterExpressions = new Expression[callSite.ParameterCallSites.Length];
                for (int i = 0; i < parameterExpressions.Length; i++)
                {
                    parameterExpressions[i] = Convert(VisitCallSite(callSite.ParameterCallSites[i], context), parameters[i].ParameterType);
                }
            }

            Expression expression = Expression.New(callSite.ConstructorInfo, parameterExpressions);

            if (callSite.ImplementationType !.IsValueType)
            {
                expression = Expression.Convert(expression, typeof(object));
            }
            return(expression);
        }
        protected override object VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
        {
            object[] parameterValues;
            if (constructorCallSite.ParameterCallSites.Length == 0)
            {
                parameterValues = Array.Empty <object>();
            }
            else
            {
                //如果当前构造器参数不为空,则实例化每一个参数的实例对象
                parameterValues = new object[constructorCallSite.ParameterCallSites.Length];
                for (var index = 0; index < parameterValues.Length; index++)
                {
                    parameterValues[index] = VisitCallSite(constructorCallSite.ParameterCallSites[index], context);
                }
            }

            try
            {
                //根据参数对象进行实例化对象并返回
                return(constructorCallSite.ConstructorInfo.Invoke(parameterValues));
            }
            catch (Exception ex) when(ex.InnerException != null)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                // The above line will always throw, but the compiler requires we throw explicitly.
                throw;
            }
        }
Пример #4
0
        protected override object VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
        {
            object[] parameterValues;
            if (constructorCallSite.ParameterCallSites.Length == 0)
            {
                parameterValues = new object[0];
            }
            else
            {
                parameterValues = new object[constructorCallSite.ParameterCallSites.Length];
                for (var index = 0; index < parameterValues.Length; index++)
                {
                    parameterValues[index] = VisitCallSite(constructorCallSite.ParameterCallSites[index], context);
                }
            }

            try
            {
                return(constructorCallSite.ConstructorInfo.Invoke(parameterValues));
            }
            catch (Exception ex) when(ex.InnerException != null)
            {
#if NET45_OR_GREATER
                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
#endif
                // The above line will always throw, but the compiler requires we throw explicitly.
                throw;
            }
        }
Пример #5
0
        protected override object VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
        {
            object[] parameterValues = new object[constructorCallSite.ParameterCallSites.Length];
            for (var index = 0; index < parameterValues.Length; index++)
            {
                parameterValues[index] = VisitCallSite(constructorCallSite.ParameterCallSites[index], scope);
            }

            try
            {
                return(constructorCallSite.ConstructorInfo.Invoke(parameterValues));
            }
            catch (Exception ex) when(ex.InnerException != null)
            {
                try
                {
                    System.Diagnostics.Debugger.Break();
                    return(constructorCallSite.ConstructorInfo.Invoke(parameterValues));
                }
                catch (Exception ex1) when(ex1.InnerException != null)
                {
                    ExceptionDispatchInfo.Capture(ex1.InnerException).Throw();
                    // The above line will always throw, but the compiler requires we throw explicitly.
                    throw;
                }
                //ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                //// The above line will always throw, but the compiler requires we throw explicitly.
                //throw;
            }
        }
Пример #6
0
        protected override Expression VisitConstructor(ConstructorCallSite callSite, ParameterExpression provider)
        {
            var parameters = callSite.ConstructorInfo.GetParameters();

            return(Expression.New(
                       callSite.ConstructorInfo,
                       callSite.ParameterCallSites.Select((c, index) =>
                                                          Expression.Convert(VisitCallSite(c, provider), parameters[index].ParameterType))));
        }
        protected override Expression VisitConstructor(ConstructorCallSite callSite, CallSiteExpressionBuilderContext context)
        {
            var parameters = callSite.ConstructorInfo.GetParameters();

            return(Expression.New(
                       callSite.ConstructorInfo,
                       callSite.ParameterCallSites.Select((c, index) =>
                                                          Convert(VisitCallSite(c, context), parameters[index].ParameterType))));
        }
Пример #8
0
 protected override Expression VisitConstructor(ConstructorCallSite constructorCallSite, ILEmitResolverBuilderContext argument)
 {
     // new T([create arguments])
     foreach (var parameterCallSite in constructorCallSite.ParameterCallSites)
     {
         VisitCallSite(parameterCallSite, argument);
     }
     argument.Generator.Emit(OpCodes.Newobj, constructorCallSite.ConstructorInfo);
     return(null);
 }
Пример #9
0
        protected override Type?VisitConstructor(ConstructorCallSite constructorCallSite, CallSiteValidatorState state)
        {
            Type?result = null;

            foreach (ServiceCallSite parameterCallSite in constructorCallSite.ParameterCallSites)
            {
                Type?scoped = VisitCallSite(parameterCallSite, state);
                result ??= scoped;
            }
            return(result);
        }
        protected override Expression VisitConstructor(ConstructorCallSite callSite, CallSiteExpressionBuilderContext context)
        {
            var parameters           = callSite.ConstructorInfo.GetParameters();
            var parameterExpressions = new Expression[callSite.ParameterCallSites.Length];

            for (int i = 0; i < parameterExpressions.Length; i++)
            {
                parameterExpressions[i] = Convert(VisitCallSite(callSite.ParameterCallSites[i], context), parameters[i].ParameterType);
            }
            return(Expression.New(callSite.ConstructorInfo, parameterExpressions));
        }
Пример #11
0
        protected override Type VisitConstructor(ConstructorCallSite constructorCallSite, CallSiteValidatorState state)
        {
            Type result = null;

            foreach (var parameterCallSite in constructorCallSite.ParameterCallSites)
            {
                var scoped = VisitCallSite(parameterCallSite, state);
                if (result == null)
                {
                    result = scoped;
                }
            }
            return(result);
        }
Пример #12
0
        protected override object VisitConstructor(ConstructorCallSite constructorCallSite, ILEmitResolverBuilderContext argument)
        {
            // new T([create arguments])
            foreach (ServiceCallSite parameterCallSite in constructorCallSite.ParameterCallSites)
            {
                VisitCallSite(parameterCallSite, argument);
                if (parameterCallSite.ServiceType.IsValueType)
                {
                    argument.Generator.Emit(OpCodes.Unbox_Any, parameterCallSite.ServiceType);
                }
            }

            argument.Generator.Emit(OpCodes.Newobj, constructorCallSite.ConstructorInfo);
            return(null);
        }
Пример #13
0
 protected override Expression VisitConstructor(ConstructorCallSite callSite, object context)
 {
     ParameterInfo[] parameters = callSite.ConstructorInfo.GetParameters();
     Expression[]    parameterExpressions;
     if (callSite.ParameterCallSites.Length == 0)
     {
         parameterExpressions = Array.Empty <Expression>();
     }
     else
     {
         parameterExpressions = new Expression[callSite.ParameterCallSites.Length];
         for (int i = 0; i < parameterExpressions.Length; i++)
         {
             parameterExpressions[i] = Convert(VisitCallSite(callSite.ParameterCallSites[i], context), parameters[i].ParameterType);
         }
     }
     return(Expression.New(callSite.ConstructorInfo, parameterExpressions));
 }
Пример #14
0
 protected abstract TResult VisitConstructor(ConstructorCallSite constructorCallSite, TArgument argument);
Пример #15
0
 private static IEnumerable <Type> GetParameters(ConstructorCallSite constructorCallSite) =>
 constructorCallSite
 .Constructor
 .GetParameters()
 .Select(p => p.ParameterType);
Пример #16
0
 private static IEnumerable<Type> GetParameters(ConstructorCallSite constructorCallSite) =>
     constructorCallSite
         .Constructor
         .GetParameters()
         .Select(p => p.ParameterType);