PrimitiveConverter
Implements all standard conversions.
using Castle.Core.Configuration;
using System;
using System.Globalization;
namespace Castle.MicroKernel.SubSystems.Conversion
{
[Serializable]
public class PrimitiveConverter : AbstractTypeConverter
{
private readonly Type[] types = new Type[15] {
typeof(char),
typeof(DateTime),
typeof(decimal),
typeof(bool),
typeof(short),
typeof(int),
typeof(long),
typeof(ushort),
typeof(uint),
typeof(ulong),
typeof(byte),
typeof(sbyte),
typeof(float),
typeof(double),
typeof(string)
};
public override bool CanHandleType(Type type)
{
return Array.IndexOf(types, type) != -1;
}
public override object PerformConversion(string value, Type targetType)
{
if (!(targetType == typeof(string)))
try {
return Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture);
} catch (Exception innerException) {
throw new ConverterException($"""{value}""{targetType.FullName}", innerException);
}
return value;
}
public override object PerformConversion(IConfiguration configuration, Type targetType)
{
return PerformConversion(configuration.get_Value(), targetType);
}
}
}