C# generic string parse to any object

后端 未结 4 464
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 04:25

I am storing object values in strings e.g.,

string[] values = new string[] { \"213.4\", \"10\", \"hello\", \"MyValue\"};

is there any way to ge

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 04:54

    public T Get(string val)
    {
        if (!string.IsNullOrWhiteSpace(val))
            return (T) TypeDescriptor.GetConverter(typeof (T)).ConvertFromString(val);
        else 
            return default(T);
    }
    

提交回复
热议问题