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;
        }

  • 相关阅读:
    华为机试练习(一)
    LM拟合算法
    5.1 模块化程序设计
    第3周 运算的流程控制
    KEGG数据库介绍
    topGO
    GO.db
    Bioconductor应用领域之基因芯片
    org.Hs.eg.db包简介(转换NCBI、ensemble等数据库中基因ID,symbol等之间的转换)
    Bioconductor的历史
  • 原文地址:https://www.cnblogs.com/robyn/p/3729321.html
Copyright © 2011-2022 走看看