C#开发一应用的总结

要搭建测试环境。
Webbrowser使用方面:
重新加载一页面后,要获取新的内容要使用重新使用browser.Document获取;

HtmlElement的GetElementsByTagName(html标记)是HtmlElement中包括的所有标记的集合,如果相同元素有嵌套时要注意。如:
<table>
<tr>
     <td>1</td>
</tr>
<tr>
<td>
        <table>
         <tr>
         <td>2</td>
         </tr><tr>
         <td>3</td>
        </tr>
       </table>
</td>
</tr>
</table>

如果获取最外层table节点为HtmlElement  he,此时使用he.GetElementsByTagName(“tr”),则包含4个元素:

0:
<tr>
<td>1</td>
</tr>
1:
<tr>
<td>
<table>
<tr>
<td>2</td>
</tr><tr>
<td>3</td>
</tr>
</table>
</td>
</tr>
2:
<tr>
<td>2</td>
</tr>
3:
<tr>
<td>3</td>
</tr>

如果要获取class属性,要使用HtmlElement he;he.getAttribute(“className”); 或he.getAttribute(“classname”); 即不区分大小写

下拉列表设置属性:
he.SetAttribute("value", "2013-1-1");  
he.RaiseEvent("onChange");    //触发onChange事件,如果在后续代码中使用he.invokeScript(js名称);这个可以不用写           


其它:

Dictionary使用中如果使用foreach travel时不能进行更改(增加、删除、修改),否则会报错:
Collection was modified; enumeration operation may not execute.
遍历并修改:

#region Dictionary Travel modify
public static void mainTest()
{
    Dictionary<String, Int32> dic = new Dictionary<String, Int32>();
    dic.Add("1", 1);
    dic.Add("2", 2);
    dic.Add("3", 3);
    foreachTravelDic(dic);
    modifyDic1(dic);
    foreachTravelDic(dic);
    modifyDic2(dic);
    foreachTravelDic(dic);
    Console.ReadKey();
}


public static void foreachTravelDic(Dictionary<String, Int32> dic)
{
    foreach (KeyValuePair<String, Int32> kvp in dic)
    {
        Console.WriteLine(String.Format("Key:{0}; Value:{1}", kvp.Key, kvp.Value));
        //dic[kvp.Key] = 2;//此操作会报错
    }
}

 

private static void modifyDic2(Dictionary<String, Int32> dic)
{
    String[] keyStr = dic.Keys.ToArray<String>();
    for (int i = 0; i < keyStr.Length; i++)
    {
        dic[keyStr[i]] = dic[keyStr[i]] + 22;
    }

}


private static void modifyDic1(Dictionary<String, Int32> dic)
{
    int dicCount = dic.Keys.Count;
    String[] strKey = new String[dicCount];
    dic.Keys.CopyTo(strKey, 0);//支持.net2.0
    for (int i = 0; i < strKey.Length; i++)
    {
        if (dic.ContainsKey(strKey[i]))
        {
            dic[strKey[i]] = dic[strKey[i]] + 11;
        }
    }
}
#endregion
output:
image

补充:
foreach在travel Dictionary时报错的原因:
http://www.cnblogs.com/dudu/archive/2006/07/31/464391.html
在执行foreach时,其他线程对_dictionary进行了Add操作,改变了_dictionary中的数据,从而产生了上述的异常信息。

     那为什么会产生这样的异常信息呢?
     foreach实际上执行的代码是:   

Dictionary<int, int>.Enumerator enumerator = _dictionary.GetEnumerator(); 
try

while (enumerator.MoveNext())  
   { 
    } 

finally

   IDisposable d = enumerator as IDisposable; 
if (d != null) d.Dispose(); 
}

     通过Reflector查看Dictionary<TKey, TValue>.Enumerator.MoveNext()源代码,我们会发现开始处有这样的代码:

if (this.version != this.dictionary.version)
      {
            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
      }

     而异常就是在这里发生的,因为Add操作时改变了Dictionary的version,通过查看Insert(TKey key, TValue value, bool add)的源代码会看出。
     我觉得Dictionary<TKey, TValue>应该提供一个方法,可以设置在MoveNext时是否进行版本检查,因为有时在foreach操作时,可能并不关心Dictionary中的数据是否被修改,我遇到的就是这样的情况,现在由于这个问题而不能使用foreach,而只能采取其他方法遍历Dictionary<TKey, TValue>中的数据。


Dictionary类型在作为参数传递时:
public static void Main(String[] args) {
           #region test Dic
            IDictionary<String,String> sumDic=new Dictionary<String,String>();
            IDictionary<String, String> secDic = new Dictionary<String, String>();
            sumDic.Add("ak","av");
            sumDic.Add("bk", "bv");
            sumDic.Add("ck", "cv");
            sumDic.Add("dk", "dv");
            sumDic.Add("ek", "ev");

            secDic = testDic(sumDic);//引用传递
            foreach (KeyValuePair<String, String> kvp in sumDic)
            {
                Console.WriteLine(String.Format("{0}====>{1}",kvp.Key,kvp.Value));
            }
            Console.WriteLine("----------------------------");
            foreach (KeyValuePair<String, String> kvp in secDic)
            {
                Console.WriteLine(String.Format("{0}====>{1}", kvp.Key, kvp.Value));
            }
//            output:
//            ak====>av
//            bk====>bv
//            ck====>cv
//            ek====>ev
//----------------------------
//            dk====>dv

            #endregion
            Console.ReadKey();

        }

private static IDictionary<String, String> testDic(IDictionary<String, String> sumDic)
  {
      IDictionary<String, String> secDic = new Dictionary<String, String>();
      String[] keysStr=sumDic.Keys.ToArray<String>();

      for (int i = 0; i < keysStr.Length; i++)
      {
          if (sumDic[keysStr[i]].Equals("dv"))
          {
              secDic.Add(keysStr[i], sumDic[keysStr[i]]);
              sumDic.Remove(keysStr[i]);
          }
      }

      return secDic;
  }


出现的问题:
1、因业务需要更改了几个关键方法,程序没有报异常,但是有一个方法一直没有执行,一直很困惑
由于开发环境和生产环境分离,也无法在现网调试,只能分析代码,最后发现问题就在改的方法上,中间的过渡方法执行了另一个操作,但是没有将值返回,导致向上层传递了空Dictionary,虽然没有报错,但不满足另一个方法的执行条件。。。。

 

2、日志输出时忽略了一些关键信息,需先规划好要输入哪些必要信息;


3、IDictionary<String, int[]>  数组作为dic的value时出现的问题:

数组是引用类型,如果在第二次使用时没有实始化就重新使用,会影响到以前的值:
有问题的:

       public static void TestDicArray() {
            Dictionary<String, int[]> dic = new Dictionary<String, int[]>();

          int[] valueArray=new int[2];
          valueArray[0] = 1;
          valueArray[1] = 1;

          dic.Add("test1",valueArray);
          //valueArray = new int[2];
          valueArray[0] = 2;
          valueArray[1] = 2;

          dic.Add("test2", valueArray);

          //valueArray = new int[2];
          valueArray[0] = 3;
          valueArray[1] = 3;

          dic.Add("test3", valueArray);

         // valueArray = new int[2];
          valueArray[0] = 4;
          valueArray[1] = 4;

          dic.Add("test4", valueArray);

         // valueArray = new int[2];
          valueArray[0] = 5;
          valueArray[1] = 5;

          dic.Add("test5", valueArray);



          foreach (KeyValuePair<String,int[]> kvp in dic)
          {

              Console.WriteLine(kvp.Key);
              int[] tempInt = kvp.Value;
              Console.WriteLine(tempInt[0]+"\t"+tempInt[1]);

              Console.WriteLine(dic[kvp.Key][0]+"\t"+dic[kvp.Key][1]);

          }
        
        
        }


image

正确的:

       public static void TestDicArray() {
            Dictionary<String, int[]> dic = new Dictionary<String, int[]>();
 
          int[] valueArray=new int[2];
          valueArray[0] = 1;
          valueArray[1] = 1;
 
          dic.Add("test1",valueArray);
          valueArray = new int[2];
          valueArray[0] = 2;
          valueArray[1] = 2;
 
          dic.Add("test2", valueArray);
 
          valueArray = new int[2];
          valueArray[0] = 3;
          valueArray[1] = 3;
 
          dic.Add("test3", valueArray);
 
          valueArray = new int[2];
          valueArray[0] = 4;
          valueArray[1] = 4;
 
          dic.Add("test4", valueArray);
 
          valueArray = new int[2];
          valueArray[0] = 5;
          valueArray[1] = 5;
 
          dic.Add("test5", valueArray);
 
 
 
          foreach (KeyValuePair<String,int[]> kvp in dic)
          {
 
              Console.WriteLine(kvp.Key);
              int[] tempInt = kvp.Value;
              Console.WriteLine(tempInt[0]+"\t"+tempInt[1]);
 
              Console.WriteLine(dic[kvp.Key][0]+"\t"+dic[kvp.Key][1]);
 
          }
        
        
        }


OutPut:

image

                    //input value attribute;InnerText is null
                    HtmlElementCollection hecSelects = hd.GetElementsByTagName("input");
                    foreach (HtmlElement heSelect in hecSelects)
                    {
                        showMsgInText("value:"+heSelect.GetAttribute("value"));
                        showMsgInText("InnerText:" + heSelect.InnerText);
                    }

posted @ 2013-06-26 17:44  沧海一滴  阅读(558)  评论(0编辑  收藏  举报