Skip to content

Commit

Permalink
Fix explicit casting from BigInt to string
Browse files Browse the repository at this point in the history
  • Loading branch information
gurnec committed Jan 24, 2020
1 parent 44b05a4 commit fb56700
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/System.Management.Automation/engine/LanguagePrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,33 @@ private static PSConverter<bool> CreateNumericToBoolConverter(Type fromType)
}
}

private static string ConvertBigIntegerToString(object valueToConvert,
Type resultType,
bool recursion,
PSObject originalValueToConvert,
IFormatProvider formatProvider,
TypeTable backupTable)
{
if (originalValueToConvert != null && originalValueToConvert.TokenText != null)
{
return originalValueToConvert.TokenText;
}

ExecutionContext ecFromTLS = LocalPipeline.GetExecutionContextFromTLS();
try
{
typeConversion.WriteLine("Converting BigInteger to string.");
return PSObject.ToStringParser(ecFromTLS, valueToConvert, formatProvider);
}
catch (ExtendedTypeSystemException e)
{
typeConversion.WriteLine("Converting BigInteger to string Exception: \"{0}\".", e.Message);
throw new PSInvalidCastException("InvalidCastFromBigIntegerToString", e,
ExtendedTypeSystem.InvalidCastExceptionWithInnerException,
valueToConvert.ToString(), resultType.ToString(), e.Message);
}
}

private static Hashtable ConvertIDictionaryToHashtable(object valueToConvert,
Type resultType,
bool recursion,
Expand Down Expand Up @@ -4385,8 +4412,7 @@ internal static ConversionRank GetConversionRank(Type fromType, Type toType)
typeof(Int16), typeof(Int32), typeof(Int64),
typeof(UInt16), typeof(UInt32), typeof(UInt64),
typeof(sbyte), typeof(byte),
typeof(Single), typeof(double), typeof(decimal),
typeof(System.Numerics.BigInteger)
typeof(Single), typeof(double), typeof(decimal)
};

private static Type[] s_integerTypes = new Type[] {
Expand Down Expand Up @@ -4420,6 +4446,7 @@ internal static void RebuildConversionCache()
CacheConversion<object>(type, typeofChar, LanguagePrimitives.ConvertIConvertible, ConversionRank.NumericString);
CacheConversion<object>(typeofNull, type, LanguagePrimitives.ConvertNullToNumeric, ConversionRank.NullToValue);
}
CacheConversion<string>(typeof(System.Numerics.BigInteger), typeofString, LanguagePrimitives.ConvertBigIntegerToString, ConversionRank.NumericString);

CacheConversion<bool>(typeof(Int16), typeofBool, ConvertInt16ToBool, ConversionRank.Language);
CacheConversion<bool>(typeof(Int32), typeofBool, ConvertInt32ToBool, ConversionRank.Language);
Expand Down

0 comments on commit fb56700

Please sign in to comment.