zoukankan      html  css  js  c++  java
  • 将字符串转化为某种类型

       //将字符串转化为type型.
        private object parse(string s, Type t)
        {
            //如果字符串是一个string,直接返回.
            if (t.IsAssignableFrom(typeof(string))) return s;
            //如果字符串是一个数组,那么将其解析为数组并返回.
            if (t.IsArray) return parseArray(s, t);

            //构造并调用tpe的Parse方法.
            BindingFlags flags = BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Public;
            MethodInfo parseMethod = t.GetMethod("Parse", flags, nullnew Type[] { typeof(string) }, null);
            if (parseMethod != null && parseMethod.ReturnType == t)
            {
                return parseMethod.Invoke(nullnew object[] { s });
            }
            else
            {
                throw new ApplicationException("Can't parse " + t.FullName + " because it doesn't have a static Parse() method");
            }
        }

        //将字符串转化为某种类型的数组.
        private object[] parseArray(string s, Type arrayType)
        {
            string[] strings = s.Split(new char[] { ',' });
            object[] result = new object[strings.Length];
            for (int i = 0; i < strings.Length; i++)
            {
                result[i] = parse(strings[i], arrayType.GetElementType());
            }
            return result;
        }

  • 相关阅读:
    ansible的管理与剧本
    条件随机场入门(二) 条件随机场的模型表示
    条件随机场入门(一) 概率无向图模型
    隐马尔科夫模型
    高斯混合模型 GMM
    K-Means 算法
    EM 算法
    Sequential Minimal Optimization (SMO) 算法
    LinkedIn文本分析平台:主题挖掘的四大技术步骤
    SVM 核方法
  • 原文地址:https://www.cnblogs.com/robyn/p/3729321.html
Copyright © 2011-2022 走看看