Exemple #1
0
        public T Convert(ResolutionContext context)
        {
            if (context.SourceValue == null)
            {
                return(null);
            }
            T result = (T)BaseTypeSafeEnum.Parse((string)context.SourceValue, context.DestinationType);

            return(result);
        }
        private static Func <string, object> GetParseFunction(Type typeToParseTo)
        {
            if (typeof(BaseTypeSafeEnum).IsAssignableFrom(typeToParseTo))
            {
                return(s => BaseTypeSafeEnum.Parse(s, typeToParseTo));
            }

            Type nullableUnderlying = Nullable.GetUnderlyingType(typeToParseTo);

            if (nullableUnderlying != null)
            {
                Func <string, object> func = GetParseFunction(nullableUnderlying);
                return(s => TryConvert(s, func));
            }

            MethodInvoker methodDelegate = typeToParseTo.DelegateForCallMethod("Parse", typeof(string));

            return(s => UnwrapInvokeExceptionOfStaticMethod(methodDelegate, new object[] { s }));
        }
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (!typeof(BaseTypeSafeEnum).IsAssignableFrom(bindingContext.ModelType))
            {
                return(false);
            }

            ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (value == null || string.IsNullOrEmpty(value.AttemptedValue))
            {
                return(false);
            }

            object model;

            if (!BaseTypeSafeEnum.TryParse(value.AttemptedValue, bindingContext.ModelType, out model))
            {
                return(false);
            }
            bindingContext.Model = model;
            return(true);
        }