PropertyInfo[] property = myQuestionMain.GetType().GetProperties();
        foreach (PropertyInfo p in property)
        {
            if (p.GetValue(myQuestionMain, null) != null && p.GetValue(myQuestionMain, null).ToString() != "0")
            {
                str += p.Name + "='" + p.GetValue(myQuestionMain, null) + "' and ";
            }
        }
把这个foreach循环改写成for循环

解决方案 »

  1.   

    PropertyInfo[] property = myQuestionMain.GetType().GetProperties(); 
            for (int i=0;i<property.Length;i++) 
            { 
                if (property[i].GetValue(myQuestionMain, null) != null && property[i].GetValue(myQuestionMain, null).ToString() != "0") 
                { 
                    str += property[i].Name + "='" + property[i].GetValue(myQuestionMain, null) + "' and "; 
                } 
            } 
      

  2.   


    PropertyInfo[] property = myQuestionMain.GetType().GetProperties(); 
            for(int i=;i<property.length;i++) 
            { 
                if (property(i).GetValue(myQuestionMain, null) != null && property(i).GetValue(myQuestionMain, null).ToString() != "0") 
                { 
                    str += property(i).Name + "='" + property(i).GetValue(myQuestionMain, null) + "' and "; 
                } 
            } 
      

  3.   

    PropertyInfo[] property = myQuestionMain.GetType().GetProperties(); 
            for (int i=0;i<property.length;i++) 
            { 
                if (property[i].GetValue(myQuestionMain, null) != null && property[i].GetValue(myQuestionMain, null).ToString() != "0") 
                { 
                    str += property[i].Name + "='" + property[i].GetValue(myQuestionMain, null) + "' and "; 
                } 
            } 
      

  4.   

    PropertyInfo[] property = myQuestionMain.GetType().GetProperties(); 
            for (int i=0;i <property.Length;i++) 
            { 
                if (property[i].GetValue(myQuestionMain, null) != null && property[i].GetValue(myQuestionMain, null).ToString() != "0") 
                { 
                    str += property[i].Name + "='" + property[i].GetValue(myQuestionMain, null) + "' and "; 
                } 
            } 
      

  5.   

    PropertyInfo[] property = myQuestionMain.GetType().GetProperties(); for (int i = 0; i < property.Length; i++)
    {
      PropertyInfo p =(PropertyInfo)property[i];
      if (p.GetValue(myQuestionMain, null) != null && p.GetValue(myQuestionMain, null).ToString() != "0") 
      { 
         str += p.Name + "='" + p.GetValue(myQuestionMain, null) + "' and "; 
      } 
    }
      

  6.   

    呵呵,又当VB的数组写了PropertyInfo[] property = myQuestionMain.GetType().GetProperties(); 
            for(int i=;i<property.length;i++) 
            { 
                if (property[i].GetValue(myQuestionMain, null) != null && property[i].GetValue(myQuestionMain, null).ToString() != "0") 
                { 
                    str += property[i].Name + "='" + property[i].GetValue(myQuestionMain, null) + "' and "; 
                } 
            } 
      

  7.   

    PropertyInfo[] property = myQuestionMain.GetType().GetProperties(); 
            for (int i=0,i<property.length,i++) 
            { 
                if (property[i].GetValue(myQuestionMain, null) != null && property[i].GetValue(myQuestionMain, null).ToString() != "0") 
                { 
                    str += property[i].Name + "='" + property[i].GetValue(myQuestionMain, null) + "' and "; 
                } 
            } 
      

  8.   

    foreach (int i=0;i< property.count;i++) {
    PropertyInfo p=property[i];

            { 
      

  9.   


    PropertyInfo[] property = myQuestionMain.GetType().GetProperties();
            for(int i=0;i<property.Length;i++)
            {
                if (property[i].GetValue(myQuestionMain, null) != null && property[i].GetValue(myQuestionMain, null).ToString() != "0")
                {
                    str += property[i].Name + "='" + property[i].GetValue(myQuestionMain, null) + "' and ";
                }
            } 
      

  10.   


    PropertyInfo[] property = myQuestionMain.GetType().GetProperties();
            for(int i=0;i<property.Length;i++)
            {
                if (property[i].GetValue(myQuestionMain, null) != null && property[i].GetValue(myQuestionMain, null).ToString() != "0")
                {
                    str += property[i].Name + "='" + property[i].GetValue(myQuestionMain, null) + "' and ";
                }
            } 
      

  11.   

    运行出错:无法对属性或索引器“System.Reflection.MemberInfo.Name”赋值 -- 它是只读的
      

  12.   

    for(int i =0;i < PropertyInfo.length;i++)
    {
     if (PropertyInfo[i].GetValue(myQuestionMain, null) != null && PropertyInfo[i].GetValue      (myQuestionMain, null).ToString() != "0") 
       { 
        str += PropertyInfo[i].Name + "='" + PropertyInfo[i].GetValue(myQuestionMain, null) + "' and "; 
        } 
    }