コード例 #1
0
		public Task<MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
		{
			context.ThrowIfNull("request");
			type.ThrowIfNull("type");
			method.ThrowIfNull("method");
			parameter.ThrowIfNull("parameter");

			Type parameterType = parameter.ParameterType;
			var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
			string json = reader.ReadToEnd();
			object jsonModel;

			try
			{
				jsonModel = JsonConvert.DeserializeObject(json, parameterType, _serializerSettings);
			}
			catch (Exception exception)
			{
				if (_errorHandling == DataConversionErrorHandling.ThrowException)
				{
					throw new ApplicationException(String.Format("Request content could not be deserialized to '{0}'.", parameterType.FullName), exception);
				}
				jsonModel = parameterType.GetDefaultValue();
			}

			return MapResult.ValueMapped(jsonModel).AsCompletedTask();
		}
コード例 #2
0
        public Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
        {
            context.ThrowIfNull("context");
            parameterType.ThrowIfNull("parameterType");

            return (parameterType == typeof(HttpServerUtilityBase)).AsCompletedTask();
        }
コード例 #3
0
        public bool CanMapType(HttpRequestBase request, Type parameterType)
        {
            request.ThrowIfNull("request");
            parameterType.ThrowIfNull("parameterType");

            return true;
        }
コード例 #4
0
		public async Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
		{
			context.ThrowIfNull("context");
			parameterType.ThrowIfNull("parameterType");

			return context.Request.ContentType == "application/json" && await Task.Run(() => _parameterTypeMatchDelegate(parameterType));
		}
コード例 #5
0
ファイル: RandomIdMapper.cs プロジェクト: dblchu/JuniorRoute
        public IdResult Map(Type type, MethodInfo method)
        {
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");

            return IdResult.IdMapped(_guidFactory.Random());
        }
コード例 #6
0
        public Task<bool> MustAuthenticateAsync(Type type, MethodInfo method)
        {
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");

            return method.GetCustomAttributes(typeof(AuthenticateAttribute), false).Any().AsCompletedTask();
        }
コード例 #7
0
        public bool MustAuthenticate(Type type, MethodInfo method)
        {
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");

            return method.GetCustomAttributes(typeof(AuthenticateAttribute), false).Any();
        }
コード例 #8
0
		public async Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
		{
			context.ThrowIfNull("context");
			parameterType.ThrowIfNull("parameterType");

			return await Task.Run(() => _parameterTypeMatchDelegate(parameterType));
		}
コード例 #9
0
        public override Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
        {
            context.ThrowIfNull("context");
            parameterType.ThrowIfNull("parameterType");

            return parameterType.ImplementsInterface<IConvertible>().AsCompletedTask();
        }
コード例 #10
0
ファイル: TypeExtensions.cs プロジェクト: zacharyyates/yak
        public static bool IsNonGenericImplementationOf(this Type type, Type interfaceType)
        {
            type.ThrowIfNull("type");
            interfaceType.ThrowIfNull("interfaceType");

            return type.GetInterfaces().Any(interfaceType.Equals);
        }
コード例 #11
0
        public Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
        {
            context.ThrowIfNull("context");
            parameterType.ThrowIfNull("parameterType");

            return true.AsCompletedTask();
        }
コード例 #12
0
        public CustomMapperAttribute(Type customMapperType)
        {
            customMapperType.ThrowIfNull("customMapperType");

            if (customMapperType.IsNotPublic)
            {
                throw new ArgumentException("Type must be public.", "customMapperType");
            }
            if (customMapperType.IsAbstract)
            {
                throw new ArgumentException("Type cannot be abstract or static.", "customMapperType");
            }
            if (!customMapperType.ImplementsInterface<ICustomMapper>())
            {
                throw new ArgumentException(String.Format("Type must implement {0}.", customMapperType.FullName), "customMapperType");
            }

            ConstructorInfo constructorInfo = customMapperType.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null);

            if (constructorInfo == null)
            {
                throw new ArgumentException("Type must declare a public default constructor.", "customMapperType");
            }

            _mapper = (ICustomMapper)Activator.CreateInstance(customMapperType);
        }
コード例 #13
0
        public bool CanMapType(HttpRequestBase request, Type parameterType)
        {
            request.ThrowIfNull("request");
            parameterType.ThrowIfNull("parameterType");

            return parameterType.ImplementsInterface<IConvertible>();
        }
コード例 #14
0
		public AspNetDiagnosticConfiguration(
			Type cacheType,
			IEnumerable<Type> requestFilterTypes,
			IEnumerable<Type> responseGeneratorTypes,
			IEnumerable<Type> responseHandlerTypes,
			IEnumerable<Type> errorHandlerTypes,
			Type antiCsrfCookieManagerType = null,
			Type antiCsrfNonceValidatorType = null,
			Type antiCsrfResponseGeneratorType = null)
		{
			cacheType.ThrowIfNull("cacheType");
			requestFilterTypes.ThrowIfNull("requestFilterTypes");
			responseGeneratorTypes.ThrowIfNull("responseGeneratorTypes");
			responseHandlerTypes.ThrowIfNull("responseHandlerTypes");
			errorHandlerTypes.ThrowIfNull("errorHandlerTypes");

			_cacheType = cacheType;
			_requestFilterTypes = requestFilterTypes.ToArray();
			_responseGeneratorTypes = responseGeneratorTypes.ToArray();
			_responseHandlerTypes = responseHandlerTypes.ToArray();
			_errorHandlerTypes = errorHandlerTypes.ToArray();
			_antiCsrfCookieManagerType = antiCsrfCookieManagerType;
			_antiCsrfNonceValidatorType = antiCsrfNonceValidatorType;
			_antiCsrfResponseGeneratorType = antiCsrfResponseGeneratorType;
		}
コード例 #15
0
ファイル: AspNetView.cs プロジェクト: kelong/JuniorRoute
		public void Populate(
			Type cacheType,
			IEnumerable<Type> requestFilterTypes,
			IEnumerable<Type> responseGeneratorTypes,
			IEnumerable<Type> responseHandlerTypes,
			IEnumerable<Type> errorHandlerTypes,
			Type antiCsrfCookieManagerType,
			Type antiCsrfNonceValidatorType,
			Type antiCsrfResponseGeneratorType)
		{
			cacheType.ThrowIfNull("cacheType");
			requestFilterTypes.ThrowIfNull("requestFilterTypes");
			responseGeneratorTypes.ThrowIfNull("responseGeneratorTypes");
			responseHandlerTypes.ThrowIfNull("responseHandlerTypes");
			errorHandlerTypes.ThrowIfNull("errorHandlerTypes");

			CacheType = cacheType;
			RequestFilterTypes = requestFilterTypes;
			ResponseGeneratorTypes = responseGeneratorTypes;
			ResponseHandlerTypes = responseHandlerTypes;
			ErrorHandlerTypes = errorHandlerTypes;
			AntiCsrfCookieManagerType = antiCsrfCookieManagerType;
			AntiCsrfNonceValidatorType = antiCsrfNonceValidatorType;
			AntiCsrfResponseGeneratorType = antiCsrfResponseGeneratorType;
		}
コード例 #16
0
ファイル: ModelMapper.cs プロジェクト: dblchu/JuniorRoute
        public bool CanMapType(HttpRequestBase request, Type parameterType)
        {
            request.ThrowIfNull("request");
            parameterType.ThrowIfNull("parameterType");

            return _parameterTypeMatchDelegate(parameterType);
        }
コード例 #17
0
		public async Task<MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
		{
			context.ThrowIfNull("context");
			type.ThrowIfNull("type");
			method.ThrowIfNull("method");
			parameter.ThrowIfNull("parameter");

			Type parameterType = parameter.ParameterType;
			object model = _container != null ? _container.GetInstance(parameterType) : Activator.CreateInstance(parameterType);
			Type modelType = model.GetType();

			foreach (PropertyInfo property in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
			{
				object mappedValue = await GetMappedValueAsync(context, modelType, property);

				if (mappedValue == null)
				{
					throw new ApplicationException(String.Format("Unable to map property '{0} {1}' of type '{2}'.", property.PropertyType.FullName, property.Name, modelType.FullName));
				}

				property.SetValue(model, mappedValue, null);
			}

			return MapResult.ValueMapped(model);
		}
コード例 #18
0
ファイル: JsonModelMapper.cs プロジェクト: dblchu/JuniorRoute
        public bool CanMapType(HttpRequestBase request, Type parameterType)
        {
            request.ThrowIfNull("request");
            parameterType.ThrowIfNull("parameterType");

            return request.ContentType == "application/json" && _parameterTypeMatchDelegate(parameterType);
        }
コード例 #19
0
        public MapResult Map(HttpRequestBase request, Type modelType, PropertyInfo property)
        {
            request.ThrowIfNull("request");
            modelType.ThrowIfNull("modelType");
            property.ThrowIfNull("property");

            return MapResult.ValueMapped(property.PropertyType.GetDefaultValue());
        }
コード例 #20
0
        protected override Task<MapResult> OnMapAsync(HttpContextBase context, string value, Type parameterType)
        {
            context.ThrowIfNull("context");
            value.ThrowIfNull("value");
            parameterType.ThrowIfNull("parameterType");

            return MapResult.ValueMapped(((IConvertible)value).ToType(parameterType, CultureInfo.InvariantCulture)).AsCompletedTask();
        }
コード例 #21
0
		public Task<MapResult> MapAsync(HttpRequestBase request, Type modelType, PropertyInfo property)
		{
			request.ThrowIfNull("request");
			modelType.ThrowIfNull("modelType");
			property.ThrowIfNull("property");

			return MapResult.ValueMapped(property.PropertyType.GetDefaultValue()).AsCompletedTask();
		}
コード例 #22
0
		public async Task<IEnumerable<object>> GetParameterValuesAsync(HttpContextBase context, Type type, MethodInfo method)
		{
			context.ThrowIfNull("context");
			type.ThrowIfNull("type");
			method.ThrowIfNull("method");

			ParameterInfo[] parameterInfos = method.GetParameters();
			var parameterValues = new List<object>();

			foreach (ParameterInfo parameterInfo in parameterInfos)
			{
				Type parameterType = parameterInfo.ParameterType;
				string parameterName = parameterInfo.Name;
				Type currentParameterType = parameterType;

				do
				{
					bool mapped = false;

					foreach (IParameterMapper parameterMapper in _parameterMappers)
					{
						if (!await parameterMapper.CanMapTypeAsync(context, parameterType))
						{
							continue;
						}
						MapResult mapResult = await parameterMapper.MapAsync(context, type, method, parameterInfo);

						if (mapResult.ResultType == MapResultType.ValueNotMapped)
						{
							continue;
						}

						parameterValues.Add(mapResult.Value);
						mapped = true;
						break;
					}
					if (mapped)
					{
						break;
					}

					currentParameterType = currentParameterType.BaseType;
				} while (currentParameterType != null);

				if (currentParameterType == null)
				{
					throw new ApplicationException(
						String.Format(
							"No request parameter mapper was found for parameter '{0} {1}' of {2}.{3}.",
							parameterType.FullName,
							parameterName,
							type.FullName,
							method.Name));
				}
			}

			return parameterValues;
		}
コード例 #23
0
ファイル: NoContentMapper.cs プロジェクト: dblchu/JuniorRoute
        public void Map(Func<IContainer> container, Type type, MethodInfo method, Routing.Route route)
        {
            container.ThrowIfNull("container");
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");
            route.ThrowIfNull("route");

            route.RespondWithNoContent();
        }
コード例 #24
0
ファイル: TypeExtensions.cs プロジェクト: zacharyyates/yak
        public static bool IsImplementationOf(this Type type, Type interfaceType)
        {
            type.ThrowIfNull("type");
            interfaceType.ThrowIfNull("interfaceType");

            return
                IsNonGenericImplementationOf(type, interfaceType) ||
                IsGenericImplementationOf(type, interfaceType);
        }
コード例 #25
0
ファイル: TypeExtensions.cs プロジェクト: zacharyyates/yak
        public static bool IsGenericImplementationOf(this Type type, Type interfaceType)
        {
            type.ThrowIfNull("type");
            interfaceType.ThrowIfNull("interfaceType");

            return type.GetInterfaces().Any(i =>
                i.IsGenericType &&
                i.GetGenericTypeDefinition() == interfaceType);
        }
コード例 #26
0
        public MapResult Map(HttpRequestBase request, Type type, MethodInfo method, ParameterInfo parameter)
        {
            request.ThrowIfNull("request");
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");
            parameter.ThrowIfNull("parameter");

            return MapResult.ValueMapped(parameter.ParameterType.GetDefaultValue());
        }
コード例 #27
0
ファイル: DependsOnAttribute.cs プロジェクト: msusur/Crowfx
 /// <summary>
 /// Initializes a new instance of <see cref="DependsOnAttribute"/>.
 /// </summary>
 public DependsOnAttribute(Type dependencyType)
 {
     dependencyType.ThrowIfNull("dependencyType");
     if (!typeof(IModule).IsAssignableFrom(dependencyType))
     {
         throw new ArgumentException("DependsOn attribute must be depends on a class that implements the IStartupInstaller.");
     }
     DependencyType = dependencyType;
 }
コード例 #28
0
        public Task<MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
        {
            context.ThrowIfNull("context");
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");
            parameter.ThrowIfNull("parameter");

            return MapResult.ValueMapped(context.Server).AsCompletedTask();
        }
コード例 #29
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="TypeMemberSelectionRule" /> class.
        /// </summary>
        /// <param name="type">The type to compare with the type of given members.</param>
        /// <param name="selectionMode">The selection mode to apply.</param>
        /// <param name="compareMode">The compare mode to apply.</param>
        /// <param name="name">The name of the rule.</param>
        /// <param name="description">The description of the rule.</param>
        public TypeMemberSelectionRule( Type type, MemberSelectionMode selectionMode, CompareMode compareMode, String name = null, String description = null )
            : base(name, description)
        {
            type.ThrowIfNull( nameof( type ) );

            _type = type;
            _selectionMode = selectionMode;
            _compareMode = compareMode;
        }
コード例 #30
0
        public Task<NameResult> MapAsync(Type type, MethodInfo method)
        {
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");

            NameAttribute attribute = method.GetCustomAttributes(typeof(NameAttribute), false).Cast<NameAttribute>().SingleOrDefault();

            return (attribute != null ? NameResult.NameMapped(attribute.Name) : NameResult.NameNotMapped()).AsCompletedTask();
        }