Source/NFX/StringValueConversion.cs
/// <summary>
/// Tries to get a string value as specified type.
/// When 'strict=false', tries to do some inference like return "true" for numbers that dont equal to zero etc.
/// When 'strict=true' throws an exception if deterministic conversion is not possible
/// </summary>
Tries to get a string value as specified type.
When 'strict=false', tries to do some inference like return "true" for numbers that dont equal to zero etc.
When 'strict=true' throws an exception if deterministic conversion is not possible
Tries to get a string value as specified type . When ' strict = false ' tries to do some inference like return " true " for numbers that dont equal to zero etc . When ' strict = true ' throws an exception if deterministic conversion is not possible
public static object AsType(this string val, Type tp, bool strict = true)
              {
                try
                {
                    Func<string, bool, object> func = null;
                    if (s_CONV.TryGetValue(tp, out func)) return func(val, strict);
                    if (tp.IsEnum)
                    {
                      return Enum.Parse(tp, val, true);
                    }
                    if (tp.IsGenericType && tp.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                          var v = val;
                          if (string.IsNullOrWhiteSpace(v)) return null;
                          var gargs = tp.GetGenericArguments();
                          if (gargs.Length==1)
                          {
                                var gtp = gargs[0];
                                if (gtp.IsEnum)
                                {
                                    return Enum.Parse(gtp, v, true);
                                }
                          }
                    }
                }
                catch(Exception error)
                {
                  throw new NFXException(string.Format(StringConsts.STRING_VALUE_COULD_NOT_BE_GOTTEN_AS_TYPE_ERROR,
                                                        val ?? StringConsts.NULL_STRING, tp.FullName), error);
                }
                throw new NFXException(string.Format(StringConsts.STRING_VALUE_COULD_NOT_BE_GOTTEN_AS_TYPE_ERROR,
                                                        val ?? StringConsts.NULL_STRING, tp.FullName));
              }
public static object AsType ( this string val , Type tp , bool strict = true ) { try { Func < string , bool , object > func = null ; if ( s_CONV . TryGetValue ( tp , out func ) ) return func ( val , strict ) ; if ( tp . IsEnum ) { return Enum . Parse ( tp , val , true ) ; } if ( tp . IsGenericType && tp . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) ) { var v = val ; if ( string . IsNullOrWhiteSpace ( v ) ) return null ; var gargs = tp . GetGenericArguments ( ) ; if ( gargs . Length == 1 ) { var gtp = gargs [ 0 ] ; if ( gtp . IsEnum ) { return Enum . Parse ( gtp , v , true ) ; } } } } catch ( Exception error ) { throw new NFXException ( string . Format ( StringConsts . STRING_VALUE_COULD_NOT_BE_GOTTEN_AS_TYPE_ERROR , val ?? StringConsts . NULL_STRING , tp . FullName ) , error ) ; } throw new NFXException ( string . Format ( StringConsts . STRING_VALUE_COULD_NOT_BE_GOTTEN_AS_TYPE_ERROR , val ?? StringConsts . NULL_STRING , tp . FullName ) ) ; }
Tries to get a string value as specified type.
Tries to get a string value as specified type .
string
this
tp
Type
strict
bool