先split('-'),获取到str里面每一项的字符串,然后循环,反射找到对应的属性,getvalue取出值,重新用"-"拼接

解决方案 »

  1.   

    string s = ""+s1+"_"+s2+"_"+s3+"";
      

  2.   


    我需要的是动态映射,
    顺序都会不同。
    顺序不同怎么了,str里顺序不同,解出的数组顺序也不同啊
      

  3.   

    你先自己试验下split是什么效果,然后有问题再问
    不要还没看懂别人说什么就急忙否定
      

  4.   

    没有更加简洁的方式.
    这个就是最简洁的方式了.
    除非你改变设计思路,不要传入这种"name_gender_age"字符串
    里面明明是三个变量名,非要放到一个字符串里去,不先分解出来,如何得到对应的每个名称?
      

  5.   

    string str="name_gender_age";
    string[] propertyField=str.Splite('_');
    string result=string.Empty;
    foreach(string item in propertyField)
    {
    PropertyInfo pi = objectType.GetProperty(item, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (pi == null)
                    throw new ArgumentException("没有找到指定属性名称。");
                if (!pi.CanRead)
                    throw new ArgumentException("指定属性没有get方式,无法读取值。");            object value = pi.GetValue(objectInstance, null);
                if(value!=null){
                     result+=value+"_";
               }
    }
    result=result.TrimEnd('_');部分手写的,可能有点错误,自己修改下