I’m writing a pluralization framework using .NET for Windows Store apps. For a custom formatter string Format(string format, params object[] args), I have the following code:
public static bool IsExactlyOne(object n)
{
if (n is Int16)
{
return (Int16)n == 1;
}
if (n is int) // Int32
{
return (int)n == 1;
}
if (n is long) // Int64
{
return (long)n == 1L;
}
if (n is UInt16)
{
return (UInt16)n == 1U;
}
if (n is uint) // UInt32
{
return (uint)n == 1U;
}
if (n is ulong) // UInt64
{
return (ulong)n == 1UL;
}
if (n is byte)
{
return (byte)n == 1;
}
if (n is sbyte)
{
return (sbyte)n == 1;
}
if (n is float)
{
return (float)n == 1.0F;
}
if (n is double)
{
return (double)n == 1.0D;
}
if (n is decimal)
{
return (decimal)n == 1.0M;
}
throw new ArgumentException("Unsupported type");
}
As you see, it’s pretty verbose. Is there some way to simplify this? Please note: IConvertible is not available for Windows Store apps.
How about using Dictionary to avoid
if:Or use
dynamic: