Get datatype from values passed as string

前端 未结 5 697
滥情空心
滥情空心 2021-02-04 12:16

I am writing a framework that will connect to many different data source types and return values from these sources. The easy ones are SQL, Access and Oracle. The tougher ones a

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 12:45

        List types = new List(new Type[] {
            typeof(Boolean)
            , typeof(int)
            , typeof(double)
            , typeof(DateTime)
        });
        string t = "true";
        object retu;
        foreach (Type type in types)
        {
            TypeConverter tc = TypeDescriptor.GetConverter(type);
            if (tc != null)
            {
                try
                {
                    object obj = tc.ConvertFromString(t); // your return value;
                }
                catch (Exception)
                {
                    continue;
                }
            }
        }
    

提交回复
热议问题