c# - C# 中的泛型类型转换

标签 c# .net

有什么方法可以改进以下代码。我知道 C# 8.0 中的不可空引用,并在考虑以这种方式或任何其他方式编写下面的代码,总体上可以变得更健壮和更好。 在通过 Entity Framework 核心插入数据库之前调用此方法以转换 xml 数据。欢迎使用这些工具来改进此代码的任何方式。

public object Convert(string value, Type toType)
    {
        try
        {
            if (toType == typeof(short))
            {
                return short.Parse(value);
            }
            if (toType == typeof(short?))
            {
                if (string.IsNullOrEmpty(value))
                {
                    return null;
                }
                return short.Parse(value);
            }
            if (toType == typeof(int))
            {
                return int.Parse(value);
            }
            if (toType == typeof(int?))
            {
                if (string.IsNullOrEmpty(value))
                {
                    return null;
                }
                return int.Parse(value);
            }
            if (toType == typeof(decimal))
            {
                return decimal.Parse(value);
            }
            if (toType == typeof(decimal?))
            {
                if (string.IsNullOrEmpty(value))
                {
                    return null;
                }
                return decimal.Parse(value);
            }
            if (toType == typeof(DateTime))
            {
                return DateTime.Parse(value);
            }
            if (toType == typeof(DateTime?))
            {
                if (string.IsNullOrEmpty(value))
                {
                    return null;
                }
                return DateTime.Parse(value);
            }

            throw new NotSupportedException($"No conversion defined for type:'{toType}'");
        }
        catch (System.FormatException excp)
        {
            throw new ConversionException($"Value:'{value}' could not be converted to:'{toType.Name}'", excp);
        }
    }

提前致谢

最佳答案

我唯一可以提供的是通过使用 .TryParse() 代替 .Parse() 来提高稳健性

class Program
{
    static void Main(string[] args)
    {
        var i = Parse<int>("100");
        var x = Parse<double>("3.1417");
        var s = Parse<string>("John");
        var d = Parse<Decimal>("1234.56");
        var f = Parse<DateTime>("4/1/2044");
        var q = Parse<byte>("4A");

        Decimal? p = Parse<decimal>("Not Decimal");
    }

    public static dynamic Parse<T>(string text)
    {
        var toType = typeof(T);
        if (toType == typeof(int))
        {
            if (int.TryParse(text, out int x))
            {
                return x;
            }
        }
        else if (toType == typeof(short))
        {
            if (short.TryParse(text, out short x))
            {
                return x;
            }
        }
        else if (toType == typeof(double))
        {
            if (double.TryParse(text, out double x))
            {
                return x;
            }
        }
        else if (toType == typeof(decimal))
        {
            if (decimal.TryParse(text, out decimal x))
            {
                return x;
            }
        }
        else if (toType == typeof(DateTime))
        {
            if (DateTime.TryParse(text, out DateTime x))
            {
                return x;
            }
        }
        else if (toType == typeof(byte))
        {
            if (byte.TryParse(text, System.Globalization.NumberStyles.HexNumber, null, out byte x))
            {
                return x;
            }
        }
        else if (toType == typeof(string))
        {
            return text;
        }
        return null;
    }
}

关于c# - C# 中的泛型类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343105/

相关文章:

c# - 对称密码算法之间的区别

c# - 如何正确模拟代表

c# - 在 winRT MessageDialog 中添加 TextBox

c# - 从路径名获取图像对象

c# - 转义字符串中的反斜杠

c# - 在组合框选择事件的顶部显示新条目,用行分隔每个新条目

c# - 连接到 Azure 中国的 KeyVault

.net - 用户图像——数据库与文件系统存储

.net - 你知道 MBUnit 的任何教程吗?

c# - COM 互操作对象在一个项目中抛出 InvalidCastException,但在其他项目中没有