<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title><![CDATA[TUPUNCO 's BLOG]]></title>
<link>http://zhq.ahau.edu.cn/blog/</link>
<description><![CDATA[小强]]></description>
<language>zh-cn</language>
<copyright><![CDATA[Copyright 2005 PBlog3 v2.8]]></copyright>
<webMaster><![CDATA[tupunco@163.com(tupunco)]]></webMaster>
<generator>PBlog2 v2.4</generator> 
<image>
	<title>TUPUNCO &#39;s BLOG</title>
	<url>http://zhq.ahau.edu.cn/blog/images/logos.gif</url>
	<link>http://zhq.ahau.edu.cn/blog/</link>
	<description>TUPUNCO &#39;s BLOG</description>
</image>

			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=487</link>
			<title><![CDATA[一致性哈希C#简单实现]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Sat,24 Jul 2010 18:10:15 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=487</guid>
		<description><![CDATA[<p><a href="http://memcached.org/" target="_blank">Memcached</a> 有名的开源高效分布式缓存系统.</p>  <p><a href="http://en.wikipedia.org/wiki/Consistent_hashing" target="_blank">Consistent Hashing</a>(一致性哈希) 算法一个几乎完美的<a href="http://en.wikipedia.org/wiki/Distributed_hash_table" target="_blank">分布式哈希算法</a>.</p>  <p>博客园7月上海活动, 第二场介绍&quot;网站缓存&quot;方面内容, 作者附带源码内提供的真不错. 不过感觉其中关于&quot;一致性哈希&quot;实现部分有点问题. 作者使用了.NET类库中 <a href="http://msdn.microsoft.com/zh-cn/6sh2ey19.aspx">List&lt;T&gt;</a> 的二叉搜索函数, <a href="http://msdn.microsoft.com/zh-cn/w4e7fxsh.aspx" target="_blank">MSDN文档</a>中对返回的信息作了以下说明:</p>  <blockquote>   <p>如果找到 item，则为已排序的 <a href="http://msdn.microsoft.com/zh-cn/6sh2ey19.aspx">List&lt;T&gt;</a> 中 item 的从零开始的索引；<strong><font color="#ff0000">否则为一个负数，该负数是大于 item 的第一个元素的索引的按位求补。如果没有更大的元素，则为 </font></strong><a href="http://msdn.microsoft.com/zh-cn/27b47ht3.aspx"><strong><font color="#ff0000">Count</font></strong></a><strong><font color="#ff0000"> 的按位求补</font></strong>。</p> </blockquote>  <p>一致性哈希简单说明(<a href="http://tech.idv2.com/2008/07/24/memcached-004/" target="_blank">来源</a>):</p>  <blockquote>   <p>首先求出memcached服务器（节点）的哈希值，并将其配置到0～2<sup>32</sup>的圆（continuum）上。然后用同样的方法求出存储数据的键的哈希值，并映射到圆上。然后从数据映射到的位置开始顺时针查找，将数据保存到找到的第一个服务器上。如果超过2<sup>32</sup>仍然找不到服务器，就会保存到第一台memcached服务器上。</p> </blockquote>  <p>好像 <a href="http://msdn.microsoft.com/zh-cn/6sh2ey19.aspx">List&lt;T&gt;</a> 提供的二叉搜索刚好满足实现, 即数据映射的节点放到大于其哈希值的顺时针的服务器节点. 下面是活动作者提供的代码核心部分:</p>  <div id="codeSnippetWrapper" class="csharpcode-wrapper">   <div id="codeSnippet" class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">string</span> Locate(<span class="kwrd">string</span> key)</pre>
<!--CRLF-->

    <pre class="alteven">{</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">uint</span> itemKeyHash = BitConverter.ToUInt32(<span class="kwrd">new</span> ModifiedFNV().ComputeHash(Encoding.Unicode.GetBytes(key)), 0);</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">int</span> foundIndex = Array.BinarySearch&lt;<span class="kwrd">uint</span>&gt;(<span class="kwrd">this</span>.keys, itemKeyHash);</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">if</span> (foundIndex &lt; 0)</pre>
<!--CRLF-->

    <pre class="alteven">    {</pre>
<!--CRLF-->

    <pre class="alt">        foundIndex = ~foundIndex;</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">if</span> (foundIndex == 0) foundIndex = <span class="kwrd">this</span>.keys.Length <strong><font color="#ff0000">- 1</font></strong>;</pre>
<!--CRLF-->

    <pre class="alt">        <span class="kwrd">else</span> <span class="kwrd">if</span> (foundIndex &gt;= <span class="kwrd">this</span>.keys.Length) foundIndex = 0;</pre>
<!--CRLF-->

    <pre class="alteven">    }</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">if</span> (foundIndex &lt; 0 || foundIndex &gt; <span class="kwrd">this</span>.keys.Length) <span class="kwrd">return</span> <span class="kwrd">null</span>;</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">return</span> <span class="kwrd">this</span>.servers[<span class="kwrd">this</span>.keys[foundIndex]];</pre>
<!--CRLF-->

    <pre class="alt">}</pre>
<!--CRLF--></div>
</div>

<p>感觉代码中不应该做 -1 的处理, 另外这个函数居然会返回 null.</p>

<p>提供一个小强自己实现的版本, 主要代码:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// Hash 具体的节点</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;key&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="kwrd">public</span> KeyValuePair&lt;<span class="kwrd">uint</span>, <span class="kwrd">string</span>&gt; Locate(<span class="kwrd">string</span> key)</pre>
<!--CRLF-->

    <pre class="alt">{</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">if</span> (<span class="kwrd">string</span>.IsNullOrEmpty(key))</pre>
<!--CRLF-->

    <pre class="alt">        <span class="kwrd">return</span> _serList[0];</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">    var hash = Hash(key);</pre>
<!--CRLF-->

    <pre class="alteven">    var index = _serList.BinarySearch(<span class="kwrd">new</span> KeyValuePair&lt;<span class="kwrd">uint</span>, <span class="kwrd">string</span>&gt;(hash, <span class="kwrd">null</span>), <span class="kwrd">new</span> KeyValuePairComparer());</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">if</span> (index &gt;= 0)</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">return</span> _serList[index];</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">else</span></pre>
<!--CRLF-->

    <pre class="alteven">    {</pre>
<!--CRLF-->

    <pre class="alt">        var nIndex = ~index;</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">if</span> (nIndex &gt;= _serList.Count)</pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">return</span> _serList[0];</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">else</span></pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">return</span> _serList[nIndex];</pre>
<!--CRLF-->

    <pre class="alteven">    }</pre>
<!--CRLF-->

    <pre class="alt">}</pre>
<!--CRLF--></div>
</div>

<p>代码下载地址:</p>

<p><iframe style="padding-bottom: 0px; background-color: #fcfcfc; padding-left: 0px; width: 98px; padding-right: 0px; height: 115px; padding-top: 0px" title="Preview" marginheight="0" src="http://cid-e63ac91b5bfc8e30.office.live.com/embedicon.aspx/Public/ConsistentHashing.cs" frameborder="0" marginwidth="0" scrolling="no"></iframe></p>

<p>参考地址: 
  <br /><a href="http://msdn.microsoft.com/zh-cn/w4e7fxsh.aspx">http://msdn.microsoft.com/zh-cn/w4e7fxsh.aspx</a> &lt;MSDN List&lt;T&gt;.BinarySearch 方法文档&gt; </p>

<p><a href="http://www.cnblogs.com/lovecindywang/archive/2010/07/19/1780589.html">http://www.cnblogs.com/lovecindywang/archive/2010/07/19/1780589.html</a>&lt;7/17博客园活动浅谈网站架构中缓存的应用所有资料&gt; </p>

<p><a href="http://tech.idv2.com/2008/07/24/memcached-004/">http://tech.idv2.com/2008/07/24/memcached-004/</a> &lt;memcached全面剖析&gt; 翻译版(只要介绍 memcached 的地方都会有本文中的几章'黄图') </p>

<p><a href="http://www.cnblogs.com/overred/archive/2009/12/29/consistent_hashing.html">http://www.cnblogs.com/overred/archive/2009/12/29/consistent_hashing.html</a> &lt;<a href="http://www.cnblogs.com/overred/archive/2009/12/29/Consistent_Hashing.html">《Shuttler.Net东写西读之二》Memcached的Consistent Hashing负载算法</a>&gt; </p>

<p><a href="http://www.codeproject.com/KB/recipes/lib-conhash.aspx" target="_blank">http://www.codeproject.com/KB/recipes/lib-conhash.aspx</a></p>

<p><a href="http://home.comcast.net/~bretm/hash/6.html">http://home.comcast.net/~bretm/hash/6.html</a> &lt;修改版FNV1_32 哈希函数&gt; 

  </p>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=486</link>
			<title><![CDATA[.NET 源代码]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Sun,20 Jun 2010 13:18:57 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=486</guid>
		<description><![CDATA[<p>查看.NET Framework的源代码实际也不是什么新鲜的事情, 最简单的可以通过Reflector查看, 不过会有一些语法特征方面的瑕疵, 例如对使用yeild关键字的实现部分, 老赵也在其博客&quot;人肉&quot;过他的<a target="_blank" href="http://blog.zhaojie.me/2010/01/decompile-methods-with-yield-manually.html">编译后的特征</a>. 印象中微软在VS2008SP1前后开放了.NET Framework 的源代码在线调试, <a target="_blank" href="http://weblogs.asp.net/scottgu/">ScottGu</a>博客也有过介绍使用的方法, <a target="_blank" href="http://www.codeplex.com/netmassdownloader">Codeplex</a> 网站也有一个可以下载.NET源代码的工具. 一直以为.NET Framework 源代码也需要那么查看, 最近博客园的一片介绍VS2010的博文使自己发现其源代码微软本身提供下载, 不过博文地址已经不记得了. 直接给出下载地址:</p>
<blockquote>
<p><a title="http://referencesource.microsoft.com/netframework.aspx" href="http://referencesource.microsoft.com/netframework.aspx">http://referencesource.microsoft.com/netframework.aspx</a></p>
</blockquote>
<p>小强下载了两项: .NET 8.0/ .Net 4, 搞不清楚这个8.0 是啥意识, 两个文件包加起来大概200M, 都为MSI格式. 安装后发现这个8.0的实际就是.NET 3.5 SP1 的&quot;所有&quot;Framework源代码, 4 的为.NET 4 Framework源代码. 好像每个版本源代码都是完整的.</p>
<p>安装后文件夹搞死人, 每个.cs文件都是一个&quot;文件名文件夹+版本号文件夹+文件&quot;的层次存放的, 例如System.Array类的存放路径为\System\Array.cs\1305376\Array.cs, 可能是TFS的缘故吧,自己也不甚了解, 只是源代码文件路径有一个&quot;DEVDIV_TFS&quot;的文件夹. 写了一个剔除版本号文件夹的一段代码:</p>
<div id="codeSnippetWrapper" class="csharpcode-wrapper">
<div id="codeSnippet" class="csharpcode">
<pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="kwrd">using</span> System;</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum2" class="lnum">   2:</span> <span class="kwrd">using</span> System.Collections.Generic;</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum3" class="lnum">   3:</span> <span class="kwrd">using</span> System.Text;</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum4" class="lnum">   4:</span> <span class="kwrd">using</span> System.IO;</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum5" class="lnum">   5:</span> <span class="kwrd">using</span> System.Threading.Tasks;</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum6" class="lnum">   6:</span>&nbsp; </pre>
<!--CRLF-->
<pre class="alt"><span id="lnum7" class="lnum">   7:</span> <span class="kwrd">namespace</span> FixSrcConsoleApplication1</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum8" class="lnum">   8:</span> {</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum9" class="lnum">   9:</span>     <span class="kwrd">class</span> Program</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum10" class="lnum">  10:</span>     {</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum11" class="lnum">  11:</span>         <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum12" class="lnum">  12:</span>         <span class="rem">/// 源文件夹</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum13" class="lnum">  13:</span>         <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum14" class="lnum">  14:</span>         <span class="kwrd">static</span> <span class="kwrd">string</span> srcPath = <span class="str">@&quot;D:\RefSrc\Source\.Net\4.0\DEVDIV_TFS\Dev10\Releases\RTMRel&quot;</span>;</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum15" class="lnum">  15:</span>         <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum16" class="lnum">  16:</span>         <span class="rem">/// 目标文件夹</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum17" class="lnum">  17:</span>         <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum18" class="lnum">  18:</span>         <span class="kwrd">static</span> <span class="kwrd">string</span> destPath = <span class="str">@&quot;D:\RTMRel&quot;</span>;</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum19" class="lnum">  19:</span>&nbsp; </pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum20" class="lnum">  20:</span>         <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum21" class="lnum">  21:</span>         {</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum22" class="lnum">  22:</span>             FixPath(srcPath);</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum23" class="lnum">  23:</span>         }</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum24" class="lnum">  24:</span>         <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum25" class="lnum">  25:</span>         <span class="rem">/// 处理</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum26" class="lnum">  26:</span>         <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum27" class="lnum">  27:</span>         <span class="rem">/// &lt;param name=&quot;currentPath&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum28" class="lnum">  28:</span>         <span class="kwrd">static</span> <span class="kwrd">void</span> FixPath(<span class="kwrd">string</span> currentPath)</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum29" class="lnum">  29:</span>         {</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum30" class="lnum">  30:</span>             var currentDir = <span class="kwrd">new</span> DirectoryInfo(currentPath);</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum31" class="lnum">  31:</span>             var files = currentDir.GetFiles();</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum32" class="lnum">  32:</span>             var dirs = currentDir.GetDirectories();</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum33" class="lnum">  33:</span>             <span class="rem">//处理文件</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum34" class="lnum">  34:</span>             <span class="kwrd">if</span> (files != <span class="kwrd">null</span> &amp;&amp; files.Length != 0)</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum35" class="lnum">  35:</span>             {</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum36" class="lnum">  36:</span>                 Parallel.ForEach(files, file =&gt;</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum37" class="lnum">  37:</span>                 {</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum38" class="lnum">  38:</span>                     <span class="rem">//foreach (var file in files)</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum39" class="lnum">  39:</span>                     <span class="rem">//{</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum40" class="lnum">  40:</span>                     var ndestFilePath = destPath + file.FullName.Remove(0, srcPath.Length);</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum41" class="lnum">  41:</span>&nbsp; </pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum42" class="lnum">  42:</span>                     TryCreatePath(ndestFilePath);</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum43" class="lnum">  43:</span>&nbsp; </pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum44" class="lnum">  44:</span>                     File.Copy(file.FullName, ndestFilePath, <span class="kwrd">true</span>);</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum45" class="lnum">  45:</span>                     <span class="rem">//}</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum46" class="lnum">  46:</span>                 });</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum47" class="lnum">  47:</span>             }</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum48" class="lnum">  48:</span>             <span class="rem">//文件夹处理</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum49" class="lnum">  49:</span>             <span class="kwrd">if</span> (dirs != <span class="kwrd">null</span> &amp;&amp; dirs.Length != 0)</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum50" class="lnum">  50:</span>             {</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum51" class="lnum">  51:</span>                 Parallel.ForEach(dirs, dir =&gt;</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum52" class="lnum">  52:</span>                 {</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum53" class="lnum">  53:</span>                     <span class="rem">//foreach (var dir in dirs)</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum54" class="lnum">  54:</span>                     <span class="rem">//{</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum55" class="lnum">  55:</span>                     <span class="rem">//.cs结尾的文件夹特殊处理</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum56" class="lnum">  56:</span>                     <span class="kwrd">if</span> (dir.FullName.EndsWith(<span class="str">&quot;.cs&quot;</span>))</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum57" class="lnum">  57:</span>                     {</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum58" class="lnum">  58:</span>                         var dirFile = dir.GetDirectories()[0].GetFiles()[0];</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum59" class="lnum">  59:</span>                         var ndestFilePath = destPath + dir.FullName.Remove(0, srcPath.Length);</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum60" class="lnum">  60:</span>&nbsp; </pre>
<!--CRLF-->
<pre class="alt"><span id="lnum61" class="lnum">  61:</span>                         TryCreatePath(ndestFilePath);</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum62" class="lnum">  62:</span>&nbsp; </pre>
<!--CRLF-->
<pre class="alt"><span id="lnum63" class="lnum">  63:</span>                         File.Copy(dirFile.FullName, ndestFilePath, <span class="kwrd">true</span>);</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum64" class="lnum">  64:</span>                     }</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum65" class="lnum">  65:</span>                     <span class="kwrd">else</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum66" class="lnum">  66:</span>                         FixPath(dir.FullName);</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum67" class="lnum">  67:</span>                     <span class="rem">//}</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum68" class="lnum">  68:</span>                 });</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum69" class="lnum">  69:</span>             }</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum70" class="lnum">  70:</span>         }</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum71" class="lnum">  71:</span>         <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum72" class="lnum">  72:</span>         <span class="rem">/// 尝试创建文件所在路径</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum73" class="lnum">  73:</span>         <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum74" class="lnum">  74:</span>         <span class="rem">/// &lt;param name=&quot;ndestFilePath&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->
<pre class="alt"><span id="lnum75" class="lnum">  75:</span>         <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> TryCreatePath(<span class="kwrd">string</span> ndestFilePath)</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum76" class="lnum">  76:</span>         {</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum77" class="lnum">  77:</span>             var ndestPath = Path.GetDirectoryName(ndestFilePath);</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum78" class="lnum">  78:</span>             <span class="kwrd">if</span> (!Directory.Exists(ndestPath))</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum79" class="lnum">  79:</span>                 Directory.CreateDirectory(ndestPath);</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum80" class="lnum">  80:</span>         }</pre>
<!--CRLF-->
<pre class="alt"><span id="lnum81" class="lnum">  81:</span>     }</pre>
<!--CRLF-->
<pre class="alteven"><span id="lnum82" class="lnum">  82:</span> }</pre>
<!--CRLF--></div>
</div>
<p>代码实现上使用了.NET 4提供的<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/dd537608.aspx">并行特征</a>, 使用并行来实现功能后发现执行效率不是一般的快.</p>
<p>查看源代码的另外一个发现, 好多源代码文件内都有重复代码, 还是上面的Array.cs文件2602行又蹦出了一个 clsss Array{}.</p>
<p>几个感兴趣的代码实现:</p>
<blockquote>
<p>\RTMRel\ndp\clr\src\BCL\System\Collections\Concurrent&nbsp;&nbsp;&nbsp; .NET 提供的几个并行容器实现部分</p>
<p>\RTMRel\ndp\fx\src\xsp\System\Web\Cache&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Web.Cache 实现部分部分(System.Runtime.Cache实现没有找到)</p>
<p>\RTMRel\ndp\fx\src\Core\System\Threading\<a title="ReaderWriterLockSlim" target="_blank" href="http://msdn.microsoft.com/zh-cn/library/bb355025.aspx">ReaderWriterLockSlim</a>&nbsp;&nbsp;&nbsp; .NET 3.5 开始提供的读写锁实现</p>
<p>&nbsp;</p>
</blockquote>
<p>一些链接:</p>
<blockquote>
<p>配置VS2008调试.NET源代码 <a title="http://www.cnblogs.com/yuyijq/archive/2008/01/17/1042521.html" href="http://www.cnblogs.com/yuyijq/archive/2008/01/17/1042521.html">http://www.cnblogs.com/yuyijq/archive/2008/01/17/1042521.html</a></p>
</blockquote>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=485</link>
			<title><![CDATA[使用异步调用来使方法等待执行]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Sun,20 Jun 2010 11:32:33 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=485</guid>
		<description><![CDATA[<p>.NET 开发上可能会碰到以下情况:</p>  <blockquote>   <p>1.等待一个方法的调用, 判断是否过期, 如果过期, 调用被忽略;</p>    <p>2.调用一个外部服务接口, 如果调用过期, 停顿一段时间在调用;</p>    <p>3.其他调用等待.</p> </blockquote>  <p>这种需求的实现可以使用.NET提供的异步编程方式来解决. 详细查看 <a href="http://msdn.microsoft.com/zh-cn/library/2e08f6yc(VS.80).aspx" target="_blank">MSDN这里</a>. 大致方法为:&quot;使用 <a href="http://msdn.microsoft.com/zh-cn/library/kzy257t0(VS.80).aspx" target="_blank">WaitHandle</a> 等待异步调用&quot;, 代码示例直接粘贴自 <a href="http://msdn.microsoft.com/zh-cn/library/2e08f6yc(VS.80).aspx" target="_blank">MSDN</a> 相应代码:</p>  <div id="codeSnippetWrapper" class="csharpcode-wrapper">   <div id="codeSnippet" class="csharpcode">     <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="kwrd">using</span> System;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> <span class="kwrd">using</span> System.Threading;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span> <span class="kwrd">namespace</span> Examples.AdvancedProgramming.AsynchronousOperations</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span> {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span>     <span class="kwrd">public</span> <span class="kwrd">class</span> AsyncDemo</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span>         <span class="rem">// The method to be executed asynchronously.</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span>         <span class="kwrd">public</span> <span class="kwrd">string</span> TestMethod(<span class="kwrd">int</span> callDuration, <span class="kwrd">out</span> <span class="kwrd">int</span> threadId)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span>         {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span>             Console.WriteLine(<span class="str">&quot;Test method begins.&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>             Thread.Sleep(callDuration);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span>             threadId = Thread.CurrentThread.ManagedThreadId; <span class="rem">//获取进程执行进程号</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span>             <span class="kwrd">return</span> String.Format(<span class="str">&quot;My call time was {0}.&quot;</span>, callDuration.ToString());</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span>         }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span>     }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum17" class="lnum">  17:</span>     <span class="rem">// The delegate must have the same signature as the method</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum18" class="lnum">  18:</span>     <span class="rem">// it will call asynchronously. 这里定义一个异步回调所用的代理</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum19" class="lnum">  19:</span>     <span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">string</span> AsyncMethodCaller(<span class="kwrd">int</span> callDuration, <span class="kwrd">out</span> <span class="kwrd">int</span> threadId);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum20" class="lnum">  20:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum21" class="lnum">  21:</span>     <span class="kwrd">public</span> <span class="kwrd">class</span> AsyncMain</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum22" class="lnum">  22:</span>     {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum23" class="lnum">  23:</span>         <span class="kwrd">static</span> <span class="kwrd">void</span> Main()</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum24" class="lnum">  24:</span>         {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum25" class="lnum">  25:</span>             <span class="rem">// The asynchronous method puts the thread id here.</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum26" class="lnum">  26:</span>             <span class="kwrd">int</span> threadId;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum27" class="lnum">  27:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum28" class="lnum">  28:</span>             <span class="rem">// Create an instance of the test class.</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum29" class="lnum">  29:</span>             AsyncDemo ad = <span class="kwrd">new</span> AsyncDemo();</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum30" class="lnum">  30:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum31" class="lnum">  31:</span>             <span class="rem">// Create the delegate. 创建代理</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum32" class="lnum">  32:</span>             AsyncMethodCaller caller = <span class="kwrd">new</span> AsyncMethodCaller(ad.TestMethod);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum33" class="lnum">  33:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum34" class="lnum">  34:</span>             <span class="rem">// Initiate the asychronous call. 开始异步调用</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum35" class="lnum">  35:</span>             IAsyncResult result = caller.BeginInvoke(3000,</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum36" class="lnum">  36:</span>                 <span class="kwrd">out</span> threadId, <span class="kwrd">null</span>, <span class="kwrd">null</span>);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum37" class="lnum">  37:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum38" class="lnum">  38:</span>             Thread.Sleep(0);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum39" class="lnum">  39:</span>             Console.WriteLine(<span class="str">&quot;Main thread {0} does some work.&quot;</span>,</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum40" class="lnum">  40:</span>                 Thread.CurrentThread.ManagedThreadId);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum41" class="lnum">  41:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum42" class="lnum">  42:</span>             <span class="rem">// Wait for the WaitHandle to become signaled.等待方法执行知道结束</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum43" class="lnum">  43:</span>             result.AsyncWaitHandle.WaitOne(); </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum44" class="lnum">  44:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum45" class="lnum">  45:</span>             <span class="rem">// Perform additional processing here.</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum46" class="lnum">  46:</span>             <span class="rem">// Call EndInvoke to retrieve the results.返回执行结束的结果</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum47" class="lnum">  47:</span>             <span class="kwrd">string</span> returnValue = caller.EndInvoke(<span class="kwrd">out</span> threadId, result);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum48" class="lnum">  48:             </span><span class="rem">// 打印结果, 查看异步执行是否的进程号, 发现跟主进程号不一样, 说明是异步执行</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum49" class="lnum">  49:</span>             Console.WriteLine(<span class="str">&quot;The call executed on thread {0}, with return value \&quot;{1}\&quot;.&quot;</span>,</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum50" class="lnum">  50:</span>                 threadId, returnValue);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum51" class="lnum">  51:</span>         }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum52" class="lnum">  52:</span>     }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum53" class="lnum">  53:</span> }</pre>
<!--CRLF--></div>
</div>

<p>代码43行使用<a href="http://msdn.microsoft.com/zh-cn/library/58195swd(VS.80).aspx" target="_blank">System.Threading.WaitHandle.WaitOne()</a> 来等待BeginInvoke的线程结束, WaitOne方法有一个可以设置等待时间的重载实现:&quot;public virtual bool <a href="http://msdn.microsoft.com/zh-cn/library/kzy257t0(VS.80).aspx" target="_blank">WaitOne</a>(int millisecondsTimeout);&quot;, 设置等待时间,&#160; 过期后等待结束, 直接返回false, 主线程做相应处理, 如下:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="kwrd">if</span> (result.AsyncWaitHandle.WaitOne(1000))</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span>     <span class="rem">// Perform additional processing here.</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span>     <span class="rem">// Call EndInvoke to retrieve the results.</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span>     <span class="kwrd">string</span> returnValue = caller.EndInvoke(<span class="kwrd">out</span> threadId, result);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span>     Console.WriteLine(<span class="str">&quot;The call executed on thread {0}, with return value \&quot;{1}\&quot;.&quot;</span>,</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span>         threadId, returnValue);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span> }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span> <span class="kwrd">else</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span> {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>     Console.WriteLine(<span class="str">&quot;方法执行, 等待过期.&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span> }</pre>
<!--CRLF--></div>
</div>

<p>等待1秒, 后直接返回,&#160; 因为TestMethod方法被挂起了3秒, 所以永远返回到12行, 调用实际上'代码1'中的14行是可以执行到的, 只是在他返回的时候, 主线程不再处理而已. 本等待方法还可以用作异步执行某些行为, 但不关心执行的结果.</p>

<p>因为使用BeginInvoke创建的是后台线程, 所以如果主线程结束, 进程会直接结束, 如果被异步调用的方法可能会发生没有被执行结束而退出的情况.</p>

<p>参考地址:</p>

<blockquote>
  <p><a title="http://msdn.microsoft.com/zh-cn/library/2e08f6yc(VS.80).aspx" href="http://msdn.microsoft.com/zh-cn/library/2e08f6yc(VS.80).aspx">http://msdn.microsoft.com/zh-cn/library/2e08f6yc(VS.80).aspx</a></p>

  <p><a title="http://msdn.microsoft.com/zh-cn/library/ms228969(VS.80).aspx" href="http://msdn.microsoft.com/zh-cn/library/ms228969(VS.80).aspx">http://msdn.microsoft.com/zh-cn/library/ms228969(VS.80).aspx</a></p>
</blockquote>

<p>2010-06-20&#160; 补充:</p>

<p>最近阅读&lt;<a href="http://book.douban.com/subject/4214617/" target="_blank">Windows并发编程指南</a>&gt;, 书第八章讲到&quot;异步编程模型&quot;中讲到&quot;委托类型&quot;实现BeginInvoke和EndInvoke的方法&quot;远程组件&quot;, 并写到&quot;尽量不要使用它们(BeginInvoke,EndInvoke来实现异步)...他将给异步调用带来非常大的开销&quot;. 以为&quot;远程组件&quot;为&quot;Remoting Components&quot;, 但印象中.NET上没有这个名称, 翻看本书的英文版才知道为&quot;Remoting Infrastructure&quot;, 按MSDN的翻译应该为&quot;远程处理基本结构&quot;.&#160; 不兴的是这个&quot;远程处理基本结构&quot;没有找到通俗讲解的地方, 只找到一下一个相关链接:</p>

<blockquote>
  <p><a title="http://msdn.microsoft.com/zh-cn/library/system.runtime.remoting.messaging.messagesurrogatefilter(v=VS.90).aspx" href="http://msdn.microsoft.com/zh-cn/library/system.runtime.remoting.messaging.messagesurrogatefilter(v=VS.90).aspx">http://msdn.microsoft.com/zh-cn/library/system.runtime.remoting.messaging.messagesurrogatefilter(v=VS.90).aspx</a>

    <br /><a title="http://msdn.microsoft.com/zh-cn/library/kwdt6w2k(v=VS.71).aspx" href="http://msdn.microsoft.com/zh-cn/library/kwdt6w2k(v=VS.71).aspx">http://msdn.microsoft.com/zh-cn/library/kwdt6w2k(v=VS.71).aspx</a>

    <br /><a title="http://msdn.microsoft.com/zh-cn/magazine/cc301332(en-us).aspx" href="http://msdn.microsoft.com/zh-cn/magazine/cc301332(en-us).aspx">http://msdn.microsoft.com/zh-cn/magazine/cc301332(en-us).aspx</a>

    <br /><a title="http://www.cnblogs.com/artech/archive/2008/01/31/1059492.html" href="http://www.cnblogs.com/artech/archive/2008/01/31/1059492.html">http://www.cnblogs.com/artech/archive/2008/01/31/1059492.html</a></p></blockquote>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=480</link>
			<title><![CDATA[编程中使用的数学(2):权重随机]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Sun,02 May 2010 14:18:43 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=480</guid>
		<description><![CDATA[<p>博客也停了半年多了, 半年内好多事情, 时间和机会可能都不适合写下点东西, 2010年了, 开个好头. <a title="编程中使用的数学" href="http://zhq.ahau.edu.cn/blog/article/444.htm" target="_blank">之前的一篇</a>写在2008年6月份, 这篇写在一年半以后, 真是个笑话.</p>  <p>步入正题. '权重随机'是去年九十月份工作中一个抽奖问题引出的. 需求需要达到不同奖品需要有不同的获得几率, 在查找资料中找到了以下几篇文章:</p>  <ol>   <li><a href="http://zzk.cnblogs.com/s?w=%e6%9d%83%e9%87%8d+%e9%9a%8f%e6%9c%ba&amp;p=1">http://zzk.cnblogs.com/s?w=%e6%9d%83%e9%87%8d+%e9%9a%8f%e6%9c%ba&amp;p=1</a> </li>    <li>生成安全的随机数&#160;&#160;&#160; <a href="http://www.cnblogs.com/rainy/archive/2006/08/05/468670.html">http://www.cnblogs.com/rainy/archive/2006/08/05/468670.html</a> </li>    <li><a title="http://www.cnblogs.com/wdfrog/archive/2007/12/14/994963.html" href="http://www.cnblogs.com/wdfrog/archive/2007/12/14/994963.html">http://www.cnblogs.com/wdfrog/archive/2007/12/14/994963.html</a> </li> </ol>  <p>按'权重随机'过程假设编程平台上的提供的随机发生器产生的每个结果都是等概率的(实际上.NET平台和其他编程平台上提供了很好的满足本条件的随机发生器), 另外一个条件是'权重'以非负整形数的形式提供. 假如'待随机项'集合为 {I,…,I<sub>n</sub>}, 其对应的权重结合为 {W,…,W<sub>n</sub>}, 在产生随机数的时候随机的范围为(0, ∑W<sub>i</sub>), 其中∑W<sub>i</sub>表示为'所有待随机项权重的和', 使用随机器产生的随机结果假如在 (w<sub>i</sub>, w<sub>i+1</sub>] 范围了(注意开闭区间), 则当前的随机结果项为I<sub>i</sub>. 可以简单理解为以下的文字描述: 把所有'待随机项'的权重和作为要随机的上限, '随机项'前后排好序, 生成的随机结果如果在某两个个随机项(A,B)权重区间范围内, 那么随机的结果项就取A. </p>  <p>经过上面几篇的文章的研读和抄袭后就出了今天文章中的代码, 下面是'权重随机'核心代码部分:</p>  <div id="codeSnippetWrapper" class="csharpcode-wrapper">   <div id="codeSnippet" class="csharpcode">     <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> <span class="rem">/// 根据随机器和权重和得到随机结果项</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span> <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span> <span class="rem">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span> <span class="rem">/// &lt;param name=&quot;items&quot;&gt;权重项&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span> <span class="rem">/// &lt;param name=&quot;rng&quot;&gt;随机化器&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span> <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span> <span class="kwrd">static</span> WeightItem&lt;T&gt; GetRandowmItem&lt;T&gt;(IEnumerable&lt;WeightItem&lt;T&gt;&gt; items, IRNG rng)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span> {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span>     <span class="kwrd">int</span> sum = GetSum(items);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span>     <span class="kwrd">int</span> rand = rng.Next(0, sum);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>     <span class="kwrd">int</span> cSum = 0;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span>     <span class="kwrd">foreach</span> (var item <span class="kwrd">in</span> items)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span>     {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span>         cSum += item.Weight;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span>         <span class="kwrd">if</span> (cSum &gt; rand)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum17" class="lnum">  17:</span>             <span class="kwrd">return</span> item;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum18" class="lnum">  18:</span>     }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum19" class="lnum">  19:</span>     <span class="kwrd">return</span> <span class="kwrd">new</span> WeightItem&lt;T&gt;();    </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum20" class="lnum">  20:</span> }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum21" class="lnum">  21:</span> <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum22" class="lnum">  22:</span> <span class="rem">/// 得到当前随机项权重和</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum23" class="lnum">  23:</span> <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum24" class="lnum">  24:</span> <span class="rem">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum25" class="lnum">  25:</span> <span class="rem">/// &lt;param name=&quot;items&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum26" class="lnum">  26:</span> <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum27" class="lnum">  27:</span> <span class="kwrd">static</span> <span class="kwrd">int</span> GetSum&lt;T&gt;(IEnumerable&lt;WeightItem&lt;T&gt;&gt; items)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum28" class="lnum">  28:</span> {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum29" class="lnum">  29:</span>     <span class="kwrd">int</span> sum = 0;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum30" class="lnum">  30:</span>     <span class="kwrd">foreach</span> (var item <span class="kwrd">in</span> items)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum31" class="lnum">  31:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum32" class="lnum">  32:</span>         sum += item.Weight;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum33" class="lnum">  33:</span>     }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum34" class="lnum">  34:</span>     <span class="kwrd">return</span> sum;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum35" class="lnum">  35:</span> }</pre>
<!--CRLF--></div>
</div>

<p>10-18行是完成整个权重随机过程, 代码与之前某篇文章&lt;<a title="http://www.cnblogs.com/wdfrog/archive/2007/12/14/994963.html" href="http://www.cnblogs.com/wdfrog/archive/2007/12/14/994963.html">http://www.cnblogs.com/wdfrog/archive/2007/12/14/994963.html</a>&gt;的实现一致, 只是这个&quot;IRNG rng&quot;有点不一样, IRNG是一个随机发生器的封装接口:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> <span class="rem">/// 随机化器 接口类</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span> <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span> <span class="kwrd">public</span> <span class="kwrd">interface</span> IRNG</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span> {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span>     <span class="rem">/// 返回一个指定范围内的随机数</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span>     <span class="rem">/// &lt;param name=&quot;minValue&quot;&gt;返回的随机数的下界(随机数可取该下界值)&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span>     <span class="rem">/// &lt;param name=&quot;maxValue&quot;&gt; 返回的随机数的上界(随机数不能取该上界值), maxValue 必须大于或等于 minValue&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span>     <span class="rem">/// &lt;returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>     <span class="rem">/// 一个大于或等于 minValue 且小于 maxValue 的 32 位带符号整数, 即: 返回的值范围包括 minValue 但不包括 maxValue. 如果</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span>     <span class="rem">/// minValue 等于 maxValue, 则返回 minValue</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span>     <span class="rem">/// &lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span>     <span class="kwrd">int</span> Next(<span class="kwrd">int</span> minValue, <span class="kwrd">int</span> maxValue);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span> }</pre>
<!--CRLF--></div>
</div>

<p>封装的目的是 .NET 中默认的 <a href="http://msdn.microsoft.com/zh-cn/library/system.random(VS.80).aspx" target="_blank">System.Random</a> 随机可能不够完美, 或不够安全, 所以封装了使用 <a href="http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rngcryptoserviceprovider(VS.80).aspx" target="_blank">System.Security.Cryptography.RNGCryptoServiceProvider</a> 来随机的随机器. int Next(int minValue, int maxValue) 实现如下:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="kwrd">public</span> <span class="kwrd">int</span> Next(<span class="kwrd">int</span> minValue, <span class="kwrd">int</span> maxValue)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span>     <span class="kwrd">if</span> (minValue &gt; maxValue)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span>         <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentOutOfRangeException(<span class="str">&quot;minValue&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span>     <span class="kwrd">int</span> numSides = maxValue - minValue;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span>     <span class="kwrd">byte</span>[] bn = <span class="kwrd">new</span> <span class="kwrd">byte</span>[4];</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span>     <span class="kwrd">int</span> num = 0;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span>     rand.GetBytes(bn);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span>     num = BitConverter.ToInt32(bn, 0);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>     num = num % numSides;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span>     <span class="kwrd">if</span> (num &lt; 0)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span>         num = -num;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span>     <span class="kwrd">return</span> num + minValue;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum17" class="lnum">  17:</span> }</pre>
<!--CRLF--></div>
</div>

<p>上面的实现实际上来自于这里:&lt;<a title="http://www.cnblogs.com/rainy/archive/2006/08/05/468670.html" href="http://www.cnblogs.com/rainy/archive/2006/08/05/468670.html">http://www.cnblogs.com/rainy/archive/2006/08/05/468670.html</a>&gt;. 由于使用 <a href="http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rngcryptoserviceprovider(VS.80).aspx" target="_blank">RNGCryptoServiceProvider</a> 来随机, 可能计算有点慢, 不过没有详细测试.</p>

<p>为了增加本文中实现的权重随机的通用性, 还实现了一个泛型'随机项'类,&#160; 如下:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> <span class="rem">/// 权重项</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span> <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span> <span class="rem">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> WeightItem&lt;T&gt;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span> {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span>     <span class="rem">/// 权重</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span>     <span class="kwrd">public</span> <span class="kwrd">int</span> Weight { get; set; }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>     <span class="rem">/// 项值</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span>     <span class="kwrd">public</span> T ItemValue { get; set; }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span>     <span class="rem">/// </span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum17" class="lnum">  17:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum18" class="lnum">  18:</span>     <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum19" class="lnum">  19:</span>     <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">string</span> ToString()</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum20" class="lnum">  20:</span>     {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum21" class="lnum">  21:</span>         <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">&quot;[ItemValue:{0}, Weight:{1}]&quot;</span>, ItemValue, Weight);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum22" class="lnum">  22:</span>     }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum23" class="lnum">  23:</span> }</pre>
<!--CRLF--></div>
</div>

<p>测试代码如下:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="rem">//权重随机项-1</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> List&lt;WeightItem&lt;<span class="kwrd">char</span>&gt;&gt; wItems = <span class="kwrd">new</span> List&lt;WeightItem&lt;<span class="kwrd">char</span>&gt;&gt;() { </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span>     <span class="kwrd">new</span> WeightItem&lt;<span class="kwrd">char</span>&gt;(){ ItemValue=<span class="str">'A'</span>, Weight=60},</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span>     <span class="kwrd">new</span> WeightItem&lt;<span class="kwrd">char</span>&gt;(){ ItemValue=<span class="str">'B'</span>, Weight=30},</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span>     <span class="kwrd">new</span> WeightItem&lt;<span class="kwrd">char</span>&gt;(){ ItemValue=<span class="str">'C'</span>, Weight=5},</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span>     <span class="kwrd">new</span> WeightItem&lt;<span class="kwrd">char</span>&gt;(){ ItemValue=<span class="str">'D'</span>, Weight=5},</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span> };</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span> <span class="rem">//随机器</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span> IRNG rng = <span class="kwrd">new</span> RNG.SysCspRNG();</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span> <span class="rem">//结果保存字典</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span> Dictionary&lt;<span class="kwrd">char</span>, <span class="kwrd">int</span>&gt; dicResult = <span class="kwrd">new</span> Dictionary&lt;<span class="kwrd">char</span>, <span class="kwrd">int</span>&gt;();</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span> <span class="rem">//根据随机项取得1000000次随机结果</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span> List&lt;WeightItem&lt;<span class="kwrd">char</span>&gt;&gt; resultList = RandomHelper.GetRandomItemsFromWeightItems(wItems, rng, 1000000);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span> <span class="kwrd">foreach</span> (var item <span class="kwrd">in</span> resultList)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span> {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span>     <span class="kwrd">if</span> (dicResult.ContainsKey(item.ItemValue))</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum17" class="lnum">  17:</span>         dicResult[item.ItemValue]++;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum18" class="lnum">  18:</span>     <span class="kwrd">else</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum19" class="lnum">  19:</span>         dicResult.Add(item.ItemValue, 0);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum20" class="lnum">  20:</span> }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum21" class="lnum">  21:</span> <span class="rem">//打印结果</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum22" class="lnum">  22:</span> <span class="kwrd">foreach</span> (var item <span class="kwrd">in</span> dicResult)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum23" class="lnum">  23:</span> {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum24" class="lnum">  24:</span>     Console.WriteLine(<span class="str">&quot;{0}:{1}&quot;</span>, item.Key, item.Value);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum25" class="lnum">  25:</span> }</pre>
<!--CRLF--></div>
</div>

<p>测试代码中随机项为 {A, B, C, D}, 权重分别为 {60, 30, 5, 5}, 为了计算方面随机 100 的倍数.次 代码中 'IRNG rng = new RNG.SysCspRNG();' 即为实现了IRNG接口使用 <a href="http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rngcryptoserviceprovider(VS.80).aspx" target="_blank">RNGCryptoServiceProvider</a> 来随机的随机器. RandomHelper.GetRandomItemsFromWeightItems 是封装了之前的'权重随机'函数的一个支持批量随机的静态方法, 如下:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> <span class="rem">/// 根据'权重项列表'和'随机次数'得到'随机项结果列表'</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span> <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span> <span class="rem">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span> <span class="rem">/// &lt;param name=&quot;items&quot;&gt;权重项&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span> <span class="rem">/// &lt;param name=&quot;rng&quot;&gt;随机化器&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span> <span class="rem">/// &lt;param name=&quot;times&quot;&gt;随机次数&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span> <span class="rem">/// &lt;returns&gt;随机结果项列表&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span> <span class="kwrd">public</span> <span class="kwrd">static</span> List&lt;WeightItem&lt;T&gt;&gt; GetRandomItemsFromWeightItems&lt;T&gt;(IEnumerable&lt;WeightItem&lt;T&gt;&gt; items, IRNG rng, <span class="kwrd">int</span> times)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span> {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span>     <span class="kwrd">if</span> (items == <span class="kwrd">null</span>)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>         <span class="kwrd">throw</span> <span class="kwrd">new</span> System.ArgumentNullException(<span class="str">&quot;items&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span>     <span class="kwrd">if</span> (rng == <span class="kwrd">null</span>)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span>         <span class="kwrd">throw</span> <span class="kwrd">new</span> System.ArgumentNullException(<span class="str">&quot;rng&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span>     <span class="kwrd">if</span> (times &lt;0)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span>         <span class="kwrd">throw</span> <span class="kwrd">new</span> System.ArgumentException(<span class="str">&quot;'随机次数'必须大于0&quot;</span>,<span class="str">&quot;times&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum17" class="lnum">  17:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum18" class="lnum">  18:</span>     <span class="kwrd">int</span> sum = GetSum(items);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum19" class="lnum">  19:</span>     List&lt;WeightItem&lt;T&gt;&gt; list = <span class="kwrd">new</span> List&lt;WeightItem&lt;T&gt;&gt;(times);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum20" class="lnum">  20:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum21" class="lnum">  21:</span>     <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; times; i++)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum22" class="lnum">  22:</span>     {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum23" class="lnum">  23:</span>         list.Add(GetRandowmItem(items,rng, sum));</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum24" class="lnum">  24:</span>     }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum25" class="lnum">  25:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum26" class="lnum">  26:</span>     <span class="kwrd">return</span> list;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum27" class="lnum">  27:</span> }</pre>
<!--CRLF--></div>
</div>

<p>随机结果如下:</p>

<blockquote>
  <p>B:299348 
    <br />A:600648 

    <br />D:49713 

    <br />C:50287 </p>
</blockquote>

<p>基本是按&quot;A:60, B:30, C:5, D5&quot; 的权重比例来的.</p>

<p>就这么多内容, 另外推荐以下文章作为扩展阅读:</p>

<ol>
  <li><a title="http://security.ctocio.com.cn/securitycomment/385/8082385.shtml" href="http://security.ctocio.com.cn/securitycomment/385/8082385.shtml">http://security.ctocio.com.cn/securitycomment/385/8082385.shtml</a> </li>

  <li><a title="http://zh.linuxvirtualserver.org/node/35" href="http://zh.linuxvirtualserver.org/node/35">http://zh.linuxvirtualserver.org/node/35</a> </li>

  <li><a title="http://zh.linuxvirtualserver.org/node/36" href="http://zh.linuxvirtualserver.org/node/36">http://zh.linuxvirtualserver.org/node/36</a> </li>

  <li><a title="http://zh.linuxvirtualserver.org/node/37" href="http://zh.linuxvirtualserver.org/node/37">http://zh.linuxvirtualserver.org/node/37</a> </li>

  <li><a title="http://mindhacks.cn/2008/09/21/the-magical-bayesian-method/" href="http://mindhacks.cn/2008/09/21/the-magical-bayesian-method/">http://mindhacks.cn/2008/09/21/the-magical-bayesian-method/</a> </li>

  <li><a title="http://jun.wu.googlepages.com/beautyofmathematics" href="http://jun.wu.googlepages.com/beautyofmathematics">http://jun.wu.googlepages.com/beautyofmathematics</a> </li>
</ol>

<p>2010年5月2号补充:</p>

<p>给出一个证明详细的文章地址:<a title="http://www.cnblogs.com/miloyip/archive/2010/04/21/1717109.html" href="http://www.cnblogs.com/miloyip/archive/2010/04/21/1717109.html">http://www.cnblogs.com/miloyip/archive/2010/04/21/1717109.html</a>.&#160; 文章中从统计学角度详细说明了本方法的理论依据, 文章中图文并茂, 论据充分, 学术严谨(谐).</p>

<p>&#160;</p>

<p>&#160;</p>

<p>演示代码下载如下:</p>
<iframe style="padding-bottom: 0px; background-color: #fcfcfc; padding-left: 0px; width: 98px; padding-right: 0px; height: 115px; padding-top: 0px" title="Preview" marginheight="0" src="http://cid-e63ac91b5bfc8e30.skydrive.live.com/embedicon.aspx/Public/TUP.WeightRandom.zip" frameborder="0" marginwidth="0" scrolling="no"></iframe>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=484</link>
			<title><![CDATA[一个线程安全的FIFO缓存容器]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Sun,28 Mar 2010 19:52:08 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=484</guid>
		<description><![CDATA[<p>项目中需要一个缓存容器, 满足先进先出(FIFO)需求, 容量10000.&#160; 参考了几个LRU缓存容器实现, 使用字典(Dictionary)+队列(Queue)组合的方式来达到'插入/删除/读取'复杂度都为O(1), 缓存容器所以需要保持线程安全, 使用简单的lock语句来实现. 下面是实现代码:</p>  <div id="codeSnippetWrapper" class="csharpcode-wrapper">   <div id="codeSnippet" class="csharpcode">     <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> <span class="rem">/// LIFO 容器</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span> <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span> <span class="rem">/// &lt;typeparam name=&quot;TKey&quot;&gt;&lt;/typeparam&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span> <span class="rem">/// &lt;typeparam name=&quot;TValue&quot;&gt;&lt;/typeparam&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> ThreadSafeLifoDictionary&lt;TKey, TValue&gt;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span> {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span>     <span class="kwrd">private</span> Dictionary&lt;TKey, TValue&gt; _Dict = <span class="kwrd">new</span> Dictionary&lt;TKey, TValue&gt;();</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span>     <span class="kwrd">private</span> Queue&lt;TKey&gt; _Queue = <span class="kwrd">new</span> Queue&lt;TKey&gt;();</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span>     <span class="kwrd">private</span> <span class="kwrd">object</span> _LockObj = <span class="kwrd">new</span> <span class="kwrd">object</span>();</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span>     <span class="rem">/// 容量</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span>     <span class="kwrd">public</span> <span class="kwrd">int</span> Capacity { get; <span class="kwrd">private</span> set; }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum17" class="lnum">  17:</span>     <span class="kwrd">public</span> ThreadSafeLifoDictionary() : <span class="kwrd">this</span>(10000) { }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum18" class="lnum">  18:</span>     <span class="kwrd">public</span> ThreadSafeLifoDictionary(<span class="kwrd">int</span> capacity)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum19" class="lnum">  19:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum20" class="lnum">  20:</span>         <span class="kwrd">this</span>.Capacity = capacity;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum21" class="lnum">  21:</span>     }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum22" class="lnum">  22:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum23" class="lnum">  23:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum24" class="lnum">  24:</span>     <span class="rem">/// 当前项数</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum25" class="lnum">  25:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum26" class="lnum">  26:</span>     <span class="kwrd">public</span> <span class="kwrd">int</span> Count</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum27" class="lnum">  27:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum28" class="lnum">  28:</span>         get</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum29" class="lnum">  29:</span>         {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum30" class="lnum">  30:</span>             <span class="kwrd">lock</span> (_LockObj)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum31" class="lnum">  31:</span>             {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum32" class="lnum">  32:</span>                 <span class="kwrd">return</span> _Queue.Count;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum33" class="lnum">  33:</span>             }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum34" class="lnum">  34:</span>         }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum35" class="lnum">  35:</span>     }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum36" class="lnum">  36:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum37" class="lnum">  37:</span>     <span class="rem">/// 清空</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum38" class="lnum">  38:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum39" class="lnum">  39:</span>     <span class="kwrd">public</span> <span class="kwrd">void</span> Clear()</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum40" class="lnum">  40:</span>     {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum41" class="lnum">  41:</span>         <span class="kwrd">lock</span> (_LockObj)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum42" class="lnum">  42:</span>         {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum43" class="lnum">  43:</span>             _Queue.Clear();</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum44" class="lnum">  44:</span>             _Dict.Clear();</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum45" class="lnum">  45:</span>         }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum46" class="lnum">  46:</span>     }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum47" class="lnum">  47:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum48" class="lnum">  48:</span>     <span class="rem">/// 添加或更新已有项</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum49" class="lnum">  49:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum50" class="lnum">  50:</span>     <span class="rem">/// &lt;param name=&quot;key&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum51" class="lnum">  51:</span>     <span class="rem">/// &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum52" class="lnum">  52:</span>     <span class="rem">/// &lt;returns&gt;添加操作返回true, 更新操作返回false&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum53" class="lnum">  53:</span>     <span class="kwrd">public</span> <span class="kwrd">bool</span> AddOrUpdate(TKey key, TValue <span class="kwrd">value</span>)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum54" class="lnum">  54:</span>     {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum55" class="lnum">  55:</span>         <span class="kwrd">lock</span> (_LockObj)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum56" class="lnum">  56:</span>         {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum57" class="lnum">  57:</span>             CheckAndTruncate();</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum58" class="lnum">  58:</span>&#160; </pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum59" class="lnum">  59:</span>             <span class="kwrd">if</span> (<span class="kwrd">this</span>._Dict.ContainsKey(key))</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum60" class="lnum">  60:</span>             {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum61" class="lnum">  61:</span>                 <span class="kwrd">this</span>._Dict[key] = <span class="kwrd">value</span>;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum62" class="lnum">  62:</span>                 <span class="kwrd">return</span> <span class="kwrd">false</span>;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum63" class="lnum">  63:</span>             }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum64" class="lnum">  64:</span>             <span class="kwrd">else</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum65" class="lnum">  65:</span>             {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum66" class="lnum">  66:</span>                 <span class="kwrd">this</span>._Dict.Add(key, <span class="kwrd">value</span>);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum67" class="lnum">  67:</span>                 <span class="kwrd">this</span>._Queue.Enqueue(key);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum68" class="lnum">  68:</span>                 <span class="kwrd">return</span> <span class="kwrd">true</span>;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum69" class="lnum">  69:</span>             }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum70" class="lnum">  70:</span>         }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum71" class="lnum">  71:</span>     }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum72" class="lnum">  72:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum73" class="lnum">  73:</span>     <span class="rem">/// 包含指定项</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum74" class="lnum">  74:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum75" class="lnum">  75:</span>     <span class="rem">/// &lt;param name=&quot;key&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum76" class="lnum">  76:</span>     <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum77" class="lnum">  77:</span>     <span class="kwrd">public</span> <span class="kwrd">bool</span> ContainsKey(TKey key)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum78" class="lnum">  78:</span>     {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum79" class="lnum">  79:</span>         <span class="kwrd">lock</span> (_LockObj)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum80" class="lnum">  80:</span>         {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum81" class="lnum">  81:</span>             <span class="kwrd">return</span> _Dict.ContainsKey(key);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum82" class="lnum">  82:</span>         }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum83" class="lnum">  83:</span>     }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum84" class="lnum">  84:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum85" class="lnum">  85:</span>     <span class="rem">/// 获取指定项</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum86" class="lnum">  86:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum87" class="lnum">  87:</span>     <span class="rem">/// &lt;param name=&quot;key&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum88" class="lnum">  88:</span>     <span class="rem">/// &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum89" class="lnum">  89:</span>     <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum90" class="lnum">  90:</span>     <span class="kwrd">public</span> <span class="kwrd">bool</span> TryGetValue(TKey key, <span class="kwrd">out</span> TValue <span class="kwrd">value</span>)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum91" class="lnum">  91:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum92" class="lnum">  92:</span>         <span class="kwrd">lock</span> (_LockObj)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum93" class="lnum">  93:</span>         {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum94" class="lnum">  94:</span>             <span class="kwrd">return</span> <span class="kwrd">this</span>._Dict.TryGetValue(key, <span class="kwrd">out</span> <span class="kwrd">value</span>);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum95" class="lnum">  95:</span>         }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum96" class="lnum">  96:</span>     }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum97" class="lnum">  97:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum98" class="lnum">  98:</span>     <span class="rem">/// 达到容量值后删除十分之一的元素</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum99" class="lnum">  99:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum100" class="lnum"> 100:</span>     <span class="kwrd">private</span> <span class="kwrd">void</span> CheckAndTruncate()</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum101" class="lnum"> 101:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum102" class="lnum"> 102:</span>         <span class="kwrd">int</span> count = _Dict.Count;</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum103" class="lnum"> 103:</span>         <span class="kwrd">if</span> (count &gt;= Capacity)</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum104" class="lnum"> 104:</span>         {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum105" class="lnum"> 105:</span>             <span class="kwrd">int</span> needRemoveCount = count / 10;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum106" class="lnum"> 106:</span>             <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; needRemoveCount; i++)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum107" class="lnum"> 107:</span>             {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum108" class="lnum"> 108:</span>                 _Dict.Remove(_Queue.Peek());</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum109" class="lnum"> 109:</span>                 _Queue.Dequeue();</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum110" class="lnum"> 110:</span>             }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum111" class="lnum"> 111:</span>         }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum112" class="lnum"> 112:</span>     }</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum113" class="lnum"> 113:</span> }</pre>
<!--CRLF--></div>
</div>

<p>对于容量控制部分直接使用了&quot;<a title="蛙蛙推荐 LRU" href="http://www.cnblogs.com/onlytiancai/archive/2009/07/22/wawa_lru.html" target="_blank">蛙蛙推荐 LRU</a>&quot;的删除策略, 如果一下只删除一个容量的方式, 会因为频繁创建销毁对象, 而给垃圾回收带来压力. </p>

<p>一个例子: </p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span id="lnum1" class="lnum">   1:</span> <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum2" class="lnum">   2:</span> <span class="rem">/// 一个使用的例子</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum3" class="lnum">   3:</span> <span class="rem">/// 订阅缓存, 缓存UserID, ChapterID</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum4" class="lnum">   4:</span> <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum5" class="lnum">   5:</span> <span class="kwrd">public</span> <span class="kwrd">class</span> SubscribeCacheDemo</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum6" class="lnum">   6:</span> {</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum7" class="lnum">   7:</span>     ThreadSafeLifoDictionary&lt;<span class="kwrd">string</span>, <span class="kwrd">string</span>&gt; _LifoDict = <span class="kwrd">null</span>;</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum8" class="lnum">   8:</span>     <span class="kwrd">private</span> SubscribeCacheDemo(<span class="kwrd">int</span> capacity)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum9" class="lnum">   9:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum10" class="lnum">  10:</span>         _LifoDict = <span class="kwrd">new</span> ThreadSafeLifoDictionary&lt;<span class="kwrd">string</span>, <span class="kwrd">string</span>&gt;(capacity);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum11" class="lnum">  11:</span>     }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum12" class="lnum">  12:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum13" class="lnum">  13:</span>     <span class="rem">/// 实例(单件模式访问本缓存对象)</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum14" class="lnum">  14:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum15" class="lnum">  15:</span>     <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">readonly</span> SubscribeCacheDemo Instance = <span class="kwrd">new</span> SubscribeCacheDemo(100000);</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum16" class="lnum">  16:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum17" class="lnum">  17:</span>     <span class="rem">/// 订阅</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum18" class="lnum">  18:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum19" class="lnum">  19:</span>     <span class="rem">/// &lt;param name=&quot;userID&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum20" class="lnum">  20:</span>     <span class="rem">/// &lt;param name=&quot;ChapterID&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum21" class="lnum">  21:</span>     <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum22" class="lnum">  22:</span>     <span class="kwrd">public</span> <span class="kwrd">bool</span> Subscribe(<span class="kwrd">int</span> userID, <span class="kwrd">int</span> ChapterID)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum23" class="lnum">  23:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum24" class="lnum">  24:</span>         <span class="kwrd">return</span> _LifoDict.AddOrUpdate(<span class="kwrd">string</span>.Format(<span class="str">&quot;{0}-{1}&quot;</span>, userID, ChapterID), <span class="kwrd">string</span>.Empty);</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum25" class="lnum">  25:</span>     }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum26" class="lnum">  26:</span>     <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum27" class="lnum">  27:</span>     <span class="rem">/// 是否订阅过</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum28" class="lnum">  28:</span>     <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum29" class="lnum">  29:</span>     <span class="rem">/// &lt;param name=&quot;userID&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum30" class="lnum">  30:</span>     <span class="rem">/// &lt;param name=&quot;ChapterID&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum31" class="lnum">  31:</span>     <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum32" class="lnum">  32:</span>     <span class="kwrd">public</span> <span class="kwrd">bool</span> HasSubscribe(<span class="kwrd">int</span> userID, <span class="kwrd">int</span> ChapterID)</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum33" class="lnum">  33:</span>     {</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum34" class="lnum">  34:</span>         <span class="kwrd">return</span> _LifoDict.ContainsKey(<span class="kwrd">string</span>.Format(<span class="str">&quot;{0}-{1}&quot;</span>, userID, ChapterID));</pre>
<!--CRLF-->

    <pre class="alt"><span id="lnum35" class="lnum">  35:</span>     }</pre>
<!--CRLF-->

    <pre class="alteven"><span id="lnum36" class="lnum">  36:</span> }</pre>
<!--CRLF--></div>
</div>

<p>本缓存容器的性能上一个不好的地方是直接使用lock方式的锁, 频繁的锁缓存容器, 没有读写分开. 一个好的改进应该使用&quot;<a title="ReaderWriterLock" href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlock.aspx" target="_blank">ReaderWriterLock</a>/<a title="ReaderWriterLockSlim" href="http://msdn.microsoft.com/zh-cn/library/bb355025.aspx" target="_blank">ReaderWriterLockSlim</a>&quot;来读写分开, 关于这两种锁的性能对比可以参看<a href="http://www.cnblogs.com/JeffreyZhao/archive/2009/11/16/concurrent-cache-performance-improvement-2-benchmark.html" target="_blank">老赵的博文</a>.</p>

<p>&#160;</p>

<p>参考地址:</p>

<blockquote>
  <p><a title="http://www.cnblogs.com/onlytiancai/archive/2009/07/22/wawa_lru.html" href="http://www.cnblogs.com/onlytiancai/archive/2009/07/22/wawa_lru.html">http://www.cnblogs.com/onlytiancai/archive/2009/07/22/wawa_lru.html</a>&#160; 蛙蛙推荐 LRU</p>

  <p><a title="http://stackoverflow.com/questions/581119/object-cache-for-c" href="http://stackoverflow.com/questions/581119/object-cache-for-c">http://stackoverflow.com/questions/581119/object-cache-for-c</a></p>

  <p><a title="http://www.cnblogs.com/JeffreyZhao/archive/2009/11/16/concurrent-cache-performance-improvement-2-benchmark.html" href="http://www.cnblogs.com/JeffreyZhao/archive/2009/11/16/concurrent-cache-performance-improvement-2-benchmark.html">http://www.cnblogs.com/JeffreyZhao/archive/2009/11/16/concurrent-cache-performance-improvement-2-benchmark.html</a></p>

  <p><a title="http://nintegrate.com" href="http://nintegrate.com">http://nintegrate.com</a></p></blockquote>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=482</link>
			<title><![CDATA[验证码上的高效正弦扭曲函数]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Sat,30 Jan 2010 17:58:13 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=482</guid>
		<description><![CDATA[<p>c#上, 但凡需要给验证码添加扭曲的地方感觉都是一个函数:</p>  <div id="codeSnippetWrapper" class="csharpcode-wrapper">   <div id="codeSnippet" class="csharpcode">     <pre class="alt"><span class="preproc">#region</span> 产生波形滤镜效果</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt"> <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">double</span> PI = 3.1415926535897932384626433832795;</pre>
<!--CRLF-->

    <pre class="alteven"> <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">double</span> PI2 = 6.283185307179586476925286766559;</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven"> <span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"> <span class="rem">/// 正弦曲线Wave扭曲图片</span></pre>
<!--CRLF-->

    <pre class="alteven"> <span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"> <span class="rem">/// &lt;param name=&quot;srcBmp&quot;&gt;图片路径&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"> <span class="rem">/// &lt;param name=&quot;bXDir&quot;&gt;如果扭曲则选择为True&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"> <span class="rem">/// &lt;param name=&quot;nMultValue&quot;&gt;波形的幅度倍数，越大扭曲的程度越高，一般为3&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"> <span class="rem">/// &lt;param name=&quot;dPhase&quot;&gt;波形的起始相位，取值区间[0-2*PI)&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"> <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"> <span class="kwrd">public</span> System.Drawing.Bitmap TwistImage(Bitmap srcBmp, <span class="kwrd">bool</span> bXDir, <span class="kwrd">double</span> dMultValue, <span class="kwrd">double</span> dPhase)</pre>
<!--CRLF-->

    <pre class="alt"> {</pre>
<!--CRLF-->

    <pre class="alteven">     System.Drawing.Bitmap destBmp = <span class="kwrd">new</span> Bitmap(srcBmp.Width, srcBmp.Height);</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">     <span class="rem">// 将位图背景填充为白色</span></pre>
<!--CRLF-->

    <pre class="alt">     System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);</pre>
<!--CRLF-->

    <pre class="alteven">     graph.FillRectangle(<span class="kwrd">new</span> SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);</pre>
<!--CRLF-->

    <pre class="alt">     graph.Dispose();</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">     <span class="kwrd">double</span> dBaseAxisLen = bXDir ? (<span class="kwrd">double</span>)destBmp.Height : (<span class="kwrd">double</span>)destBmp.Width;</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">     <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; destBmp.Width; i++)</pre>
<!--CRLF-->

    <pre class="alteven">     {</pre>
<!--CRLF-->

    <pre class="alt">         <span class="kwrd">for</span> (<span class="kwrd">int</span> j = 0; j &lt; destBmp.Height; j++)</pre>
<!--CRLF-->

    <pre class="alteven">         {</pre>
<!--CRLF-->

    <pre class="alt">             <span class="kwrd">double</span> dx = 0;</pre>
<!--CRLF-->

    <pre class="alteven">             dx = bXDir ? (PI2 * (<span class="kwrd">double</span>)j) / dBaseAxisLen : (PI2 * (<span class="kwrd">double</span>)i) / dBaseAxisLen;</pre>
<!--CRLF-->

    <pre class="alt">             dx += dPhase;</pre>
<!--CRLF-->

    <pre class="alteven">             <span class="kwrd">double</span> dy = Math.Sin(dx);</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">             <span class="rem">// 取得当前点的颜色</span></pre>
<!--CRLF-->

    <pre class="alt">             <span class="kwrd">int</span> nOldX = 0, nOldY = 0;</pre>
<!--CRLF-->

    <pre class="alteven">             nOldX = bXDir ? i + (<span class="kwrd">int</span>)(dy * dMultValue) : i;</pre>
<!--CRLF-->

    <pre class="alt">             nOldY = bXDir ? j : j + (<span class="kwrd">int</span>)(dy * dMultValue);</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">             System.Drawing.Color color = srcBmp.GetPixel(i, j);</pre>
<!--CRLF-->

    <pre class="alteven">             <span class="kwrd">if</span> (nOldX &gt;= 0 &amp;&amp; nOldX &lt; destBmp.Width</pre>
<!--CRLF-->

    <pre class="alt">              &amp;&amp; nOldY &gt;= 0 &amp;&amp; nOldY &lt; destBmp.Height)</pre>
<!--CRLF-->

    <pre class="alteven">             {</pre>
<!--CRLF-->

    <pre class="alt">                 destBmp.SetPixel(nOldX, nOldY, color);</pre>
<!--CRLF-->

    <pre class="alteven">             }</pre>
<!--CRLF-->

    <pre class="alt">         }</pre>
<!--CRLF-->

    <pre class="alteven">     }</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">     <span class="kwrd">return</span> destBmp;</pre>
<!--CRLF-->

    <pre class="alt"> }</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt"> <span class="preproc">#endregion</span></pre>
<!--CRLF--></div>
</div>

<p>这个函数实在效率不高, 但还是很多人用. 可以从下面的地址得到结果:</p>

<p><a title="http://www.google.cn/search?hl=zh-CN&amp;newwindow=1&amp;q=Bitmap+TwistImage%28Bitmap+srcBmp&amp;btnG=Google+%E6%90%9C%E7%B4%A2&amp;aq=f&amp;oq=" href="http://www.google.cn/search?hl=zh-CN&amp;newwindow=1&amp;q=Bitmap+TwistImage%28Bitmap+srcBmp&amp;btnG=Google+%E6%90%9C%E7%B4%A2&amp;aq=f&amp;oq=">http://www.google.cn/search?hl=zh-CN&amp;newwindow=1&amp;q=Bitmap+TwistImage%28Bitmap+srcBmp&amp;btnG=Google+%E6%90%9C%E7%B4%A2&amp;aq=f&amp;oq=</a></p>

<p>下面是改进的这个函数:</p>

<p>&#160;</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span class="preproc">#region</span> 波浪函数</pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">//正弦起始点</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">double</span> PI = 3.141592;</pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">//正弦扭曲率</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">double</span> PI2 = 6.28318;</pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// 正弦曲线Wave扭曲图片</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;srcBmp&quot;&gt;图片路径&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;bXDir&quot;&gt;沿Y轴扭曲则选择为True&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;dMultValue&quot;&gt;波形的幅度倍数，越大扭曲的程度越高，一般为3&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;dPhase&quot;&gt;波形的起始相位，取值区间[0-2*PI)&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="kwrd">private</span> <span class="kwrd">static</span> Bitmap TwistImage(Bitmap srcBmp, <span class="kwrd">bool</span> bXDir, <span class="kwrd">double</span> dMultValue, <span class="kwrd">double</span> dPhase)</pre>
<!--CRLF-->

    <pre class="alt">{</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">return</span> TwistImage(srcBmp, bXDir, dMultValue, dPhase, 1f);</pre>
<!--CRLF-->

    <pre class="alt">}</pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// 正弦曲线Wave扭曲图片</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;srcBmp&quot;&gt;图片路径&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;bXDir&quot;&gt;沿Y轴扭曲则选择为True&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;dMultValue&quot;&gt;波形的幅度倍数，越大扭曲的程度越高，一般为3&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;dPhase&quot;&gt;波形的起始相位，取值区间[0-2*PI)&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;times&quot;&gt;应用几个周期&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="kwrd">private</span> <span class="kwrd">static</span> Bitmap TwistImage(Bitmap srcBmp, <span class="kwrd">bool</span> bXDir, <span class="kwrd">double</span> dMultValue, <span class="kwrd">double</span> dPhase, <span class="kwrd">float</span> times)</pre>
<!--CRLF-->

    <pre class="alteven">{</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">int</span> w = srcBmp.Width;</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">int</span> h = srcBmp.Height;</pre>
<!--CRLF-->

    <pre class="alt">    System.Drawing.Bitmap destBmp = <span class="kwrd">new</span> Bitmap(w, h, PixelFormat.Format24bppRgb);</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">    <span class="rem">// 将位图背景填充为白色</span></pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">using</span> (System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp))</pre>
<!--CRLF-->

    <pre class="alt">    {</pre>
<!--CRLF-->

    <pre class="alteven">        graph.Clear(Color.White);</pre>
<!--CRLF-->

    <pre class="alt">    }</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">double</span> dBaseAxisLen = bXDir ? (<span class="kwrd">double</span>)h : (<span class="kwrd">double</span>)w;</pre>
<!--CRLF-->

    <pre class="alteven">    BitmapData destData = destBmp.LockBits(<span class="kwrd">new</span> Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, destBmp.PixelFormat);</pre>
<!--CRLF-->

    <pre class="alt">    BitmapData srcData = srcBmp.LockBits(<span class="kwrd">new</span> Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, srcBmp.PixelFormat);</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">unsafe</span></pre>
<!--CRLF-->

    <pre class="alt">    {</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">byte</span>* p = (<span class="kwrd">byte</span>*)(<span class="kwrd">void</span>*)srcData.Scan0;</pre>
<!--CRLF-->

    <pre class="alt">        <span class="kwrd">byte</span>* p2 = (<span class="kwrd">byte</span>*)(<span class="kwrd">void</span>*)destData.Scan0;</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; w; i++)</pre>
<!--CRLF-->

    <pre class="alt">        {</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">for</span> (<span class="kwrd">int</span> j = 0; j &lt; h; j++)</pre>
<!--CRLF-->

    <pre class="alt">            {</pre>
<!--CRLF-->

    <pre class="alteven">                <span class="kwrd">double</span> dx = 0;</pre>
<!--CRLF-->

    <pre class="alt">                dx = bXDir ? (PI2 * (<span class="kwrd">double</span>)j * times) / dBaseAxisLen : (PI2 * (<span class="kwrd">double</span>)i * times) / dBaseAxisLen;</pre>
<!--CRLF-->

    <pre class="alteven">                dx += dPhase;</pre>
<!--CRLF-->

    <pre class="alt">                <span class="kwrd">double</span> dy = Math.Sin(dx);</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">                <span class="rem">// 取得当前点的颜色</span></pre>
<!--CRLF-->

    <pre class="alteven">                <span class="kwrd">int</span> nOldX = 0, nOldY = 0;</pre>
<!--CRLF-->

    <pre class="alt">                nOldX = bXDir ? i + (<span class="kwrd">int</span>)(dy * dMultValue) : i;</pre>
<!--CRLF-->

    <pre class="alteven">                nOldY = bXDir ? j : j + (<span class="kwrd">int</span>)(dy * dMultValue);</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">                <span class="kwrd">if</span> (nOldX &gt;= 0 &amp;&amp; nOldX &lt; w</pre>
<!--CRLF-->

    <pre class="alt">                 &amp;&amp; nOldY &gt;= 0 &amp;&amp; nOldY &lt; h)</pre>
<!--CRLF-->

    <pre class="alteven">                {</pre>
<!--CRLF-->

    <pre class="alt">                    p2[(nOldY * destData.Stride) + (nOldX * 3)] = p[(j * srcData.Stride) + (i * 3)];</pre>
<!--CRLF-->

    <pre class="alteven">                    p2[(nOldY * destData.Stride) + (nOldX * 3) + 1] = p[(j * srcData.Stride) + (i * 3) + 1];</pre>
<!--CRLF-->

    <pre class="alt">                    p2[(nOldY * destData.Stride) + (nOldX * 3) + 2] = p[(j * srcData.Stride) + (i * 3) + 2];</pre>
<!--CRLF-->

    <pre class="alteven">                }</pre>
<!--CRLF-->

    <pre class="alt">            }</pre>
<!--CRLF-->

    <pre class="alteven">        }</pre>
<!--CRLF-->

    <pre class="alt">    }</pre>
<!--CRLF-->

    <pre class="alteven">    destBmp.UnlockBits(destData);</pre>
<!--CRLF-->

    <pre class="alt">    srcBmp.UnlockBits(srcData);</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">if</span> (srcBmp != <span class="kwrd">null</span>)</pre>
<!--CRLF-->

    <pre class="alt">        srcBmp.Dispose();</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">return</span> destBmp;</pre>
<!--CRLF-->

    <pre class="alt">}</pre>
<!--CRLF--></div>
</div>

<p>调整了PI值, 测试发现这个值小了效率有所提高. 另外使用unsafe代码绘制波纹, 这个效率基本可以提高10倍之上. 另外扩展了这个函数可以支持多个周期的正弦扭曲. 需要注意的地方是:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt">System.Drawing.Bitmap destBmp = <span class="kwrd">new</span> Bitmap(w, h, PixelFormat.Format24bppRgb);</pre>
<!--CRLF--></div>
</div>

<p></p>

<p></p>

<p>强制使用24位, 原因可以参看以下解释:</p>

<ol>
  <li><a title="http://www.bobpowell.net/lockingbits.htm" href="http://www.bobpowell.net/lockingbits.htm">http://www.bobpowell.net/lockingbits.htm</a> </li>

  <li><a title="http://notebk.gofreeserve.com/?p=206" href="http://notebk.gofreeserve.com/?p=206">http://notebk.gofreeserve.com/?p=206</a>(上文翻译) </li>

  <li><a title="http://msdn.microsoft.com/zh-cn/library/5ey6h79d(VS.80).aspx" href="http://msdn.microsoft.com/zh-cn/library/5ey6h79d(VS.80).aspx">http://msdn.microsoft.com/zh-cn/library/5ey6h79d(VS.80).aspx</a> </li>

  <li><a title="http://msdn.microsoft.com/zh-cn/library/system.drawing.imaging.pixelformat.aspx" href="http://msdn.microsoft.com/zh-cn/library/system.drawing.imaging.pixelformat.aspx">http://msdn.microsoft.com/zh-cn/library/system.drawing.imaging.pixelformat.aspx</a> </li>
</ol>

<p>另外提供一个不用unsafe的实现版本:</p>

<div id="codeSnippetWrapper" class="csharpcode-wrapper">
  <div id="codeSnippet" class="csharpcode">
    <pre class="alt"><span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// 正弦曲线Wave扭曲图片</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;srcBmp&quot;&gt;图片路径&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;bXDir&quot;&gt;沿Y轴扭曲则选择为True&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;dMultValue&quot;&gt;波形的幅度倍数，越大扭曲的程度越高，一般为3&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;dPhase&quot;&gt;波形的起始相位，取值区间[0-2*PI)&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;times&quot;&gt;应用几个周期&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="kwrd">private</span> <span class="kwrd">static</span> Bitmap TwistImage2(Bitmap srcBmp, <span class="kwrd">bool</span> bXDir, <span class="kwrd">double</span> dMultValue, <span class="kwrd">double</span> dPhase, <span class="kwrd">float</span> times)</pre>
<!--CRLF-->

    <pre class="alt">{</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">int</span> w = srcBmp.Width;</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">int</span> h = srcBmp.Height;</pre>
<!--CRLF-->

    <pre class="alteven">    System.Drawing.Bitmap destBmp = <span class="kwrd">new</span> Bitmap(w, h, PixelFormat.Format24bppRgb);</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="rem">// 将位图背景填充为白色</span></pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">using</span> (System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp))</pre>
<!--CRLF-->

    <pre class="alteven">    {</pre>
<!--CRLF-->

    <pre class="alt">        graph.Clear(Color.White);</pre>
<!--CRLF-->

    <pre class="alteven">    }</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">double</span> dBaseAxisLen = bXDir ? (<span class="kwrd">double</span>)h : (<span class="kwrd">double</span>)w;</pre>
<!--CRLF-->

    <pre class="alt">    BitmapData destData = destBmp.LockBits(<span class="kwrd">new</span> Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, destBmp.PixelFormat);</pre>
<!--CRLF-->

    <pre class="alteven">    BitmapData srcData = srcBmp.LockBits(<span class="kwrd">new</span> Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, srcBmp.PixelFormat);</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">byte</span>[] rgbValues = <span class="kwrd">new</span> <span class="kwrd">byte</span>[3];</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; w; i++)</pre>
<!--CRLF-->

    <pre class="alt">    {</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">for</span> (<span class="kwrd">int</span> j = 0; j &lt; h; j++)</pre>
<!--CRLF-->

    <pre class="alt">        {</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">double</span> dx = 0;</pre>
<!--CRLF-->

    <pre class="alt">            dx = bXDir ? (PI2 * (<span class="kwrd">double</span>)j * times) / dBaseAxisLen : (PI2 * (<span class="kwrd">double</span>)i * times) / dBaseAxisLen;</pre>
<!--CRLF-->

    <pre class="alteven">            dx += dPhase;</pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">double</span> dy = Math.Sin(dx);</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">            <span class="rem">// 取得当前点的颜色</span></pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">int</span> nOldX = 0, nOldY = 0;</pre>
<!--CRLF-->

    <pre class="alt">            nOldX = bXDir ? i + (<span class="kwrd">int</span>)(dy * dMultValue) : i;</pre>
<!--CRLF-->

    <pre class="alteven">            nOldY = bXDir ? j : j + (<span class="kwrd">int</span>)(dy * dMultValue);</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">if</span> (nOldX &gt;= 0 &amp;&amp; nOldX &lt; w</pre>
<!--CRLF-->

    <pre class="alt">             &amp;&amp; nOldY &gt;= 0 &amp;&amp; nOldY &lt; h)</pre>
<!--CRLF-->

    <pre class="alteven">            {</pre>
<!--CRLF-->

    <pre class="alt">                Marshal.Copy((IntPtr)((<span class="kwrd">int</span>)srcData.Scan0 + ((j * srcData.Stride) + (i * 3))), rgbValues, 0, 3);</pre>
<!--CRLF-->

    <pre class="alteven">                Marshal.Copy(rgbValues, 0, (IntPtr)((<span class="kwrd">int</span>)destData.Scan0 + ((nOldY * destData.Stride) + (nOldX * 3))), 3);</pre>
<!--CRLF-->

    <pre class="alt">            }</pre>
<!--CRLF-->

    <pre class="alteven">        }</pre>
<!--CRLF-->

    <pre class="alt">    }</pre>
<!--CRLF-->

    <pre class="alteven">    destBmp.UnlockBits(destData);</pre>
<!--CRLF-->

    <pre class="alt">    srcBmp.UnlockBits(srcData);</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">if</span> (srcBmp != <span class="kwrd">null</span>)</pre>
<!--CRLF-->

    <pre class="alt">        srcBmp.Dispose();</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">return</span> destBmp;</pre>
<!--CRLF-->

    <pre class="alt">}</pre>
<!--CRLF-->

    <pre class="alteven"><span class="preproc">#endregion</span></pre>
<!--CRLF--></div>
</div>

<p>这下&quot;<span class="preproc">#region</span> &quot;关闭了就.</p>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=483</link>
			<title><![CDATA[KCAPTCHA 验证码波纹扭曲函数 C# 实现]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Sat,30 Jan 2010 17:56:57 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=483</guid>
		<description><![CDATA[<p><a href="http://www.captcha.ru/en/kcaptcha/" target="_blank">KCAPTCHA</a>&#160; 一个不错的验证码实现, 官方网站:<a title="http://www.captcha.ru/en/kcaptcha/" href="http://www.captcha.ru/en/kcaptcha/">http://www.captcha.ru/en/kcaptcha/</a>. 因为工作原因, 需要改进验证码所有有机会把波纹扭曲部分使用 C# 实现. </p>  <div id="codeSnippetWrapper" class="csharpcode-wrapper">   <div id="codeSnippet" class="csharpcode">     <pre class="alt"><span class="preproc">#region  KCAPTCHA 波纹扭曲</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// # <a href="http://www.captcha.ru/en/kcaptcha/" target="_blank">KCAPTCHA</a> PROJECT VERSION 1.2.6</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// www.captcha.ru, www.kruglov.ru</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// 波形扭曲 FROM KCAPTCHA</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;srcBmp&quot;&gt;待扭曲的图像 必须为 PixelFormat.Format24bppRgb 格式图像&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="kwrd">private</span> <span class="kwrd">static</span> Bitmap WaveDistortion(Bitmap srcBmp)</pre>
<!--CRLF-->

    <pre class="alteven">{</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">if</span> (srcBmp == <span class="kwrd">null</span>)</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">return</span> <span class="kwrd">null</span>;</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">if</span> (srcBmp.PixelFormat != PixelFormat.Format24bppRgb)</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(<span class="str">&quot;srcBmp PixelFormat.Format24bppRgb 格式图像&quot;</span>, <span class="str">&quot;srcBmp&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">    var width = srcBmp.Width;</pre>
<!--CRLF-->

    <pre class="alt">    var height = srcBmp.Height;</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">    Bitmap destBmp = <span class="kwrd">new</span> Bitmap(width, height, PixelFormat.Format24bppRgb);</pre>
<!--CRLF-->

    <pre class="alteven">    {</pre>
<!--CRLF-->

    <pre class="alt">        <span class="rem">//前景色</span></pre>
<!--CRLF-->

    <pre class="alteven">        Color foreground_color = Color.FromArgb(randx.Next(10, 100), randx.Next(10, 100), randx.Next(10, 100));</pre>
<!--CRLF-->

    <pre class="alt">        <span class="rem">//背景色</span></pre>
<!--CRLF-->

    <pre class="alteven">        Color background_color = Color.FromArgb(randx.Next(200, 250), randx.Next(200, 250), randx.Next(200, 250));</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">using</span> (Graphics newG = Graphics.FromImage(destBmp))</pre>
<!--CRLF-->

    <pre class="alt">        {</pre>
<!--CRLF-->

    <pre class="alteven">            newG.Clear(background_color);</pre>
<!--CRLF-->

    <pre class="alt">            <span class="rem">// periods 时间</span></pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">double</span> rand1 = randx.Next(710000, 1200000) / 10000000.0;</pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">double</span> rand2 = randx.Next(710000, 1200000) / 10000000.0;</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">double</span> rand3 = randx.Next(710000, 1200000) / 10000000.0;</pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">double</span> rand4 = randx.Next(710000, 1200000) / 10000000.0;</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="rem">// phases  相位</span></pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">double</span> rand5 = randx.Next(0, 31415926) / 10000000.0;</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">double</span> rand6 = randx.Next(0, 31415926) / 10000000.0;</pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">double</span> rand7 = randx.Next(0, 31415926) / 10000000.0;</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">double</span> rand8 = randx.Next(0, 31415926) / 10000000.0;</pre>
<!--CRLF-->

    <pre class="alt">            <span class="rem">// amplitudes 振幅</span></pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">double</span> rand9 = randx.Next(330, 420) / 110.0;</pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">double</span> rand10 = randx.Next(330, 450) / 110.0;</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">double</span> amplitudesFactor = randx.Next(5, 6) / 10.0;<span class="rem">//振幅小点防止出界</span></pre>
<!--CRLF-->

    <pre class="alt">            <span class="kwrd">double</span> center = width / 2.0;</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">            <span class="rem">//wave distortion 波纹扭曲</span></pre>
<!--CRLF-->

    <pre class="alteven">            BitmapData destData = destBmp.LockBits(<span class="kwrd">new</span> Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, destBmp.PixelFormat);</pre>
<!--CRLF-->

    <pre class="alt">            BitmapData srcData = srcBmp.LockBits(<span class="kwrd">new</span> Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, srcBmp.PixelFormat);</pre>
<!--CRLF-->

    <pre class="alteven">            <span class="kwrd">for</span> (var x = 0; x &lt; width; x++)</pre>
<!--CRLF-->

    <pre class="alt">            {</pre>
<!--CRLF-->

    <pre class="alteven">                <span class="kwrd">for</span> (var y = 0; y &lt; height; y++)</pre>
<!--CRLF-->

    <pre class="alt">                {</pre>
<!--CRLF-->

    <pre class="alteven">                    var sx = x + (Math.Sin(x * rand1 + rand5)</pre>
<!--CRLF-->

    <pre class="alt">                                + Math.Sin(y * rand3 + rand6)) * rand9 - width / 2 + center + 1;</pre>
<!--CRLF-->

    <pre class="alteven">                    var sy = y + (Math.Sin(x * rand2 + rand7)</pre>
<!--CRLF-->

    <pre class="alt">                                + Math.Sin(y * rand4 + rand8)) * rand10 * amplitudesFactor; <span class="rem">//振幅小点防止出界</span></pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">                    <span class="kwrd">int</span> color, color_x, color_y, color_xy;</pre>
<!--CRLF-->

    <pre class="alteven">                    Color overColor = Color.Empty;</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">                    <span class="kwrd">if</span> (sx &lt; 0 || sy &lt; 0 || sx &gt;= width - 1 || sy &gt;= height - 1)</pre>
<!--CRLF-->

    <pre class="alt">                    {</pre>
<!--CRLF-->

    <pre class="alteven">                        <span class="kwrd">continue</span>;</pre>
<!--CRLF-->

    <pre class="alt">                    }</pre>
<!--CRLF-->

    <pre class="alteven">                    <span class="kwrd">else</span></pre>
<!--CRLF-->

    <pre class="alt">                    {</pre>
<!--CRLF-->

    <pre class="alteven">                        color = BitmapDataColorAt(srcData, (<span class="kwrd">int</span>)sx, (<span class="kwrd">int</span>)sy).B;</pre>
<!--CRLF-->

    <pre class="alt">                        color_x = BitmapDataColorAt(srcData, (<span class="kwrd">int</span>)(sx + 1), (<span class="kwrd">int</span>)sy).B;</pre>
<!--CRLF-->

    <pre class="alteven">                        color_y = BitmapDataColorAt(srcData, (<span class="kwrd">int</span>)sx, (<span class="kwrd">int</span>)(sy + 1)).B;</pre>
<!--CRLF-->

    <pre class="alt">                        color_xy = BitmapDataColorAt(srcData, (<span class="kwrd">int</span>)(sx + 1), (<span class="kwrd">int</span>)(sy + 1)).B;</pre>
<!--CRLF-->

    <pre class="alteven">                    }</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">                    <span class="kwrd">if</span> (color == 255 &amp;&amp; color_x == 255 &amp;&amp; color_y == 255 &amp;&amp; color_xy == 255)</pre>
<!--CRLF-->

    <pre class="alt">                    {</pre>
<!--CRLF-->

    <pre class="alteven">                        <span class="kwrd">continue</span>;</pre>
<!--CRLF-->

    <pre class="alt">                    }</pre>
<!--CRLF-->

    <pre class="alteven">                    <span class="kwrd">else</span> <span class="kwrd">if</span> (color == 0 &amp;&amp; color_x == 0 &amp;&amp; color_y == 0 &amp;&amp; color_xy == 0)</pre>
<!--CRLF-->

    <pre class="alt">                    {</pre>
<!--CRLF-->

    <pre class="alteven">                        overColor = Color.FromArgb(foreground_color.R, foreground_color.G, foreground_color.B);</pre>
<!--CRLF-->

    <pre class="alt">                    }</pre>
<!--CRLF-->

    <pre class="alteven">                    <span class="kwrd">else</span></pre>
<!--CRLF-->

    <pre class="alt">                    {</pre>
<!--CRLF-->

    <pre class="alteven">                        <span class="kwrd">double</span> frsx = sx - Math.Floor(sx);</pre>
<!--CRLF-->

    <pre class="alt">                        <span class="kwrd">double</span> frsy = sy - Math.Floor(sy);</pre>
<!--CRLF-->

    <pre class="alteven">                        <span class="kwrd">double</span> frsx1 = 1 - frsx;</pre>
<!--CRLF-->

    <pre class="alt">                        <span class="kwrd">double</span> frsy1 = 1 - frsy;</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">                        <span class="kwrd">double</span> newColor =</pre>
<!--CRLF-->

    <pre class="alteven">                             color * frsx1 * frsy1 +</pre>
<!--CRLF-->

    <pre class="alt">                             color_x * frsx * frsy1 +</pre>
<!--CRLF-->

    <pre class="alteven">                             color_y * frsx1 * frsy +</pre>
<!--CRLF-->

    <pre class="alt">                             color_xy * frsx * frsy;</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">                        <span class="kwrd">if</span> (newColor &gt; 255) newColor = 255;</pre>
<!--CRLF-->

    <pre class="alteven">                        newColor = newColor / 255;</pre>
<!--CRLF-->

    <pre class="alt">                        <span class="kwrd">double</span> newcolor0 = 1 - newColor;</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">                        <span class="kwrd">int</span> newred = Math.Min((<span class="kwrd">int</span>)(newcolor0 * foreground_color.R + newColor * background_color.R), 255);</pre>
<!--CRLF-->

    <pre class="alteven">                        <span class="kwrd">int</span> newgreen = Math.Min((<span class="kwrd">int</span>)(newcolor0 * foreground_color.G + newColor * background_color.G), 255);</pre>
<!--CRLF-->

    <pre class="alt">                        <span class="kwrd">int</span> newblue = Math.Min((<span class="kwrd">int</span>)(newcolor0 * foreground_color.B + newColor * background_color.B), 255);</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">                        overColor = Color.FromArgb(newred, newgreen, newblue);</pre>
<!--CRLF-->

    <pre class="alteven">                    }</pre>
<!--CRLF-->

    <pre class="alt">                    BitmapDataColorSet(destData, x, y, overColor);</pre>
<!--CRLF-->

    <pre class="alteven">                }</pre>
<!--CRLF-->

    <pre class="alt">            }</pre>
<!--CRLF-->

    <pre class="alteven">            destBmp.UnlockBits(destData);</pre>
<!--CRLF-->

    <pre class="alt">            srcBmp.UnlockBits(srcData);</pre>
<!--CRLF-->

    <pre class="alteven">        }</pre>
<!--CRLF-->

    <pre class="alt">        <span class="kwrd">if</span> (srcBmp != <span class="kwrd">null</span>)</pre>
<!--CRLF-->

    <pre class="alteven">            srcBmp.Dispose();</pre>
<!--CRLF-->

    <pre class="alt">    }</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">return</span> destBmp;</pre>
<!--CRLF-->

    <pre class="alt">}</pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// 获得 BitmapData 指定坐标的颜色信息</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// 实现 PHP imagecolorat</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;srcData&quot;&gt;从图像数据获得颜色 必须为 PixelFormat.Format24bppRgb 格式图像数据&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;x&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;y&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;returns&gt;x,y 坐标的颜色数据&lt;/returns&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;remarks&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// Format24BppRgb 已知X，Y坐标，像素第一个元素的位置为Scan0+(Y*Stride)+(X*3)。</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// 这是blue字节的位置，接下来的2个字节分别含有green、red数据。</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;/remarks&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="kwrd">static</span> Color BitmapDataColorAt(BitmapData srcData, <span class="kwrd">int</span> x, <span class="kwrd">int</span> y)</pre>
<!--CRLF-->

    <pre class="alt">{</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">if</span> (srcData.PixelFormat != PixelFormat.Format24bppRgb)</pre>
<!--CRLF-->

    <pre class="alt">        <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(<span class="str">&quot;srcData PixelFormat.Format24bppRgb 格式图像数据&quot;</span>, <span class="str">&quot;srcData&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alteven">&#160;</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">byte</span>[] rgbValues = <span class="kwrd">new</span> <span class="kwrd">byte</span>[3];</pre>
<!--CRLF-->

    <pre class="alteven">    Marshal.Copy((IntPtr)((<span class="kwrd">int</span>)srcData.Scan0 + ((y * srcData.Stride) + (x * 3))), rgbValues, 0, 3);</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">return</span> Color.FromArgb(rgbValues[2], rgbValues[1], rgbValues[0]);</pre>
<!--CRLF-->

    <pre class="alteven">}</pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// 设置 BitmapData 指定坐标的颜色信息</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// 实现 PHP ImageColorSet</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;/summary&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;destData&quot;&gt;设置图像数据的颜色 必须为 PixelFormat.Format24bppRgb 格式图像数据&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;x&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;param name=&quot;y&quot;&gt;&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;param name=&quot;color&quot;&gt;待设置颜色&lt;/param&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// &lt;remarks&gt;</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// Format24BppRgb 已知X，Y坐标，像素第一个元素的位置为Scan0+(Y*Stride)+(X*3)。</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="rem">/// 这是blue字节的位置，接下来的2个字节分别含有green、red数据。</span></pre>
<!--CRLF-->

    <pre class="alteven"><span class="rem">/// &lt;/remarks&gt;</span></pre>
<!--CRLF-->

    <pre class="alt"><span class="kwrd">static</span> <span class="kwrd">void</span> BitmapDataColorSet(BitmapData destData, <span class="kwrd">int</span> x, <span class="kwrd">int</span> y, Color color)</pre>
<!--CRLF-->

    <pre class="alteven">{</pre>
<!--CRLF-->

    <pre class="alt">    <span class="kwrd">if</span> (destData.PixelFormat != PixelFormat.Format24bppRgb)</pre>
<!--CRLF-->

    <pre class="alteven">        <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(<span class="str">&quot;destData PixelFormat.Format24bppRgb 格式图像数据&quot;</span>, <span class="str">&quot;destData&quot;</span>);</pre>
<!--CRLF-->

    <pre class="alt">&#160;</pre>
<!--CRLF-->

    <pre class="alteven">    <span class="kwrd">byte</span>[] rgbValues = <span class="kwrd">new</span> <span class="kwrd">byte</span>[3] { color.B, color.G, color.R };</pre>
<!--CRLF-->

    <pre class="alt">    Marshal.Copy(rgbValues, 0, (IntPtr)((<span class="kwrd">int</span>)destData.Scan0 + ((y * destData.Stride) + (x * 3))), 3);</pre>
<!--CRLF-->

    <pre class="alteven">}</pre>
<!--CRLF-->

    <pre class="alt"><span class="preproc">#endregion</span></pre>
<!--CRLF--></div>
</div>

<p>另外一些验证码游优化有关的链接:</p>

<ol>
  <li><a title="http://www.captcha.ru/en/kcaptcha/" href="http://www.captcha.ru/en/kcaptcha/">http://www.captcha.ru/en/kcaptcha/</a>&#160;&#160; (KCAPTCHA 官方) </li>

  <li><a title="http://zh.wikipedia.org/wiki/CAPTCHA" href="http://zh.wikipedia.org/wiki/CAPTCHA">http://zh.wikipedia.org/wiki/CAPTCHA</a> </li>

  <li><a title="http://en.wikipedia.org/wiki/CAPTCHA" href="http://en.wikipedia.org/wiki/CAPTCHA">http://en.wikipedia.org/wiki/CAPTCHA</a> </li>

  <li><a title="http://caca.zoy.org/wiki/PWNtcha" href="http://caca.zoy.org/wiki/PWNtcha">http://caca.zoy.org/wiki/PWNtcha</a> </li>

  <li><a title="http://www.brains-n-brawn.com/default.aspx?vDir=aicaptcha" href="http://www.brains-n-brawn.com/default.aspx?vDir=aicaptcha">http://www.brains-n-brawn.com/default.aspx?vDir=aicaptcha</a> </li>

  <li><a title="http://www.captcha.net/" href="http://www.captcha.net/">http://www.captcha.net/</a> </li>
</ol>

<p>还有一些验证码识别的链接:</p>

<ol>
  <li><a title="http://www.cnblogs.com/xiaotie/archive/2009/01/15/1376677.html" href="http://www.cnblogs.com/xiaotie/archive/2009/01/15/1376677.html">http://www.cnblogs.com/xiaotie/archive/2009/01/15/1376677.html</a> </li>

  <li><a title="http://www.codeproject.com/KB/recipes/Shape_context_matching.aspx" href="http://www.codeproject.com/KB/recipes/Shape_context_matching.aspx">http://www.codeproject.com/KB/recipes/Shape_context_matching.aspx</a> </li>

  <li><a title="http://en.wikipedia.org/wiki/Shape_context" href="http://en.wikipedia.org/wiki/Shape_context">http://en.wikipedia.org/wiki/Shape_context</a> </li>

  <li><a title="http://www.eecs.berkeley.edu/Research/Projects/CS/vision/shape/sc_digits.html" href="http://www.eecs.berkeley.edu/Research/Projects/CS/vision/shape/sc_digits.html">http://www.eecs.berkeley.edu/Research/Projects/CS/vision/shape/sc_digits.html</a> </li>
</ol>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=475</link>
			<title><![CDATA[软件开发经典书籍的魅力]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Mon,06 Apr 2009 10:07:26 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=475</guid>
		<description><![CDATA[<p>读软件开发方面书籍自己似乎有偏好，不喜欢看国内作者写的（不过有好书也是看的，比如<a href="http://www.broadview.com.cn" target="_blank">博文视点</a>的好多原创经典书籍）大部分看的是国外书籍的译本（英语不好只能看译本），译本也是有选择的，如果是一堆人翻译的基本也不看，除非这本书讲的方面确实就这么一本书，并且翻译的也不赖。<a href="http://www.cnblogs.com" target="_blank">博客园</a>有一.NET方面的牛人名叫“<a href="http://zhq.ahau.edu.cn/blog/www.cnblogs.com/JeffreyZhao/" target="_blank">老赵</a>”，他的博客格言是“让老外看中国人写的计算机书籍”，甚是敬仰。</p>  <p>过年前后接手的项目是一个“客户端数据录入”的软件，之前开发的都是B/S软件，对WinForm开发中的某些要求也不甚了解，所以按着WEB软件的模式来建立“数据连接层”、“业务逻辑层”。其实自己使用了一个“<a href="http://www.socansoft.com/" target="_blank">SocanCode</a>”的代码生成器来生成了“数据连接层”和“业务逻辑层”，由于客户要求的这个客户端软件除了要有一个“中心版”还要一个“部门版”的（软件为什么没有直接设计成B/S版的呢？因为客户的理由是“部门版”的用户可能没有网络，并且点名要搞两个版本），考虑到性能方面的要求所以“部门版”数据库用Access，“中心版”使用SQL Server 2000 个人版，所以这个“代码生成器”生成了两套“数据连接层”代码，真是不错的东西。</p>  <p>“<a href="http://www.socansoft.com/" target="_blank">SocanCode</a>”生成的代码类似于“PetShop 4”，模式的代码，“数据实体模型”部分直接就是实体类，软件在实现的时候碰到一个问题：数据库的某些字段的值与其他字段有隐藏的“关联”关系，比如一个XX号=某些字典字段+某些字典字段+自编号。数据库在设计的时候实际上已经违背了某些“数据库设计范式”，因为为了最后呈现的时候方便，也为了客户在导出数据的时候方便。为了实现这个“关联”最后每个“数据实体模型”实现了“<a href="http://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged(VS.80).aspx" target="_blank">System.ComponentModel.INotifyPropertyChanged</a>”接口，来“通知”某些列的更改，通过“PropertyChanged”事件来捕捉“通知”，再“Remove”事件，更改某些“关联”关系的字段值，再“Add”事件来完成整一个“关联”关系字段的控制。虽然实现起来麻烦了点，但客户再提“关联”关系字段更改的需求改的就很方便了。</p>  <p>软件交付之后，经过几次的BUG修改，客户数据录入接近尾声之后，客户要求“中心版”软件“数据横表（起始就是一个GridView）”部分要有可编辑功能，因为“部门”录入的数据太乱了，需要快速编辑修改功能。问题出来了“得到数据横表更改，保存数据横表更改的数据”怎么解决？记得学习.NET的时候使用DataSet+DataAdapter来完成数据绑定到表格，批量更改后“DataTable.GetChanges()”更改，保存数据，再“DataTable.AcceptChanges()”更改，就可以了。事情到了这个步骤更改成DataSet来实现肯定软件动的地方不少。最后解决的办法是“数据横表”绑定的“数据实体列表”转换成“DataTable”，修改完成后再转换成“List&lt;&gt;”类型的数据列表，再保存数据。在更改这个“横表”编辑功能的时候又发现一个问题“用户录入表单编辑的数据撤销操作后无法回复原来数据”，记得一本好像是“<a href="http://www.douban.com/subject/2127497/" target="_blank">Expert C# 2005 Business Objects</a>”书籍上讲到“多层撤销”的问题，解决的方法是“数据实体模型”实现了“<a href="http://msdn.microsoft.com/zh-cn/library/system.componentmodel.ieditableobject(VS.80).aspx" target="_blank">System.ComponentModel.IEditableObject</a>”结构，使得实体模型对象具有了“BeginEdit、CancelEdit、EndEdit”功能，为了期间的一个数据拷贝功能“数据实体模型”又实现了“<a href="http://msdn.microsoft.com/zh-cn/library/system.icloneable.aspx" target="_blank">System.ICloneable</a>”接口。最后总算所有的问题都解决来了，好像很累人，还好在处理数据“关联”问题时没有去实现“<a href="http://msdn.microsoft.com/zh-cn/library/system.componentmodel.ibindinglist(VS.80).aspx" target="_blank">System.ComponentModel.IBindingList</a>”接口。</p>  <p>似乎自己在开发WinForm软件的时候犯了很大错误——把WinForm当成了WEB程序来开发，结构一开始就错了。如果一开始使用“强类型数据集”也没有多少错误（没有用的原因是自己在原先的WEB开发中因为使用“强类型数据集”，在后期给自己维护造成了很大的麻烦），最后为了实现某些功能自己实现了一个超级简化版的“DataTable”，自己在为了实现“数据实体模型”可以“GetChanges()”的时候差一点把“数据实体模型”进行了一个大规模的代码重构（因为在CodeProject上一篇文章讲到了可以使得得到“实体模型类表”具有“GetChanges()”功能的方法）。</p>  <p>清明节到图书城看到了《<a href="http://www.douban.com/subject/2054931/" target="_blank">Effective C#</a>》这本书，其实自己从前看过这本书，只是没有认真的看，其中的的第41条：《<a href="http://www.cnblogs.com/WuCountry/archive/2007/03/31/695300.html" target="_blank">选择DataSet而不是自定义的数据结构</a>（Prefer DataSets to Custom Structures）》，看了之后真是羞愧万千，怪自己没有认真的领悟书中的东西，下面是内容摘要：</p>  <blockquote>   <p>因为两个原则，把DataSet的名声搞的不好。首先就是使用XML序列化的DataSet与其它的非.Net代码进行交互时不方便。如果在Web服务的API中使用DataSet时，在与其它没有使用.Net框架的系统进行交互时会相当困难。其次，它是一个很一般的容器。你可以通过欺骗.Net框架里的一些安全类型来错误DataSet。但在现代软件系统中，DataSet还可以解决很多常规的问题。如果你明白它的优势，避免它的缺点，你就可以扩展这个类型了。 </p>    <p>DataSet类设计出来是为了离线使用一些存储在相关数据库里的数据。你已经知道它是用来存储DataTable的，而DataTable就是一个与数据库里的结构在行和列上进行匹配的内存表。或许你已经看到过一些关于DataSet支持在内部的表中建立关系的例子。甚至还有可能，你已经见过在DataSet里验证它所包含的数据，进行数据约束的例子。 </p>    <p>但不仅仅是这些，DataSet还支持AcceptChanges 和RejectChanges 方法来进行事务处理，而且它们可以做为DiffGrams存储，也就是包含曾经修改过的数据。多个DataSet还可以通过合并成为一个常规的存储库。DataSet还支持视图，这就是说你可以通过标准的查询来检测数据里的部份内容。而且视图是可以建立在多个表上的。 </p>    <p>然而，有些人想开发自己的存储结构，而不用DataSet。因DataSet是一个太一般的容器，这会在性能上有所损失。一个DataSet并不是一个强类型的存储容器，其实存储在里面的对象是一个字典。而且在里的表中的列也是字典。存储在里的元素都是以System.Object的引用形式存在。这使得我们要这样写代码： </p>    <p>…省略中间部分…      <br /></p>    <p>花了几页的代码来支持一些已经在DataSet里实现的了的功能。实际上，这还不能像DataSet那样恰当的工作。例如，交互式的添加一个新记录到集合中，以及支持事务所要求的BeginEdit, CancelEdit, 和EndEdit等。 你要在CancelEdit 调用时检测一个新的对象而不是一个已经修改了的对象。CancelEdit 必须从集合上移除这个新的对象，该对象应该是上次调用BeginEdit时创建的。对于AddressRecord 来说，还有很多修改要完成，而且一对事件还要添加到AddressList 类上。 </p>    <p>最后，就是这个IBindingList接口。这个接口至少包含了20个方法和属性，用于控件查询列表上的功能描述。你必须为只读列表实现IBindingList 或者交互排序，或者支持搜索。在你取得内容之前就陷于层次关系和导航关系中了。我也不准备为上面所有的代码添加任何例子了。      <br />几页过后，再问问你自己，还准备创建你自己的特殊集合吗？或者你想使用一个DataSet吗？除非你的集合是一个基于某些算法，对性能要求严格的集合，或者必须有轻便的格式，就要使用自己的DataSet，特别是类型化的DataSet。这将花去你大量的时间，是的，你可以争辩说DataSet并不是一个基于面向对象设计的最好的例子。类型化的DataSet甚至会破坏更多的规则。但，使用它所产生的代码开发效率，比起自己手写更优美的代码所花的时间，</p>    <p>这只是其中一小部分。</p> </blockquote>  <p>经典的书籍往往就是经典，C++方面经典的书籍一版一版的发布，一次一次的印刷，看得人津津乐道，JAVA方面的书籍也是这样。C#方面因为也就八九年的时间，经典的书籍似乎少了点，大概是翻译成中文的不多（国人写的似乎流行不起来），但一本《Effective C#》虽然讲解的是“C# 1.0”方面的内容，但毕竟是.NET方面的，内容很经典。这些天闲了在看一本《<a href="http://www.douban.com/subject/1230559/" target="_blank">企业应用架构模式</a>》的书籍，书籍至少是03年之前的写的，到现在.NET软件开发设计方面的内容也有很多进步，但这本书讲的确实很不错，用现在的眼光来看作者讲的确实很有前瞻性，应该是自己并不了解企业软件开发方面的内容，这么多年自己只能从七七八八的书籍上来自己体会企业软件开发，看到这本书真是眼前一亮。</p>  <p>&#160;</p>  <blockquote>   <p>万物变化的季节</p>    <p>TUPUNCO</p>    <p>2009.04.05</p>    <p>好运每一天</p></blockquote>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=474</link>
			<title><![CDATA[ASP.NET MVC CRUD 实例]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[原创]]></category>
			<pubDate>Tue,24 Mar 2009 10:01:20 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=474</guid>
		<description><![CDATA[<p><a href="http://www.asp.net/mvc" rel="tag" target="_blank">ASP.NET MVC</a> 1.0 已经于上周发布了, RC1的时候写了两个例子, 一个是一个CRUD的例子, 一个是仿<a href="http://www.blueidea.com" rel="tag" target="_blank">蓝色梦想</a><a href="http://Case.Blueidea.Com" rel="tag" target="_blank">作品集</a>URL路径的例子. CRUD例子是一个拥有完整的从&quot;数据源&quot;查询数据列表/单条详细/删除/更新添加的功能的列子, 仿蓝色梦想作品集URL路径的例子是模仿作品集的URL切换方式, 展示了ASP.NET MVC 功能强大的路由功能, 自定义控件的列子. 本文事CRUD例子的说明和技术细节.</p>  <p>ASP.NET MVC安装/创建项目的方法可以参看<a href="http://www.asp.net/mvc">http://www.asp.net/mvc</a>的说明教程. </p>  <p>创建项目完毕后, 创建一个数据模型(实际为一数据实体)和数据服务(Fake的数据仓库)部分代码:</p>  <div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">   <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">     <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">namespace</span> MvcApplicationCRUD.Models</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> People</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Age { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p>&#160;</p>

<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">using</span> System.Collections.Generic;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">namespace</span> MvcApplicationCRUD.Models</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> PeopleDataService</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> List&lt;People&gt; PeopleList = <span style="color: #0000ff">null</span>;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">static</span> PeopleDataService()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            PeopleList = <span style="color: #0000ff">new</span> List&lt;People&gt;();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; 20; i++)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                PeopleList.Add(<span style="color: #0000ff">new</span> People() { Id = i + 1, Name = <span style="color: #006080">&quot;name&quot;</span> + i * i, Age = i + 10 });</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p>实现Controller内各个Action:</p>

<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 285px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">using</span> System;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">using</span> System.Linq;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">using</span> System.Web.Mvc;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">namespace</span> MvcApplicationCRUD.Controllers</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">using</span> MvcPaging;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">using</span> Models;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> PeopleController : Controller</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> defaultPageSize = 10;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">//</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// GET: /People/</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// 列表分页显示</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;param name=&quot;page&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> ActionResult Index(<span style="color: #0000ff">int</span>? page)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var peopleList = PeopleDataService.PeopleList;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">int</span> currentPageIndex = page.HasValue ? page.Value - 1 : 0;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> View(peopleList.ToPagedList(currentPageIndex, defaultPageSize));</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">//</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// GET: /People/Details/5</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// 单条详细显示</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;param name=&quot;id&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> ActionResult Details(<span style="color: #0000ff">int</span> id)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var people = PeopleDataService.PeopleList.First(p =&gt; p.Id == id);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> View(people);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">//</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// GET: /People/Create</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// 创建</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> ActionResult Create()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var people = <span style="color: #0000ff">new</span> People();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> View(people);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">//</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// POST: /People/Create</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// 保存创建内容</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;param name=&quot;people&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        [AcceptVerbs(HttpVerbs.Post)]</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> ActionResult Create([Bind(Include = <span style="color: #006080">&quot;Age,Name&quot;</span>)] People people)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">try</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #008000">//得到当前数据服务内ID最大值并生成当前记录的ID</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                people.Id = PeopleDataService.PeopleList.Max(p =&gt; p.Id) + 1;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #008000">//Valid</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                GetValidation(people);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">if</span> (!ModelState.IsValid)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> Exception();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">else</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    PeopleDataService.PeopleList.Add(people);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    <span style="color: #0000ff">return</span> RedirectToAction(<span style="color: #006080">&quot;Index&quot;</span>);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">catch</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> View(people);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">//</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// GET: /People/Edit/5</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// 编辑某条记录</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;param name=&quot;id&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> ActionResult Edit(<span style="color: #0000ff">int</span> id)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var people = PeopleDataService.PeopleList.First(p =&gt; p.Id == id);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> View(people);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">//</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// POST: /People/Edit/5</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// 保存内容</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;param name=&quot;id&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;param name=&quot;people&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        [AcceptVerbs(HttpVerbs.Post)]</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> ActionResult Edit(<span style="color: #0000ff">int</span> id, [Bind(Include = <span style="color: #006080">&quot;Age,Name&quot;</span>)] People people)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var oPeople = PeopleDataService.PeopleList.First(p =&gt; p.Id == id);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">try</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">if</span> (oPeople != <span style="color: #0000ff">null</span>)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    oPeople.Age = people.Age;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    oPeople.Name = people.Name;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #008000">//Valid</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                GetValidation(oPeople);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">if</span> (!ModelState.IsValid)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> Exception();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">else</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    <span style="color: #0000ff">return</span> RedirectToAction(<span style="color: #006080">&quot;Index&quot;</span>);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">catch</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> View(oPeople);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// 数据验证之用</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">/// &lt;param name=&quot;oPeople&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> GetValidation(People oPeople)</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #008000">//if (oPeople.Age &lt; 10)</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #008000">//    ModelState.AddModelError(&quot;Age&quot;, &quot;Age 必须大于等于10...&quot;);</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">string</span>.IsNullOrEmpty(oPeople.Name))</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                ModelState.AddModelError(<span style="color: #006080">&quot;Name&quot;</span>, <span style="color: #006080">&quot;Name 不能为空...&quot;</span>);</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p>控制器代码中使用了一个叫&quot;MvcPaging&quot;的分页类库, 这个可能是现在碰到的最优美的ASP.NET MVC上分页类库. ASP.NET MVC 中错误处理使用ModelState.AddModelError方法来设置, 调用ModelState.IsValid来验证验证通过与否. 通过设置某一个Action的AcceptVerbs属性来使得其可以接受特定Verbs的外部数据, 这个功能比CTP版中的实现不知道好了多少倍(CTP版中搞得莫名其妙), 这个功能也是实现REST风格URL的完备解决. 通过设置Action参数为一自定义的类来接受前台视图内的复杂数据, 并设置[Bind]属性来设置那些数据可以接受. </p>

<p>控制器代码写好后, VS IDE内使用右键&quot;Add View&quot;方式来快速生成视图部分, 期间注意设置强类型视图就可以了. Index内添加分页部分功能:</p>

<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">div</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">=&quot;pager&quot;</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="background-color: #ffff00">&lt;%</span><pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060">   1:</span> = Html.Pager(ViewData.Model.PageSize, ViewData.Model.PageNumber, ViewData.Model.TotalItemCount) </pre><span style="background-color: #ffff00">%&gt;</span></pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">div</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>&#160;</p>

<p>代码下载:<iframe style="border-bottom: #dde5e9 1px solid; border-left: #dde5e9 1px solid; padding-bottom: 0px; background-color: #ffffff; margin: 3px; padding-left: 0px; width: 240px; padding-right: 0px; height: 66px; border-top: #dde5e9 1px solid; border-right: #dde5e9 1px solid; padding-top: 0px" marginheight="0" src="http://cid-e63ac91b5bfc8e30.skydrive.live.com/embedrowdetail.aspx/Public/MvcApplicationCRUD.zip" frameborder="0" marginwidth="0" scrolling="no"></iframe></p>

<p>&#160;</p>

<blockquote>
  <p>春暖花开的季节.</p>

  <p>TUPUNCO</p>

  <p>2009.03.23</p>

  <p>好运每一天.</p></blockquote>]]></description>
		</item>
		
			<item>
			<link>http://zhq.ahau.edu.cn/blog/article.asp?id=473</link>
			<title><![CDATA[[原创]C#中使用反射]]></title>
			<author>tupunco@163.com(tupunco)</author>
			<category><![CDATA[随笔]]></category>
			<pubDate>Mon,09 Mar 2009 09:02:28 +0800</pubDate>
			<guid>http://zhq.ahau.edu.cn/blog/default.asp?id=473</guid>
		<description><![CDATA[<p>过了年到现在忙了起来, 不像年前, 可以整月的闲着, 不过闲着也有好处, 学习了好多东西. 过了年的项目中多次用到了 .Net 反射机制, 使得其间巧妙地解决了很多问题. 总结一下使用到的地方.</p>  <h4>1.使用工厂模式来抽象数据连接层之用(PetShop模式的三层结构内的使用方式).</h4>  <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px">   <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">     <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> System.Reflection.Assembly assembly = Assembly.LoadFile(<span style="color: #006080">&quot;Assembly Path&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> IXXXXXDAL dal = (IXXXXXDAL)assembly.CreateInstance(<span style="color: #006080">&quot;Namespace.DAL.XXXXXDAL&quot;</span>);</pre>
  </div>
</div>

<p></p>

<h4>2.使用 &quot;System.Activator.CreateInstance()&quot; 方式来创建对象.</h4>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #008000">//</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> <span style="color: #008000">// 摘要:</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span> <span style="color: #008000">//     创建类型的一个实例，该类型由指定的泛型类型参数指定。</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span> <span style="color: #008000">//</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span> <span style="color: #008000">// 类型参数:</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span> <span style="color: #008000">//   T:</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span> <span style="color: #008000">//     要创建的类型。</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span> <span style="color: #008000">//</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span> <span style="color: #008000">// 返回结果:</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span> <span style="color: #008000">//     对新创建对象的引用。</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> T CreateInstance&lt;T&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span> <span style="color: #008000">//</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  13:</span> <span style="color: #008000">// 摘要:</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span> <span style="color: #008000">//     使用指定类型的默认构造函数来创建该类型的实例。</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  15:</span> <span style="color: #008000">//</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span> <span style="color: #008000">// 参数:</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  17:</span> <span style="color: #008000">//   type:</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span> <span style="color: #008000">//     要创建的对象的类型。</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  19:</span> <span style="color: #008000">//</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  20:</span> <span style="color: #008000">// 返回结果:</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  21:</span> <span style="color: #008000">//     对新创建对象的引用。</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  22:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">object</span> CreateInstance(Type type);</pre>
  </div>
</div>

<h4>3.显示某对象的子级属性值.</h4>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> DisplayPropertyInfo(<span style="color: #0000ff">object</span> obj)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>     StringBuilder sb = <span style="color: #0000ff">new</span> StringBuilder();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>     Type type = obj.GetType();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span>     sb.AppendFormat(<span style="color: #006080">&quot;[{0}]\r\n&quot;</span>,obj);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">foreach</span> (var item <span style="color: #0000ff">in</span> type.GetProperties())</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span>     {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>         sb.AppendFormat(<span style="color: #006080">&quot;\t[{0}:{1}]\r\n&quot;</span>, item.Name,</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span>             item.GetValue(obj, <span style="color: #0000ff">null</span>));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>     }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">return</span> sb.ToString();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span> }</pre>
  </div>
</div>

<h4>4.转换一个DataTable类型的对象成为一个实体集合(Table的列与实体类的属性是一一对应的).</h4>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #008000">/// 数据服务基类对象</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> DataServiceBase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#region</span> DB 处理</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 执行SQL字符串返回一个DataTable</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;sql&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">protected</span> DataTable ExecuteSQL(<span style="color: #0000ff">string</span> sql)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #008000">//*****返回一个DataTable****</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">       <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#region</span> Tabel 类型转换相关</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 转化Table成一个相对应的实体类集合</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;table&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> List&lt;T&gt; ConvertDataTableToEntityCollections&lt;T&gt;(DataTable table)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> T : <span style="color: #0000ff">class</span>, <span style="color: #0000ff">new</span>()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (table != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            PropertyInfo[] pInfos = GetTypePropertyInfo(<span style="color: #0000ff">typeof</span>(T));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            List&lt;T&gt; list = <span style="color: #0000ff">new</span> List&lt;T&gt;(table.Rows.Count);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">foreach</span> (DataRow row <span style="color: #0000ff">in</span> table.Rows)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                T t = <span style="color: #0000ff">new</span> T();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                <span style="color: #0000ff">foreach</span> (var pInfo <span style="color: #0000ff">in</span> pInfos)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                    <span style="color: #0000ff">if</span> (table.Columns.Contains(pInfo.Name))</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                        <span style="color: #008000">//HACK: 对象转换成指定的类型使用 Convert.ChangeType</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                        <span style="color: #008000">// Convert.ChangeType(row[pInfo.Name], pInfo.PropertyType);</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                        <span style="color: #0000ff">try</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                            pInfo.SetValue(t, ChangeType(row[pInfo.Name], pInfo.PropertyType), <span style="color: #0000ff">null</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                        <span style="color: #0000ff">catch</span>(Exception ex){</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                            <span style="color: #0000ff">throw</span> ex;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                list.Add(t);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">return</span> list;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 得到指定SQL执行的结果并返回结果的实体类集合对象</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 本方法综合了 ConvertDataTableToEntityCollections/ExecuteSQL 方法</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;sql&quot;&gt;待执行的SQL&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">protected</span> List&lt;T&gt; GetEntityConllectionsFromSQL&lt;T&gt;(<span style="color: #0000ff">string</span> sql)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> T : <span style="color: #0000ff">class</span>, <span style="color: #0000ff">new</span>()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (<span style="color: #0000ff">string</span>.IsNullOrEmpty(sql))</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">else</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> ConvertDataTableToEntityCollections&lt;T&gt;(ExecuteSQL(sql));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 得到指定类型公开的属性</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;type&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> PropertyInfo[] GetTypePropertyInfo(Type type)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        PropertyInfo[] infos = <span style="color: #0000ff">null</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #008000">//锁定防止 多次 ADD 进缓存</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">lock</span> (_PROPERTYINFO_READ_LOCK)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _PropertyInfoCache.TryGetValue(type, <span style="color: #0000ff">out</span> infos);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">if</span> (infos == <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                infos = type.GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                _PropertyInfoCache.Add(type, infos);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> infos;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 类型转换</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// FROM: http://www.cnblogs.com/cnee5/archive/2006/05/21/405403.html</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;conversionType&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">object</span> ChangeType(<span style="color: #0000ff">object</span> <span style="color: #0000ff">value</span>, Type conversionType)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #008000">//INFO 对DBNull类型特殊处理</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (Convert.IsDBNull(<span style="color: #0000ff">value</span>))</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #008000">//非可空类型的值类型处理</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">if</span> (!(conversionType.IsGenericType &amp;&amp;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                conversionType.GetGenericTypeDefinition().Equals(<span style="color: #0000ff">typeof</span>(Nullable&lt;&gt;))))</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                <span style="color: #0000ff">return</span> Activator.CreateInstance(conversionType);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">else</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #008000">//可空类型类型处理</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (conversionType.IsGenericType &amp;&amp;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                conversionType.GetGenericTypeDefinition().Equals(<span style="color: #0000ff">typeof</span>(Nullable&lt;&gt;)))</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">value</span> == <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            System.ComponentModel.NullableConverter nullableConverter</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                = <span style="color: #0000ff">new</span> System.ComponentModel.NullableConverter(conversionType);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            conversionType = nullableConverter.UnderlyingType;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> Convert.ChangeType(<span style="color: #0000ff">value</span>, conversionType);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">object</span> _PROPERTYINFO_READ_LOCK = <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 实体类属性缓存</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Dictionary&lt;Type, PropertyInfo[]&gt; _PropertyInfoCache = <span style="color: #0000ff">new</span> Dictionary&lt;Type, PropertyInfo[]&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>使用GetEntityConllectionsFromSQL&lt;T&gt;(string sql)方法直接执行一个返回DataTable的ExecuteSQL(sql)方法来快捷完成转换. 在不用ORM框架的情况下就是简单的取得数据变成实体对象集合时很方便, 当然前提是不使用强类型数据集.</p>

<p></p>

<p></p>

<h4>5.转换WebServices生成的实体数据到三层结构已经定义好的实体数据.</h4>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; max-height: 200px">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DataServiceBase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#region</span> 类型转换之用</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 转换一个WebServices Entity</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 此类实体的特点 对于目标实体&quot;属性字段&quot;和现在的实体&quot;属性字段&quot;名称一致,</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 但对于列表实体会是数组保存</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;typeparam name=&quot;TTarget&quot;&gt;&lt;/typeparam&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;typeparam name=&quot;TSource&quot;&gt;&lt;/typeparam&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;entity&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> TTarget ConvertWebServiceEntity&lt;TTarget, TSource&gt;(TSource entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> TTarget : <span style="color: #0000ff">class</span>, <span style="color: #0000ff">new</span>()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> TSource : <span style="color: #0000ff">class</span>, <span style="color: #0000ff">new</span>()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> (TTarget)ConvertWebServiceEntity(<span style="color: #0000ff">typeof</span>(TTarget), <span style="color: #0000ff">typeof</span>(TSource), (<span style="color: #0000ff">object</span>)entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 转换一个WebServices Entity</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 此类实体的特点 对于目标实体&quot;属性字段&quot;和现在的实体&quot;属性字段&quot;名称一致,</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 但对于列表实体会是数组保存</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;tType&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;sType&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;sEntity&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">object</span> ConvertWebServiceEntity(Type tType, Type sType, <span style="color: #0000ff">object</span> sEntity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (sEntity != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">object</span> t = Activator.CreateInstance(tType);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            PropertyInfo[] pTInfos = GetTypePropertyInfo(tType);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            PropertyInfo[] pSInfos = GetTypePropertyInfo(sType);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">int</span> i = 0;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">foreach</span> (var item <span style="color: #0000ff">in</span> pSInfos)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                <span style="color: #0000ff">if</span> (!item.PropertyType.IsArray)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                    pTInfos[i].SetValue(t, item.GetValue(sEntity, <span style="color: #0000ff">null</span>), <span style="color: #0000ff">null</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                <span style="color: #0000ff">else</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                    <span style="color: #008000">//tgType 肯定为泛型</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                    <span style="color: #008000">//taType 肯定为 数组类型</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                    Type tgType = pTInfos[i].PropertyType.GetGenericArguments()[0];</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                    Type taType = item.PropertyType.GetElementType();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                    <span style="color: #0000ff">object</span> tList = ConverWebServiceEntityCollections(tgType, taType, (<span style="color: #0000ff">object</span>[])item.GetValue(sEntity, <span style="color: #0000ff">null</span>));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                    pTInfos[i].SetValue(t, tList, <span style="color: #0000ff">null</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                i++;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">return</span> t;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 转换一个WebServices Entity</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 此类实体的特点 对于目标实体&quot;属性字段&quot;和现在的实体&quot;属性字段&quot;名称一致,</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 但对于列表实体会是数组保存</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;tTarget&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;tSource&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;sCollections&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">object</span> ConverWebServiceEntityCollections(</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        Type tTarget, Type tSource,</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">object</span>[] sCollections)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #008000">//HACK 创建一个目标类型的泛型列表</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        Type gListType = <span style="color: #0000ff">typeof</span>(List&lt;&gt;).MakeGenericType(<span style="color: #0000ff">new</span> Type[] { tTarget });</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">object</span> tgListObj = Activator.CreateInstance(gListType);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        MethodInfo tgAddMi = gListType.GetMethod(<span style="color: #006080">&quot;Add&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">foreach</span> (var item <span style="color: #0000ff">in</span> sCollections)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            tgAddMi.Invoke(tgListObj, <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>[] { ConvertWebServiceEntity(tTarget, tSource, item) });</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> tgListObj;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 从列表到列表</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;typeparam name=&quot;TTarget&quot;&gt;&lt;/typeparam&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;typeparam name=&quot;TSource&quot;&gt;&lt;/typeparam&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;collections&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> List&lt;TTarget&gt; ConverWebServiceEntityCollections&lt;TTarget, TSource&gt;(IEnumerable&lt;TSource&gt; collections)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> TTarget : <span style="color: #0000ff">class</span>, <span style="color: #0000ff">new</span>()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> TSource : <span style="color: #0000ff">class</span>, <span style="color: #0000ff">new</span>()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        List&lt;TTarget&gt; tList = <span style="color: #0000ff">new</span> List&lt;TTarget&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">foreach</span> (var item <span style="color: #0000ff">in</span> collections)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            tList.Add(ConvertWebServiceEntity&lt;TTarget, TSource&gt;(item));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> tList;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// 得到指定类型公开的属性</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;param name=&quot;type&quot;&gt;&lt;/param&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> PropertyInfo[] GetTypePropertyInfo(Type type)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        PropertyInfo[] infos = <span style="color: #0000ff">null</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #008000">//锁定防止 多次 ADD 进缓存</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">lock</span> (_PROPERTYINFO_READ_LOCK)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _PropertyInfoCache.TryGetValue(type, <span style="color: #0000ff">out</span> infos);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">if</span> (infos == <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                infos = type.GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                _PropertyInfoCache.Add(type, infos);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> infos;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">object</span> _PROPERTYINFO_READ_LOCK = <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #008000">/// 实体类属性缓存</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #008000">/// &lt;/summary&gt;</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Dictionary&lt;Type, PropertyInfo[]&gt; _PropertyInfoCache = <span style="color: #0000ff">new</span> Dictionary&lt;Type, PropertyInfo[]&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Web Services 生成实体数据会把List类型的数据转换成数组, 也不知道自己写这个东西是不是正确, 反正是可以使用. 另外NBear4中提供一个ObjectConvertor的东西, 很好用可以支持不同类型之间的转换, 并且还可以自定义转换映射关系.</p>]]></description>
		</item>
		
</channel>
</rss>
