Exemplo n.º 1
0
        private static Hub TryGetHub(HttpContext httpContext, Type hubType)
        {
            Hub                 hub           = null;
            IValueProvider      valueProvider = ValueProviderFactories.Factories.GetValueProvider(httpContext);
            IModelBinderInvoker binderInvoker = new DefaultModelBinderInvoker();

            foreach (var constructor in hubType.GetConstructors(
                         BindingFlags.CreateInstance |
                         BindingFlags.Instance |
                         BindingFlags.Public)
                     .OrderByDescending(x => x.GetParameters().Length))
            {
                var parameters = constructor.GetParameters();
                if (parameters == null || parameters.Length == 0)
                {
                    hub = constructor.Invoke(null) as Hub;
                    break;
                }
                else
                {
                    var           index           = 0;
                    List <object> parameterValues = new List <object>();
                    for (; index < parameters.Length; index++)
                    {
                        if (binderInvoker.TryBindModel(httpContext, valueProvider, parameters[index], out var value))
                        {
                            parameterValues.Add(value);
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (index == parameters.Length)
                    {
                        hub = constructor.Invoke(parameterValues.ToArray()) as Hub;
                        break;
                    }
                }
            }

            return(hub);
        }
        internal bool TryBindingParameter(Controller controller, out ParameterDescriptor[] paramDescriptors)
        {
            int index;

            paramDescriptors = GetParameters(controller.ControllerContext);

            if (paramDescriptors == null)
            {
                return(false);
            }

            if (paramDescriptors.Length == 0)
            {
                controller.ControllerContext.ParameterDescriptors = null;
                return(true);
            }

            IModelBinderInvoker binderInvoker = new DefaultModelBinderInvoker();
            IValueProvider      valueProvider = ValueProviderFactories.Factories.GetValueProvider(controller.HttpContext);

            for (index = 0; index < paramDescriptors.Length; index++)
            {
                if (binderInvoker.TryBindModel(controller.HttpContext, valueProvider, paramDescriptors[index].ParameterInfo, out var value))
                {
                    paramDescriptors[index].ParameterValue = value;
                }
                else
                {
                    break;
                }

                //if (!String.IsNullOrWhiteSpace(controller.ControllerContext.RouteEntity.ParameterValue) &&
                //    String.Equals(controller.ControllerContext.RouteEntity.DefaultParameter,
                //    paramDescriptors[index].ParameterName,
                //    StringComparison.OrdinalIgnoreCase))
                //{
                //    if (controller.ControllerContext.RouteEntity.ParameterValue != null)
                //    {
                //        TypeConverter converter = TypeDescriptor.GetConverter(paramDescriptor.ParameterType);
                //        try
                //        {
                //            var value = converter.ConvertTo(controller.ControllerContext.RouteEntity.ParameterValue, paramDescriptor.ParameterType);
                //            paramDescriptors[index].ParameterValue = value;
                //            continue;
                //        }
                //        catch
                //        {

                //        }
                //    }
                //}

                //var binderProviders = Services.GetRequiredService<IModelBinderProvider>();
                //var modelBinder = binderProviders.GetBinder(paramDescriptor.ParameterType);
                //var modelMetadata = Services.GetRequiredService<IModelMetadataProvider>().GetMetadataForType(null, paramDescriptor.ParameterType);
                ////var modelBinderModelBinders.Binders.GetBinder(paramDescriptor.ParameterType);

                //if (TryBindModel(modelBinder, modelMetadata, controller, "", out var paramValue))
                //{
                //    paramDescriptors[index].ParameterValue = paramValue;
                //    continue;
                //}

                //if (TryBindModel(modelBinder, modelMetadata, controller, paramDescriptor.ParameterName, out paramValue))
                //{
                //    paramDescriptors[index].ParameterValue = paramValue;
                //    continue;
                //}

                //// 尝试依赖注入
                //modelBinder = binderProviders.GetBinder(typeof(IServiceProvider));

                //if (TryBindModel(modelBinder, modelMetadata, controller, "", out paramValue))
                //{

                //    paramDescriptors[index].ParameterValue = paramValue;
                //    continue;
                //}

                //if (DefaultParamterValue.TryGetDefaultValue(paramDescriptor.ParameterInfo, out paramValue))
                //{
                //    // 注:默认值可能为Null
                //    paramDescriptors[index].ParameterValue = paramValue;
                //    continue;
                //}
                //else if(TypeHelper.IsNullableValueType(modelMetadata.ModelType))
                //{
                //    paramDescriptors[index].ParameterValue = null;
                //    continue;
                //}
                //else
                //{
                //    break;
                //}
            }

            if (index < paramDescriptors.Length)
            {
                return(false);
            }

            controller.ModelState.ModelWrappers = paramDescriptors
                                                  .Select(param => new ModelWrapper
            {
                ModelName = param.ParameterName,
                Model     = param.ParameterValue,
                ModelType = param.ParameterType
            });

            return(true);
        }