Source/NFX.Erlang/Types/ErlObject.cs
/// <summary>
/// Tries to convert an Erlang term as specified native type.
/// Throw exception if conversion is not possible
/// </summary>
Tries to convert an Erlang term as specified native type.
Throw exception if conversion is not possible
Tries to convert an Erlang term as specified native type . Throw exception if conversion is not possible
public static object AsType(this IErlObject val, Type t)
    {
      try
      {
        if (typeof(IErlObject).IsAssignableFrom(t)) return val;
        if (t == typeof(string))    return val.ValueAsString;
        if (t == typeof(int))       return val.ValueAsInt;
        if (t == typeof(long))      return val.ValueAsLong;
        if (t == typeof(short))     return (short)val.ValueAsInt;
        if (t == typeof(bool))      return val.ValueAsBool;
        if (t == typeof(float))     return (float)val.ValueAsDouble;
        if (t == typeof(double))    return val.ValueAsDouble;
        if (t == typeof(decimal))   return val.ValueAsDecimal;
        if (t == typeof(TimeSpan))  return val.ValueAsTimeSpan;
        if (t == typeof(DateTime))  return val.ValueAsDateTime;
        if (t == typeof(object))    return val;
        if (t.IsEnum) return Enum.Parse(t, val.ValueAsString, true);
        bool empty = string.IsNullOrEmpty(val.ValueAsString);
        if (t == typeof(int?))      return empty ? (int?)null       : val.ValueAsInt;
        if (t == typeof(long?))     return empty ? (long?)null      : val.ValueAsLong;
        if (t == typeof(short?))    return empty ? (short?)null     : (short)val.ValueAsInt;
        if (t == typeof(bool?))     return empty ? (bool?)null      : val.ValueAsBool;
        if (t == typeof(float?))    return empty ? (float?)null     : (float)val.ValueAsDouble;
        if (t == typeof(double?))   return empty ? (double?)null    : val.ValueAsDouble;
        if (t == typeof(decimal?))  return empty ? (decimal?)null   : val.ValueAsDecimal;
        if (t == typeof(TimeSpan?)) return empty ? (TimeSpan?)null  : val.ValueAsTimeSpan;
        if (t == typeof(DateTime?)) return empty ? (DateTime?)null  : val.ValueAsDateTime;
        if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
          var v = val.ToString();
          if (string.IsNullOrWhiteSpace(v)) return null;
          var gargs = t.GetGenericArguments();
          if (gargs.Length == 1)
          {
            var gtp = gargs[0];
            if (gtp.IsEnum)
                return Enum.Parse(gtp, v, true);
          }
        }
      }
      catch (Exception e)
      {
        throw new NFXException(StringConsts.ERL_CANNOT_CONVERT_TYPES_ERROR.Args(val.GetType().Name, t.FullName), e);
      }
      throw new ErlException(StringConsts.ERL_CANNOT_CONVERT_TYPES_ERROR, val.GetType().Name, t.FullName);
    }
public static object AsType ( this IErlObject val , Type t ) { try { if ( typeof ( IErlObject ) . IsAssignableFrom ( t ) ) return val ; if ( t == typeof ( string ) ) return val . ValueAsString ; if ( t == typeof ( int ) ) return val . ValueAsInt ; if ( t == typeof ( long ) ) return val . ValueAsLong ; if ( t == typeof ( short ) ) return ( short ) val . ValueAsInt ; if ( t == typeof ( bool ) ) return val . ValueAsBool ; if ( t == typeof ( float ) ) return ( float ) val . ValueAsDouble ; if ( t == typeof ( double ) ) return val . ValueAsDouble ; if ( t == typeof ( decimal ) ) return val . ValueAsDecimal ; if ( t == typeof ( TimeSpan ) ) return val . ValueAsTimeSpan ; if ( t == typeof ( DateTime ) ) return val . ValueAsDateTime ; if ( t == typeof ( object ) ) return val ; if ( t . IsEnum ) return Enum . Parse ( t , val . ValueAsString , true ) ; bool empty = string . IsNullOrEmpty ( val . ValueAsString ) ; if ( t == typeof ( int ? ) ) return empty ? ( int ? ) null : val . ValueAsInt ; if ( t == typeof ( long ? ) ) return empty ? ( long ? ) null : val . ValueAsLong ; if ( t == typeof ( short ? ) ) return empty ? ( short ? ) null : ( short ) val . ValueAsInt ; if ( t == typeof ( bool ? ) ) return empty ? ( bool ? ) null : val . ValueAsBool ; if ( t == typeof ( float ? ) ) return empty ? ( float ? ) null : ( float ) val . ValueAsDouble ; if ( t == typeof ( double ? ) ) return empty ? ( double ? ) null : val . ValueAsDouble ; if ( t == typeof ( decimal ? ) ) return empty ? ( decimal ? ) null : val . ValueAsDecimal ; if ( t == typeof ( TimeSpan ? ) ) return empty ? ( TimeSpan ? ) null : val . ValueAsTimeSpan ; if ( t == typeof ( DateTime ? ) ) return empty ? ( DateTime ? ) null : val . ValueAsDateTime ; if ( t . IsGenericType && t . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) ) { var v = val . ToString ( ) ; if ( string . IsNullOrWhiteSpace ( v ) ) return null ; var gargs = t . GetGenericArguments ( ) ; if ( gargs . Length == 1 ) { var gtp = gargs [ 0 ] ; if ( gtp . IsEnum ) return Enum . Parse ( gtp , v , true ) ; } } } catch ( Exception e ) { throw new NFXException ( StringConsts . ERL_CANNOT_CONVERT_TYPES_ERROR . Args ( val . GetType ( ) . Name , t . FullName ) , e ) ; } throw new ErlException ( StringConsts . ERL_CANNOT_CONVERT_TYPES_ERROR , val . GetType ( ) . Name , t . FullName ) ; }
Tries to convert an Erlang term as specified native type.
Tries to convert an Erlang term as specified native type .
IErlObject
this
t
Type