C#中的泛型类型转换

Meelfan Bmfp

有什么办法可以改善以下代码。我知道C#8.0中的不可为空的引用,并且在想,无论哪种方式,或者它们在下面进行编码的任何其他方式,通常都可以变得更健壮和更好。在通过实体框架核心插入数据库之前,调用此方法来转换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;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在C#中强制转换为任何类型的泛型

来自分类Dev

在 C# 中安全地转换泛型类型

来自分类Dev

转换泛型类型C#

来自分类Dev

C#中泛型类型的缺点

来自分类Dev

C#中的泛型类型

来自分类Dev

在C#中循环泛型类型

来自分类Dev

C#中的双重泛型类型

来自分类Dev

C#:从泛型转换为值类型

来自分类Dev

C#中的泛型继承:无法将类型“ MyWidget”隐式转换为“ IWidget”

来自分类Dev

在C#中的函数中访问泛型类型的属性

来自分类Dev

C#中的条件泛型类型构造函数?

来自分类Dev

在C#中访问非泛型类型

来自分类Dev

C#中的泛型和继承类型

来自分类Dev

返回C#中作为集合的泛型类型

来自分类Dev

C#中的递归泛型类型参数

来自分类Dev

在c#中遍历泛型类型列表

来自分类Dev

比较C#中2个泛型类型的对象

来自分类Dev

C#泛型类型中的多态性

来自分类Dev

C#转换为接口泛型中的对象

来自分类Dev

C#转换为接口泛型中的对象

来自分类Dev

将引用转换为C#中的泛型类

来自分类Dev

使用C#中的泛型进行转换

来自分类Dev

C#泛型并使用类型化方法中的非泛型版本

来自分类Dev

在C#中检查T泛型类型具有属性S(泛型)

来自分类Dev

c#获取泛型类中泛型类型参数的名称

来自分类Dev

C# 泛型对象不能添加到该泛型类型的列表中

来自分类Dev

在C#中强制转换泛型函数类型参数

来自分类Dev

C#强制转换泛型类型(如C ++用户定义的转换)

来自分类Dev

两类泛型中的一种类型的C#转换不起作用