General replacement for multiple Funcs

I parse the vast xml content and populate the DTO to send to the partner endpoint. The xml content is stored in text files, which I read periodically and send. Anyway, now I found myself with Funcs as follows:

Func<string, int> ConvertInt= (p) => String.IsNullOrEmpty(p) ? 0 : Convert.ToInt32(p);

I check if the element that I am reading does not read, and if it does not send the default value, that is, in the case of integers, the value is 0. Do I have the same for double and DateTime? , one way or another, to convert this, so I could say an extension method that would handle all types, i.e. ints, doubleles and DateTimes? Again, the reason I am doing this conversion is because it allows me to talk about some integer value that the DTO expects but is not required, I don’t want to try to use an empty string for it and so on ... if there is no value Date, I just want to return null, because the DTO accepts values ​​with a null value, so I wrote funcs, and I want to consolidate.

+3
source share
1

TypeConverter: http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx

protected X Convert<X>(string value)
{
    X val = default(X);

    var tc = TypeDescriptor.GetConverter(typeof(X));

    if (!string.IsNullOrEmpty(value) && tc.CanConvertFrom(typeof(string)))
    {
        val = (X)tc.ConvertFromString(value);
    }

    return val;
}
+8

All Articles