Beispiel #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 }));
        }