Pregunta

I have some methods to perform data conversion depending of source data type:

private static string GetStringValue(String value) { ... }

private static string GetStringValue(DateTime value) { ... }

private static string GetStringValue(Decimal value) { ... }

private static string GetStringValue(Double value) { ... }

private static string GetStringValue(Enum value) { ... }

private static string GetStringValue(Object value) { ... }

From source code, I get to call to one of the methods using this:

GetStringValue((dynamic)v);

This works fine for almost all cases, but I'm getting problems when trying to use it with decimal or double... I got an exception caused by ambiguous call between decimal and double variants.

Is there any way I can workaround this problem?

¿Fue útil?

Solución

I finally found the problem. The exception was raising when 'v' is an Int32. I didn't defined an overload for this type because I thought that it would go down to the 'object' overload, but instead of that, the runtime tries to use the 'decimal' and 'double' ones, which caused the exception.

So the solution is simply to add

private static string GetStringValue(Int32 value) { ... }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top