<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>进化的测试 &#187; C#</title>
	<atom:link href="http://magustest.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://magustest.com/blog</link>
	<description>关注软件测试，白盒测试，自动化测试，性能测试</description>
	<lastBuildDate>Wed, 02 Jun 2010 16:12:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>利用序列化和反序列化实现深拷贝</title>
		<link>http://magustest.com/blog/net/serialize-deserialize-implement-deep-copy/</link>
		<comments>http://magustest.com/blog/net/serialize-deserialize-implement-deep-copy/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 06:24:05 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=389</guid>
		<description><![CDATA[假如说有一个简单的类，只有2个属性，那么可以用比较简单的方法实现深拷贝。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 &#91;Serializable&#93; public class Person &#123; public int Height &#123; get; set; &#125; public string FirstName &#123; get; set; &#125; &#125; &#160; class Program &#123; static void Main&#40;string&#91;&#93; args&#41; &#123; //实例化一个对象 Person PersonOne = new Person&#40;&#41;; [...]


Related posts:<ol><li><a href='http://magustest.com/blog/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/' rel='bookmark' title='Permanent Link: PEX-.NET自动化白盒测试工具的介绍(1)'>PEX-.NET自动化白盒测试工具的介绍(1)</a></li>
<li><a href='http://magustest.com/blog/net/linq-type-convert/' rel='bookmark' title='Permanent Link: LINQ查询操作中的类型关系'>LINQ查询操作中的类型关系</a></li>
<li><a href='http://magustest.com/blog/net/automatic-generate-xml-instance-by-using-xsd/' rel='bookmark' title='Permanent Link: 用XSD自动生成XML对应的.NET实体类'>用XSD自动生成XML对应的.NET实体类</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>假如说有一个简单的类，只有2个属性，那么可以用比较简单的方法实现深拷贝。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Serializable<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Person
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Height <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #FF0000;">class</span> Program <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//实例化一个对象</span>
        Person PersonOne <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        PersonOne.<span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;John&quot;</span><span style="color: #008000;">;</span>
        PersonOne.<span style="color: #0000FF;">Height</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">//深拷贝</span>
        Person DeepCopyPerson <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        DeepCopyPerson.<span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> PersonOne.<span style="color: #0000FF;">FirstName</span><span style="color: #008000;">;</span>
        DeepCopyPerson.<span style="color: #0000FF;">Height</span> <span style="color: #008000;">=</span> PersonOne.<span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-389"></span><br />
代码中的DeepCopyPerson对象就是对PersonOne对象的一个深拷贝。其实还可以用序列化和反序列化的方法来实现对对象的深拷贝。Person类的[Serializable]属性是为了下面介绍序列化反序列化方法做深拷贝而做的准备，在这里没什么特别含义。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> T DeepCopy<span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>T obj<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">object</span> retval<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>MemoryStream ms <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        BinaryFormatter bf <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BinaryFormatter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">//序列化成流</span>
        bf.<span style="color: #0000FF;">Serialize</span><span style="color: #000000;">&#40;</span>ms, obj<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ms.<span style="color: #0000FF;">Seek</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, SeekOrigin.<span style="color: #0000FF;">Begin</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">//反序列化成对象</span>
        retval <span style="color: #008000;">=</span> bf.<span style="color: #0000FF;">Deserialize</span><span style="color: #000000;">&#40;</span>ms<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ms.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span>retval<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>t<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>用的时候就是：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Program <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//实例化一个对象</span>
        Person PersonOne <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        PersonOne.<span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;John&quot;</span><span style="color: #008000;">;</span>
        PersonOne.<span style="color: #0000FF;">Height</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">//深拷贝</span>
        Person DeepCopyPerson <span style="color: #008000;">=</span> DeepCopy<span style="color: #008000;">&lt;</span>person<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>PersonOne<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>person<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>只要一个类被声明为可序列化的（就是带有[Serializable]标签），就可以用这个方法来进行深拷贝。记下来，免得以后自己忘记了。</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://magustest.com/blog/feed/" title="RSS"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F&amp;title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D&amp;annotation=%E5%81%87%E5%A6%82%E8%AF%B4%E6%9C%89%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E7%B1%BB%EF%BC%8C%E5%8F%AA%E6%9C%892%E4%B8%AA%E5%B1%9E%E6%80%A7%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8F%AF%E4%BB%A5%E7%94%A8%E6%AF%94%E8%BE%83%E7%AE%80%E5%8D%95%E7%9A%84%E6%96%B9%E6%B3%95%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D%E3%80%82%0D%0A%0D%0A%5BSerializable%5D%0D%0Apublic%20class%20Person%0D%0A%7B%0D%0A%20%20%20%20public%20int%20Height%20%7B%20get%3B%20set%3B%20%7D%0D%0A%20%20%20%20public%20string%20FirstName%20%7B%20get%3B%20set%3B%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Program%20%7B" title="Google Bookmarks"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F&amp;title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D&amp;bodytext=%E5%81%87%E5%A6%82%E8%AF%B4%E6%9C%89%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E7%B1%BB%EF%BC%8C%E5%8F%AA%E6%9C%892%E4%B8%AA%E5%B1%9E%E6%80%A7%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8F%AF%E4%BB%A5%E7%94%A8%E6%AF%94%E8%BE%83%E7%AE%80%E5%8D%95%E7%9A%84%E6%96%B9%E6%B3%95%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D%E3%80%82%0D%0A%0D%0A%5BSerializable%5D%0D%0Apublic%20class%20Person%0D%0A%7B%0D%0A%20%20%20%20public%20int%20Height%20%7B%20get%3B%20set%3B%20%7D%0D%0A%20%20%20%20public%20string%20FirstName%20%7B%20get%3B%20set%3B%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Program%20%7B" title="Digg"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F&amp;title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D&amp;notes=%E5%81%87%E5%A6%82%E8%AF%B4%E6%9C%89%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E7%B1%BB%EF%BC%8C%E5%8F%AA%E6%9C%892%E4%B8%AA%E5%B1%9E%E6%80%A7%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8F%AF%E4%BB%A5%E7%94%A8%E6%AF%94%E8%BE%83%E7%AE%80%E5%8D%95%E7%9A%84%E6%96%B9%E6%B3%95%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D%E3%80%82%0D%0A%0D%0A%5BSerializable%5D%0D%0Apublic%20class%20Person%0D%0A%7B%0D%0A%20%20%20%20public%20int%20Height%20%7B%20get%3B%20set%3B%20%7D%0D%0A%20%20%20%20public%20string%20FirstName%20%7B%20get%3B%20set%3B%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Program%20%7B" title="del.icio.us"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F&amp;t=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D" title="Facebook"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F&title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D" title="豆瓣"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban.png" title="豆瓣" alt="豆瓣" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F&title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D&n=1" title="豆瓣九点"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban9.png" title="豆瓣九点" alt="豆瓣九点" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D&amp;link=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F" title="FriendFeed"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F&amp;title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D" title="Live"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://ping.fm/ref/?link=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F&amp;title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D&amp;body=%E5%81%87%E5%A6%82%E8%AF%B4%E6%9C%89%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E7%B1%BB%EF%BC%8C%E5%8F%AA%E6%9C%892%E4%B8%AA%E5%B1%9E%E6%80%A7%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8F%AF%E4%BB%A5%E7%94%A8%E6%AF%94%E8%BE%83%E7%AE%80%E5%8D%95%E7%9A%84%E6%96%B9%E6%B3%95%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D%E3%80%82%0D%0A%0D%0A%5BSerializable%5D%0D%0Apublic%20class%20Person%0D%0A%7B%0D%0A%20%20%20%20public%20int%20Height%20%7B%20get%3B%20set%3B%20%7D%0D%0A%20%20%20%20public%20string%20FirstName%20%7B%20get%3B%20set%3B%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Program%20%7B" title="Ping.fm"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://shuqian.qq.com/post?jumpback=1&title=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D&uri=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F" title="QQ书签"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/qq.png" title="QQ书签" alt="QQ书签" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=%E5%88%A9%E7%94%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%AE%9E%E7%8E%B0%E6%B7%B1%E6%8B%B7%E8%B4%9D%20-%20http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fserialize-deserialize-implement-deep-copy%2F" title="Twitter"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>

<p>Related posts:<ol><li><a href='http://magustest.com/blog/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/' rel='bookmark' title='Permanent Link: PEX-.NET自动化白盒测试工具的介绍(1)'>PEX-.NET自动化白盒测试工具的介绍(1)</a></li>
<li><a href='http://magustest.com/blog/net/linq-type-convert/' rel='bookmark' title='Permanent Link: LINQ查询操作中的类型关系'>LINQ查询操作中的类型关系</a></li>
<li><a href='http://magustest.com/blog/net/automatic-generate-xml-instance-by-using-xsd/' rel='bookmark' title='Permanent Link: 用XSD自动生成XML对应的.NET实体类'>用XSD自动生成XML对应的.NET实体类</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/net/serialize-deserialize-implement-deep-copy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET平台上的Memcached客户端介绍</title>
		<link>http://magustest.com/blog/net/introduction-to-dotnet-memcached-client/</link>
		<comments>http://magustest.com/blog/net/introduction-to-dotnet-memcached-client/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 08:50:52 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=364</guid>
		<description><![CDATA[早上接到一个任务，需要对Linux服务器的Memcached的update操作进行性能测试，我发现我是一个典型的“手里拿着锤子，就把所有问题都当成钉子”的人。我第一个念头就是，上Memcached的官网找.NET的客户端。最后在Codeplex上找到了一个叫Memcached Providers的客户端程序，很小，218K，里面就3个DLL,一个是Memcached Providers本身的DLL，还有一个是Enyim.Caching，Enyim.Caching也是一个.NET平台上的Memcached客户端，最后就是著名的log4net。 Memcached Providers的配置很方便，首先就是在.NET项目中引用上述提到的3个DLL文件，然后就需要修改项目的配置文件，如果是桌面程序，就修改APP.CONFIG，如果是WEB程序，就修改WEB.CONFIG。 首先在configSections节点下增加如下配置 1 2 3 4 5 6 &#60;section name=&#34;cacheProvider&#34; type=&#34;MemcachedProviders.Cache.CacheProviderSection, MemcachedProviders&#34; allowDefinition=&#34;MachineToApplication&#34; restartOnExternalChanges=&#34;true&#34;/&#62; &#60;sectionGroup name=&#34;enyim.com&#34;&#62; &#60;section name=&#34;memcached&#34; type=&#34;Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching&#34; /&#62; &#60;/sectionGroup&#62; &#60;section name=&#34;log4net&#34; type=&#34;log4net.Config.Log4NetConfigurationSectionHandler,log4net&#34;/&#62; 接着在configuration节点下增加Enyim的配置节点 1 2 3 4 5 6 7 8 9 &#60;enyim.com&#62; &#60;memcached&#62; &#60;servers&#62; &#60;!-- put your own server(s) here--&#62; &#60;add address=&#34;10.60.0.105&#34; port=&#34;19191&#34; /&#62; &#60;/servers&#62; &#60;socketPool minPoolSize=&#34;10&#34; [...]


Related posts:<ol><li><a href='http://magustest.com/blog/net/automatic-generate-xml-instance-by-using-xsd/' rel='bookmark' title='Permanent Link: 用XSD自动生成XML对应的.NET实体类'>用XSD自动生成XML对应的.NET实体类</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/three-different-test-fixture-setup-approach/' rel='bookmark' title='Permanent Link: 单元测试中三种准备Test Fixture的方法比较'>单元测试中三种准备Test Fixture的方法比较</a></li>
<li><a href='http://magustest.com/blog/net/aspnet-web-config-priority/' rel='bookmark' title='Permanent Link: ASP.NET中配置文件web.config的优先级'>ASP.NET中配置文件web.config的优先级</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>早上接到一个任务，需要对Linux服务器的Memcached的update操作进行性能测试，我发现我是一个典型的“手里拿着锤子，就把所有问题都当成钉子”的人。我第一个念头就是，上<a href="http://www.danga.com/memcached/" target="_blank">Memcached的官网</a>找.NET的客户端。最后在Codeplex上找到了一个叫<a href="http://memcachedproviders.codeplex.com/" target="_blank">Memcached Providers</a>的客户端程序，很小，218K，里面就3个DLL,一个是Memcached Providers本身的DLL，还有一个是Enyim.Caching，<a href="http://enyimmemcached.codeplex.com/" target="_blank">Enyim.Caching</a>也是一个.NET平台上的Memcached客户端，最后就是著名的log4net。</p>
<p>Memcached Providers的配置很方便，首先就是在.NET项目中引用上述提到的3个DLL文件，然后就需要修改项目的配置文件，如果是桌面程序，就修改APP.CONFIG，如果是WEB程序，就修改WEB.CONFIG。<br />
<span id="more-364"></span><br />
首先在configSections节点下增加如下配置</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cacheProvider&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;MemcachedProviders.Cache.CacheProviderSection, MemcachedProviders&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">allowDefinition</span>=<span style="color: #ff0000;">&quot;MachineToApplication&quot;</span> <span style="color: #000066;">restartOnExternalChanges</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sectionGroup</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;enyim.com&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;memcached&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sectionGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;log4net&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>接着在configuration节点下增加Enyim的配置节点</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;enyim.com<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;memcached<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #808080; font-style: italic;">&lt;!-- put your own server(s) here--&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;10.60.0.105&quot;</span> <span style="color: #000066;">port</span>=<span style="color: #ff0000;">&quot;19191&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;socketPool</span> <span style="color: #000066;">minPoolSize</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">maxPoolSize</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">connectionTimeout</span>=<span style="color: #ff0000;">&quot;00:00:10&quot;</span> <span style="color: #000066;">deadTimeout</span>=<span style="color: #ff0000;">&quot;00:02:00&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/memcached<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/enyim.com<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>在configuration节点下增加Memcached Providers的配置节点</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cacheProvider</span> <span style="color: #000066;">defaultProvider</span>=<span style="color: #ff0000;">&quot;MemcachedCacheProvider&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;providers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MemcachedCacheProvider&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">keySuffix</span>=<span style="color: #ff0000;">&quot;_MySuffix_&quot;</span> <span style="color: #000066;">defaultExpireTime</span>=<span style="color: #ff0000;">&quot;2000&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/providers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cacheProvider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>最后就是在configuration节点下增加Log4net的配置节点</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">/cacheProvider&gt;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;log4net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ConsoleAppender&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Appender.ConsoleAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Layout.PatternLayout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;conversionPattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;%date [%thread] %-5level %logger [%property{NDC}]- %message%newline&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;priority</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;WARN&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender-ref</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;ConsoleAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Filter.LevelRangeFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;levelMin</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;WARN&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;levelMax</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;FATAL&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/log4net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>最后做一个简单的测试</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TestMethod1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #FF0000;">string</span> key <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;mykey&quot;</span><span style="color: #008000;">;</span>
  <span style="color: #FF0000;">string</span> value <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Success!!!&quot;</span><span style="color: #008000;">;</span>
  DistCache.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>key, value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>                             <span style="color: #008080; font-style: italic;">//存数据</span>
  Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #FF0000;">string</span> ret <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>DistCache.<span style="color: #0000FF;">Get</span><span style="color: #000000;">&#40;</span>key<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>            <span style="color: #008080; font-style: italic;">//读数据</span>
  Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>value, ret<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>                             <span style="color: #008080; font-style: italic;">//验证</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>整个过程下来比较顺利，基本没有遇到问题，这一切准备完毕以后就可以进入测试了。PHP，JAVA，Python等程序语言的Memcached客户端是比较多的，而.NET平台的客户端却只有2、3个，如果打算在.NET程序中使用Memcached，Memcached Providers也是一个不错的选择。</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://magustest.com/blog/feed/" title="RSS"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F&amp;title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D&amp;annotation=%E6%97%A9%E4%B8%8A%E6%8E%A5%E5%88%B0%E4%B8%80%E4%B8%AA%E4%BB%BB%E5%8A%A1%EF%BC%8C%E9%9C%80%E8%A6%81%E5%AF%B9Linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84Memcached%E7%9A%84update%E6%93%8D%E4%BD%9C%E8%BF%9B%E8%A1%8C%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95%EF%BC%8C%E6%88%91%E5%8F%91%E7%8E%B0%E6%88%91%E6%98%AF%E4%B8%80%E4%B8%AA%E5%85%B8%E5%9E%8B%E7%9A%84%E2%80%9C%E6%89%8B%E9%87%8C%E6%8B%BF%E7%9D%80%E9%94%A4%E5%AD%90%EF%BC%8C%E5%B0%B1%E6%8A%8A%E6%89%80%E6%9C%89%E9%97%AE%E9%A2%98%E9%83%BD%E5%BD%93%E6%88%90%E9%92%89%E5%AD%90%E2%80%9D%E7%9A%84%E4%BA%BA%E3%80%82%E6%88%91%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%BF%B5%E5%A4%B4%E5%B0%B1%E6%98%AF%EF%BC%8C%E4%B8%8AMemcached%E7%9A%84%E5%AE%98%E7%BD%91%E6%89%BE.N" title="Google Bookmarks"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F&amp;title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D&amp;bodytext=%E6%97%A9%E4%B8%8A%E6%8E%A5%E5%88%B0%E4%B8%80%E4%B8%AA%E4%BB%BB%E5%8A%A1%EF%BC%8C%E9%9C%80%E8%A6%81%E5%AF%B9Linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84Memcached%E7%9A%84update%E6%93%8D%E4%BD%9C%E8%BF%9B%E8%A1%8C%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95%EF%BC%8C%E6%88%91%E5%8F%91%E7%8E%B0%E6%88%91%E6%98%AF%E4%B8%80%E4%B8%AA%E5%85%B8%E5%9E%8B%E7%9A%84%E2%80%9C%E6%89%8B%E9%87%8C%E6%8B%BF%E7%9D%80%E9%94%A4%E5%AD%90%EF%BC%8C%E5%B0%B1%E6%8A%8A%E6%89%80%E6%9C%89%E9%97%AE%E9%A2%98%E9%83%BD%E5%BD%93%E6%88%90%E9%92%89%E5%AD%90%E2%80%9D%E7%9A%84%E4%BA%BA%E3%80%82%E6%88%91%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%BF%B5%E5%A4%B4%E5%B0%B1%E6%98%AF%EF%BC%8C%E4%B8%8AMemcached%E7%9A%84%E5%AE%98%E7%BD%91%E6%89%BE.N" title="Digg"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F&amp;title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D&amp;notes=%E6%97%A9%E4%B8%8A%E6%8E%A5%E5%88%B0%E4%B8%80%E4%B8%AA%E4%BB%BB%E5%8A%A1%EF%BC%8C%E9%9C%80%E8%A6%81%E5%AF%B9Linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84Memcached%E7%9A%84update%E6%93%8D%E4%BD%9C%E8%BF%9B%E8%A1%8C%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95%EF%BC%8C%E6%88%91%E5%8F%91%E7%8E%B0%E6%88%91%E6%98%AF%E4%B8%80%E4%B8%AA%E5%85%B8%E5%9E%8B%E7%9A%84%E2%80%9C%E6%89%8B%E9%87%8C%E6%8B%BF%E7%9D%80%E9%94%A4%E5%AD%90%EF%BC%8C%E5%B0%B1%E6%8A%8A%E6%89%80%E6%9C%89%E9%97%AE%E9%A2%98%E9%83%BD%E5%BD%93%E6%88%90%E9%92%89%E5%AD%90%E2%80%9D%E7%9A%84%E4%BA%BA%E3%80%82%E6%88%91%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%BF%B5%E5%A4%B4%E5%B0%B1%E6%98%AF%EF%BC%8C%E4%B8%8AMemcached%E7%9A%84%E5%AE%98%E7%BD%91%E6%89%BE.N" title="del.icio.us"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F&amp;t=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D" title="Facebook"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F&title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D" title="豆瓣"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban.png" title="豆瓣" alt="豆瓣" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F&title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D&n=1" title="豆瓣九点"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban9.png" title="豆瓣九点" alt="豆瓣九点" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D&amp;link=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F" title="FriendFeed"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F&amp;title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D" title="Live"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://ping.fm/ref/?link=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F&amp;title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D&amp;body=%E6%97%A9%E4%B8%8A%E6%8E%A5%E5%88%B0%E4%B8%80%E4%B8%AA%E4%BB%BB%E5%8A%A1%EF%BC%8C%E9%9C%80%E8%A6%81%E5%AF%B9Linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84Memcached%E7%9A%84update%E6%93%8D%E4%BD%9C%E8%BF%9B%E8%A1%8C%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95%EF%BC%8C%E6%88%91%E5%8F%91%E7%8E%B0%E6%88%91%E6%98%AF%E4%B8%80%E4%B8%AA%E5%85%B8%E5%9E%8B%E7%9A%84%E2%80%9C%E6%89%8B%E9%87%8C%E6%8B%BF%E7%9D%80%E9%94%A4%E5%AD%90%EF%BC%8C%E5%B0%B1%E6%8A%8A%E6%89%80%E6%9C%89%E9%97%AE%E9%A2%98%E9%83%BD%E5%BD%93%E6%88%90%E9%92%89%E5%AD%90%E2%80%9D%E7%9A%84%E4%BA%BA%E3%80%82%E6%88%91%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%BF%B5%E5%A4%B4%E5%B0%B1%E6%98%AF%EF%BC%8C%E4%B8%8AMemcached%E7%9A%84%E5%AE%98%E7%BD%91%E6%89%BE.N" title="Ping.fm"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://shuqian.qq.com/post?jumpback=1&title=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D&uri=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F" title="QQ书签"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/qq.png" title="QQ书签" alt="QQ书签" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=.NET%E5%B9%B3%E5%8F%B0%E4%B8%8A%E7%9A%84Memcached%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BB%8B%E7%BB%8D%20-%20http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fintroduction-to-dotnet-memcached-client%2F" title="Twitter"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>

<p>Related posts:<ol><li><a href='http://magustest.com/blog/net/automatic-generate-xml-instance-by-using-xsd/' rel='bookmark' title='Permanent Link: 用XSD自动生成XML对应的.NET实体类'>用XSD自动生成XML对应的.NET实体类</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/three-different-test-fixture-setup-approach/' rel='bookmark' title='Permanent Link: 单元测试中三种准备Test Fixture的方法比较'>单元测试中三种准备Test Fixture的方法比较</a></li>
<li><a href='http://magustest.com/blog/net/aspnet-web-config-priority/' rel='bookmark' title='Permanent Link: ASP.NET中配置文件web.config的优先级'>ASP.NET中配置文件web.config的优先级</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/net/introduction-to-dotnet-memcached-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LINQ查询操作中的类型关系</title>
		<link>http://magustest.com/blog/net/linq-type-convert/</link>
		<comments>http://magustest.com/blog/net/linq-type-convert/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 04:10:14 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=333</guid>
		<description><![CDATA[最近在测试中，想尝试用LINQ来简化一下测试的代码，初步学习了一下LINQ，个人感觉需要认真理解查询操作中的类型关系，在阅读MSDN文档后写下了本文，主要参考MSDN讲述理解查询操作中的类型关系，还有对C# 3.0的一个新的关键字var的理解。 首先有一段LINQ查询表达式的代码 1 2 3 4 5 6 7 8 List&#60;onlineUser&#62; ret = AccountGateway.AccountLiteProvider.GetOnlineUsers&#40;1, 10000&#41;; var query = from onlineUser in ret where &#40;onlineUser.UserId &#62; 1301000200&#41; select onlineUser.UserId; foreach&#40;int iUser in query&#41; &#123; Console.WriteLine&#40;iUser.ToString&#40;&#41;&#41;; &#125; 这段代码首先查询出10000个在线用户，然后在这10000个在线用户中筛选出ID大于1301000200的用户ID。其中各个变量的类型如下： onlineUser &#8212; OnlineUser类 query &#8212; IEnumerable&#60;int&#62; iUser &#8212; int 下面可以看一下MSDN的详细说明： 数据源的类型参数决定范围变量的类型。 选择的对象的类型决定查询变量的类型。此处的 name 为一个字符串。因此，查询变量是一个 IEnumerable&#60;string&#62;。 在 foreach 语句中循环访问查询变量。因为查询变量是一个字符串序列，所以迭代变量也是一个字符串。 [...]


Related posts:<ol><li><a href='http://magustest.com/blog/softwaretesting/using-httpcontext-in-unittest/' rel='bookmark' title='Permanent Link: 在C#单元测试中使用HttpContext的简单解决办法'>在C#单元测试中使用HttpContext的简单解决办法</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/using-linq-to-object-in-testing/' rel='bookmark' title='Permanent Link: Linq to Object在测试中的应用'>Linq to Object在测试中的应用</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/data-driven-test-in-unit-test/' rel='bookmark' title='Permanent Link: 在单元测试中应用数据驱动'>在单元测试中应用数据驱动</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>最近在测试中，想尝试用LINQ来简化一下测试的代码，初步学习了一下LINQ，个人感觉需要认真理解查询操作中的类型关系，在阅读MSDN文档后写下了本文，主要参考MSDN讲述理解查询操作中的类型关系，还有对C# 3.0的一个新的关键字var的理解。</p>
<p>首先有一段LINQ查询表达式的代码</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>onlineUser<span style="color: #008000;">&gt;</span> ret <span style="color: #008000;">=</span> AccountGateway.<span style="color: #0000FF;">AccountLiteProvider</span>.<span style="color: #0000FF;">GetOnlineUsers</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var query <span style="color: #008000;">=</span> from onlineUser <span style="color: #0600FF;">in</span> ret
			where <span style="color: #000000;">&#40;</span>onlineUser.<span style="color: #0000FF;">UserId</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1301000200</span><span style="color: #000000;">&#41;</span>
			select onlineUser.<span style="color: #0000FF;">UserId</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> iUser <span style="color: #0600FF;">in</span> query<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>iUser.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-333"></span><br />
这段代码首先查询出10000个在线用户，然后在这10000个在线用户中筛选出ID大于1301000200的用户ID。其中各个变量的类型如下：</p>
<ul>
<li>onlineUser &#8212;  OnlineUser类</li>
<li>query &#8212; IEnumerable&lt;int&gt;</li>
<li>iUser &#8212; int</li>
</ul>
<p>下面可以看一下MSDN的详细说明：</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/02/linq-ienumerable1.png"><img class="alignnone size-full wp-image-335" title="linq-IEnumerable" src="http://magustest.com/blog/wp-content/uploads/2009/02/linq-ienumerable1.png" alt="" width="403" height="228" /></a></p>
<ol>
<li>数据源的类型参数决定范围变量的类型。</li>
<li>选择的对象的类型决定查询变量的类型。此处的 <span class="code">name</span> 为一个字符串。因此，查询变量是一个  <strong>IEnumerable</strong>&lt;string&gt;。</li>
<li>在 <span class="keyword">foreach</span> 语句中循环访问查询变量。因为查询变量是一个字符串序列，所以迭代变量也是一个字符串。</li>
</ol>
<p>下面这个例子跟本文刚开始的代码例子相似</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/02/linq-data-conversion1.png"><img class="alignnone size-full wp-image-336" title="linq-data-conversion" src="http://magustest.com/blog/wp-content/uploads/2009/02/linq-data-conversion1.png" alt="" width="404" height="230" /></a></p>
<ol>
<li>数据源的类型参数决定范围变量的类型。</li>
<li><span class="keyword">select</span> 语句返回 <span class="code">Name</span> 属性，而非完整的  <span class="code">Customer</span> 对象。因为 <span class="code">Name</span> 是一个字符串，所以  <span class="code">custNameQuery</span> 的类型参数是 <span class="keyword">string</span>，而非 <span class="code">Customer</span>。</li>
<li>因为 <span class="code">custNameQuery</span> 是一个字符串序列，所以 <span class="keyword">foreach</span> 循环的迭代变量也必须是 <span class="keyword">string</span>。</li>
</ol>
<p>像上面这样做，有点不爽的就是每个变量类型都要编程的人来指定，C# 3.0还有一个新特性，就是隐式类型本地变量，也就是var关键字</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/02/linq-impl-var1.png"><img class="alignnone size-full wp-image-337" title="linq-impl-var" src="http://magustest.com/blog/wp-content/uploads/2009/02/linq-impl-var1.png" alt="" width="403" height="227" /></a></p>
<ol>
<li>数据源的类型参数始终为查询中的范围变量的类型。</li>
<li>因为 <span class="code">select</span> 语句生成匿名类型，所以必须使用 <span class="keyword">var</span> 隐式类型化查询变量。</li>
<li>因为查询变量的类型是隐式的，所以 <span class="keyword">foreach</span> 循环中的迭代变量也必须是隐式的。</li>
</ol>
<p>到此，对LINQ查询操作中的类型关系也比较明白了吧。</p>
<p>一个变量被var关键字声明为隐式类型本地变量后，它并不是一个动态变量，C#动态变量要等4.0的dynamic关键字。那么这个var是什么呢？其实var也是一个强类型变量，它的类型是在编译时就已经确定下来了，并不是运行时才确定下来的。例如：</p>
<p>var s = &#8220;I am a string&#8221;;   //s的类型是string</p>
<p>var i = 88;                    //i的类型是int</p>
<p>s = 99;                         //编译出错，不能把一个int赋值给string类型变量</p>
<p>用Reflector看编译后的代码就是这样子：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>onlineUser<span style="color: #008000;">&gt;</span> ret <span style="color: #008000;">=</span> AccountGateway.<span style="color: #0000FF;">AccountLiteProvider</span>.<span style="color: #0000FF;">GetOnlineUsers</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> query <span style="color: #008000;">=</span> from onlineUser <span style="color: #0600FF;">in</span> ret
			where onlineUser.<span style="color: #0000FF;">UserId</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1301000200</span>
			select onlineUser.<span style="color: #0000FF;">UserId</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> iUser <span style="color: #0600FF;">in</span> query<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>iUser.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>稍微总结一下var</p>
<ul>
<li>var还是强类型变量，不是弱类型变量</li>
<li>使用var不会带来性能损失，因为编译为IL以后，变量的类型已经是确定，不是在运行时通过反射获取类型。</li>
</ul>
<p>再最后，C#3.0, 3.5的很多新特性感觉都是为LINQ而出现的，例如：隐式类型化本地变量，匿名函数，匿名类，lambda表达式，扩展方法……</p>
<p>本文参考MSDN：http://msdn.microsoft.com/zh-cn/library/bb397924.aspx</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://magustest.com/blog/feed/" title="RSS"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F&amp;title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB&amp;annotation=%E6%9C%80%E8%BF%91%E5%9C%A8%E6%B5%8B%E8%AF%95%E4%B8%AD%EF%BC%8C%E6%83%B3%E5%B0%9D%E8%AF%95%E7%94%A8LINQ%E6%9D%A5%E7%AE%80%E5%8C%96%E4%B8%80%E4%B8%8B%E6%B5%8B%E8%AF%95%E7%9A%84%E4%BB%A3%E7%A0%81%EF%BC%8C%E5%88%9D%E6%AD%A5%E5%AD%A6%E4%B9%A0%E4%BA%86%E4%B8%80%E4%B8%8BLINQ%EF%BC%8C%E4%B8%AA%E4%BA%BA%E6%84%9F%E8%A7%89%E9%9C%80%E8%A6%81%E8%AE%A4%E7%9C%9F%E7%90%86%E8%A7%A3%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB%EF%BC%8C%E5%9C%A8%E9%98%85%E8%AF%BBMSDN%E6%96%87%E6%A1%A3%E5%90%8E%E5%86%99%E4%B8%8B%E4%BA%86%E6%9C%AC%E6%96%87%EF%BC%8C%E4%B8%BB%E8%A6%81%E5%8F%82%E8%80%83MSDN%E8%AE%B2%E8%BF%B0%E7%90%86%E8%A7%A3%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB" title="Google Bookmarks"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F&amp;title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB&amp;bodytext=%E6%9C%80%E8%BF%91%E5%9C%A8%E6%B5%8B%E8%AF%95%E4%B8%AD%EF%BC%8C%E6%83%B3%E5%B0%9D%E8%AF%95%E7%94%A8LINQ%E6%9D%A5%E7%AE%80%E5%8C%96%E4%B8%80%E4%B8%8B%E6%B5%8B%E8%AF%95%E7%9A%84%E4%BB%A3%E7%A0%81%EF%BC%8C%E5%88%9D%E6%AD%A5%E5%AD%A6%E4%B9%A0%E4%BA%86%E4%B8%80%E4%B8%8BLINQ%EF%BC%8C%E4%B8%AA%E4%BA%BA%E6%84%9F%E8%A7%89%E9%9C%80%E8%A6%81%E8%AE%A4%E7%9C%9F%E7%90%86%E8%A7%A3%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB%EF%BC%8C%E5%9C%A8%E9%98%85%E8%AF%BBMSDN%E6%96%87%E6%A1%A3%E5%90%8E%E5%86%99%E4%B8%8B%E4%BA%86%E6%9C%AC%E6%96%87%EF%BC%8C%E4%B8%BB%E8%A6%81%E5%8F%82%E8%80%83MSDN%E8%AE%B2%E8%BF%B0%E7%90%86%E8%A7%A3%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB" title="Digg"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F&amp;title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB&amp;notes=%E6%9C%80%E8%BF%91%E5%9C%A8%E6%B5%8B%E8%AF%95%E4%B8%AD%EF%BC%8C%E6%83%B3%E5%B0%9D%E8%AF%95%E7%94%A8LINQ%E6%9D%A5%E7%AE%80%E5%8C%96%E4%B8%80%E4%B8%8B%E6%B5%8B%E8%AF%95%E7%9A%84%E4%BB%A3%E7%A0%81%EF%BC%8C%E5%88%9D%E6%AD%A5%E5%AD%A6%E4%B9%A0%E4%BA%86%E4%B8%80%E4%B8%8BLINQ%EF%BC%8C%E4%B8%AA%E4%BA%BA%E6%84%9F%E8%A7%89%E9%9C%80%E8%A6%81%E8%AE%A4%E7%9C%9F%E7%90%86%E8%A7%A3%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB%EF%BC%8C%E5%9C%A8%E9%98%85%E8%AF%BBMSDN%E6%96%87%E6%A1%A3%E5%90%8E%E5%86%99%E4%B8%8B%E4%BA%86%E6%9C%AC%E6%96%87%EF%BC%8C%E4%B8%BB%E8%A6%81%E5%8F%82%E8%80%83MSDN%E8%AE%B2%E8%BF%B0%E7%90%86%E8%A7%A3%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB" title="del.icio.us"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F&amp;t=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB" title="Facebook"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F&title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB" title="豆瓣"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban.png" title="豆瓣" alt="豆瓣" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F&title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB&n=1" title="豆瓣九点"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban9.png" title="豆瓣九点" alt="豆瓣九点" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB&amp;link=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F" title="FriendFeed"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F&amp;title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB" title="Live"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://ping.fm/ref/?link=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F&amp;title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB&amp;body=%E6%9C%80%E8%BF%91%E5%9C%A8%E6%B5%8B%E8%AF%95%E4%B8%AD%EF%BC%8C%E6%83%B3%E5%B0%9D%E8%AF%95%E7%94%A8LINQ%E6%9D%A5%E7%AE%80%E5%8C%96%E4%B8%80%E4%B8%8B%E6%B5%8B%E8%AF%95%E7%9A%84%E4%BB%A3%E7%A0%81%EF%BC%8C%E5%88%9D%E6%AD%A5%E5%AD%A6%E4%B9%A0%E4%BA%86%E4%B8%80%E4%B8%8BLINQ%EF%BC%8C%E4%B8%AA%E4%BA%BA%E6%84%9F%E8%A7%89%E9%9C%80%E8%A6%81%E8%AE%A4%E7%9C%9F%E7%90%86%E8%A7%A3%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB%EF%BC%8C%E5%9C%A8%E9%98%85%E8%AF%BBMSDN%E6%96%87%E6%A1%A3%E5%90%8E%E5%86%99%E4%B8%8B%E4%BA%86%E6%9C%AC%E6%96%87%EF%BC%8C%E4%B8%BB%E8%A6%81%E5%8F%82%E8%80%83MSDN%E8%AE%B2%E8%BF%B0%E7%90%86%E8%A7%A3%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB" title="Ping.fm"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://shuqian.qq.com/post?jumpback=1&title=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB&uri=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F" title="QQ书签"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/qq.png" title="QQ书签" alt="QQ书签" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=LINQ%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%85%B3%E7%B3%BB%20-%20http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Flinq-type-convert%2F" title="Twitter"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>

<p>Related posts:<ol><li><a href='http://magustest.com/blog/softwaretesting/using-httpcontext-in-unittest/' rel='bookmark' title='Permanent Link: 在C#单元测试中使用HttpContext的简单解决办法'>在C#单元测试中使用HttpContext的简单解决办法</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/using-linq-to-object-in-testing/' rel='bookmark' title='Permanent Link: Linq to Object在测试中的应用'>Linq to Object在测试中的应用</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/data-driven-test-in-unit-test/' rel='bookmark' title='Permanent Link: 在单元测试中应用数据驱动'>在单元测试中应用数据驱动</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/net/linq-type-convert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何把GUID转换为整形INT数值</title>
		<link>http://magustest.com/blog/net/convert-guid-to-integer/</link>
		<comments>http://magustest.com/blog/net/convert-guid-to-integer/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 13:56:34 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=247</guid>
		<description><![CDATA[在前面一篇介绍在测试过程中创建唯一数据的文章中，推荐使用GUID作为生成唯一测试数据的方法，本人在测试过程中碰到一个问题，就是对于电话号码的生成，如何使得生成的数据尽可能地达到不重复。C#库里面有一个类-Random，这个类可以帮助开发者或者测试员生成随机数，但是大家都知道，计算机生成的随机数准确来说是伪随机数。本人曾经做过一个小实验，用当前时间作为随机种子，创建1000个随机数，里面重复值是相当多。 沿用前一篇文章的思想，我想把GUID转换为整数，那么应该怎么做呢？在程序内部，GUID也是可以看作是一个整数形式存在的变量，当要做这样子的转换的时候，需要了解的是转换会带来信息的丢失；就好像把一个long转换为一个int，有时候会有信息的丢失。我们可以容忍这个丢失，因为从实践来看，用GUID转成int，比Random类生成随机数好随机的多。以下是如何把GUID转换为int的一个方法： 1 2 3 Guid tempGuid = Guid.NewGuid&#40;&#41;; byte&#91;&#93; bytes = tempGuid.ToByteArray&#40;&#41;; int i = Math.Abs&#40;&#40;&#40;int&#41;bytes&#91;0&#93;&#41; &#124; &#40;&#40;int&#41;bytes&#91;1&#93; &#60;&#60; 8&#41; &#124; &#40;&#40;int&#41;bytes&#91;2&#93; &#60;&#60; 16&#41; &#124; &#40;&#40;int&#41;bytes&#91;3&#93; &#60;&#60; 24&#41;&#41;; 这样就可以生成一个正整数了。 经过兄弟的留言，C#下其实根本不用那样子折腾 1 int i = Math.Abs&#40;Guid.NewGuid&#40;&#41;.GetHashCode&#40;&#41;&#41;; Share and Enjoy: Related posts:在测试中使用正确的方法创建唯一的测试数据


Related posts:<ol><li><a href='http://magustest.com/blog/whiteboxtesting/creating-unique-test-data/' rel='bookmark' title='Permanent Link: 在测试中使用正确的方法创建唯一的测试数据'>在测试中使用正确的方法创建唯一的测试数据</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>在前面一篇<a href="http://magustest.com/blog/softwaretesting/whiteboxtesting/creating-unique-test-data/ " target="_blank">介绍在测试过程中创建唯一数据的文章中</a>，推荐使用GUID作为生成唯一测试数据的方法，本人在测试过程中碰到一个问题，就是对于电话号码的生成，如何使得生成的数据尽可能地达到不重复。C#库里面有一个类-Random，这个类可以帮助开发者或者测试员生成随机数，但是大家都知道，计算机生成的随机数准确来说是伪随机数。本人曾经做过一个小实验，用当前时间作为随机种子，创建1000个随机数，里面重复值是相当多。</p>
<p>沿用前一篇文章的思想，我想把GUID转换为整数，那么应该怎么做呢？在程序内部，GUID也是可以看作是一个整数形式存在的变量，当要做这样子的转换的时候，需要了解的是转换会带来信息的丢失；就好像把一个long转换为一个int，有时候会有信息的丢失。我们可以容忍这个丢失，因为从实践来看，用GUID转成int，比Random类生成随机数好随机的多。以下是如何把GUID转换为int的一个方法：<br />
<span id="more-247"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Guid tempGuid <span style="color: #008000;">=</span> Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bytes <span style="color: #008000;">=</span> tempGuid.<span style="color: #0000FF;">ToByteArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Abs</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>bytes<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">|</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>bytes<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">&lt;&lt;</span> <span style="color: #FF0000;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">|</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>bytes<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">&lt;&lt;</span> <span style="color: #FF0000;">16</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">|</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>bytes<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">&lt;&lt;</span> <span style="color: #FF0000;">24</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>这样就可以生成一个正整数了。</p>
<p>经过兄弟的留言，C#下其实根本不用那样子折腾</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Abs</span><span style="color: #000000;">&#40;</span>Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetHashCode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>




Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://magustest.com/blog/feed/" title="RSS"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F&amp;title=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC&amp;notes=%E5%9C%A8%E5%89%8D%E9%9D%A2%E4%B8%80%E7%AF%87%E4%BB%8B%E7%BB%8D%E5%9C%A8%E6%B5%8B%E8%AF%95%E8%BF%87%E7%A8%8B%E4%B8%AD%E5%88%9B%E5%BB%BA%E5%94%AF%E4%B8%80%E6%95%B0%E6%8D%AE%E7%9A%84%E6%96%87%E7%AB%A0%E4%B8%AD%EF%BC%8C%E6%8E%A8%E8%8D%90%E4%BD%BF%E7%94%A8GUID%E4%BD%9C%E4%B8%BA%E7%94%9F%E6%88%90%E5%94%AF%E4%B8%80%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%E7%9A%84%E6%96%B9%E6%B3%95%EF%BC%8C%E6%9C%AC%E4%BA%BA%E5%9C%A8%E6%B5%8B%E8%AF%95%E8%BF%87%E7%A8%8B%E4%B8%AD%E7%A2%B0%E5%88%B0%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98%EF%BC%8C%E5%B0%B1%E6%98%AF%E5%AF%B9%E4%BA%8E%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%E7%9A%84%E7%94%9F%E6%88%90%EF%BC%8C%E5%A6%82%E4%BD%95%E4%BD%BF%E5%BE%97%E7%94%9F%E6%88%90%E7%9A%84%E6%95%B0%E6%8D%AE%E5%B0%BD%E5%8F%AF%E8%83%BD%E5%9C%B0" title="del.icio.us"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F&amp;t=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC" title="Facebook"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F&title=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC" title="豆瓣"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban.png" title="豆瓣" alt="豆瓣" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F&title=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC&n=1" title="豆瓣九点"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban9.png" title="豆瓣九点" alt="豆瓣九点" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC&amp;link=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F" title="FriendFeed"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F&amp;title=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC" title="Live"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://ping.fm/ref/?link=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F&amp;title=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC&amp;body=%E5%9C%A8%E5%89%8D%E9%9D%A2%E4%B8%80%E7%AF%87%E4%BB%8B%E7%BB%8D%E5%9C%A8%E6%B5%8B%E8%AF%95%E8%BF%87%E7%A8%8B%E4%B8%AD%E5%88%9B%E5%BB%BA%E5%94%AF%E4%B8%80%E6%95%B0%E6%8D%AE%E7%9A%84%E6%96%87%E7%AB%A0%E4%B8%AD%EF%BC%8C%E6%8E%A8%E8%8D%90%E4%BD%BF%E7%94%A8GUID%E4%BD%9C%E4%B8%BA%E7%94%9F%E6%88%90%E5%94%AF%E4%B8%80%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%E7%9A%84%E6%96%B9%E6%B3%95%EF%BC%8C%E6%9C%AC%E4%BA%BA%E5%9C%A8%E6%B5%8B%E8%AF%95%E8%BF%87%E7%A8%8B%E4%B8%AD%E7%A2%B0%E5%88%B0%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98%EF%BC%8C%E5%B0%B1%E6%98%AF%E5%AF%B9%E4%BA%8E%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%E7%9A%84%E7%94%9F%E6%88%90%EF%BC%8C%E5%A6%82%E4%BD%95%E4%BD%BF%E5%BE%97%E7%94%9F%E6%88%90%E7%9A%84%E6%95%B0%E6%8D%AE%E5%B0%BD%E5%8F%AF%E8%83%BD%E5%9C%B0" title="Ping.fm"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://shuqian.qq.com/post?jumpback=1&title=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC&uri=http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F" title="QQ书签"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/qq.png" title="QQ书签" alt="QQ书签" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=%E5%A6%82%E4%BD%95%E6%8A%8AGUID%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B4%E5%BD%A2INT%E6%95%B0%E5%80%BC%20-%20http%3A%2F%2Fmagustest.com%2Fblog%2Fnet%2Fconvert-guid-to-integer%2F" title="Twitter"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>

<p>Related posts:<ol><li><a href='http://magustest.com/blog/whiteboxtesting/creating-unique-test-data/' rel='bookmark' title='Permanent Link: 在测试中使用正确的方法创建唯一的测试数据'>在测试中使用正确的方法创建唯一的测试数据</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/net/convert-guid-to-integer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>在C#单元测试中使用HttpContext的简单解决办法</title>
		<link>http://magustest.com/blog/softwaretesting/using-httpcontext-in-unittest/</link>
		<comments>http://magustest.com/blog/softwaretesting/using-httpcontext-in-unittest/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 07:31:59 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[软件测试]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[单元测试]]></category>
		<category><![CDATA[白盒测试]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=233</guid>
		<description><![CDATA[场景：最近在测试一个.NET的Http Module，这个Module是用来做URL重写的。刚开始进展的比较顺利，因为该Module里面的方法参数基本上都是String，后来这个Module进行了一下重构，所有参数都变成了HttpContext了，这就直接导致原来的单元测试都跑不起来了，接着就开始了弄HttpContext了。 1. 采用Visual Studio自带的ASP.NET单元测试 刚开始我看了一下被测试的代码，虽然说用到了HttpContext，但是有很多地方我都可以绕过去的，意思就是这个HttpContext只是名以上需要的一个参数，只要它不是NULL就可以了，并不影响我的测试，所以我采用了ASP.NET Unit Test的办法来获取一个HttpContext，这个方法实现起来是最简单的，但是会有一些问题，后面会提及到。 首先新建一个WEB项目，然后把被测的Http Module挂到这个新建的WEB项目中。然后就可以配置ASP.NET单元测试，把原来一般的单元测试改造成为一个ASP.NET的单元测试。步骤如下，在测试方法前面加上以下属性 1 2 3 &#91;HostType&#40;&#34;ASP.NET&#34;&#41;&#93; &#91;UrlToTest&#40;&#34;http://localhost:6988/Default.aspx&#34;&#41;&#93; &#91;AspNetDevelopmentServerHost&#40;&#34;D:\\MS_Code\\Public\\UrlRouter.WebTest&#34;, &#34;/&#34;&#41;&#93; [UrlToTest] &#8212; 这个属性指定了运行该单元测试时的URL [HostType] &#8212; 一般的单元测试是在VSTest的宿主进程下运行，所以是没有HttpContext的；如果作为ASP.NET单元测试，那么必须要在ASP.NET宿主进程下运行 [AspNetDevelopmentServerHost] &#8212; 由于我使用了ASP.NET Development Server 作为测试的主机服务器，而不是IIS；所以我需要设置这个属性来指定Web应用程序的完整路径。这里我需要把目录指向刚才新建的WEB项目的路径中。这样子就可以在单元测试中直接使用HttpContext.Current来获取一个HttpContext了。 1 2 3 4 5 6 7 8 9 10 11 &#91;&#91;TestMethod&#93; &#91;HostType&#40;&#34;ASP.NET&#34;&#41;&#93; &#91;AspNetDevelopmentServerHost&#40;&#34;D:\\MS_Code\\Public\\UrlRouter.WebTest&#34;, &#34;/&#34;&#41;&#93; &#91;UrlToTest&#40;&#34;http://localhost:6988/Default.aspx&#34;&#41;&#93; public void Fuseaction_PUT&#40;&#41; &#123; handler = new UrlMapHandler&#40;HttpContext.Current&#41;; Assert.IsNotNull&#40;handler&#41;; result [...]


Related posts:<ol><li><a href='http://magustest.com/blog/net/introduction-to-dotnet-memcached-client/' rel='bookmark' title='Permanent Link: .NET平台上的Memcached客户端介绍'>.NET平台上的Memcached客户端介绍</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/three-different-test-fixture-setup-approach/' rel='bookmark' title='Permanent Link: 单元测试中三种准备Test Fixture的方法比较'>单元测试中三种准备Test Fixture的方法比较</a></li>
<li><a href='http://magustest.com/blog/net/serialize-deserialize-implement-deep-copy/' rel='bookmark' title='Permanent Link: 利用序列化和反序列化实现深拷贝'>利用序列化和反序列化实现深拷贝</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>场景：最近在测试一个.NET的Http Module，这个Module是用来做URL重写的。刚开始进展的比较顺利，因为该Module里面的方法参数基本上都是String，后来这个Module进行了一下重构，所有参数都变成了HttpContext了，这就直接导致原来的单元测试都跑不起来了，接着就开始了弄HttpContext了。</p>
<p><strong><span style="font-size: medium; color: #3366ff;">1. 采用Visual Studio自带的ASP.NET单元测试</span></strong></p>
<p>刚开始我看了一下被测试的代码，虽然说用到了HttpContext，但是有很多地方我都可以绕过去的，意思就是这个HttpContext只是名以上需要的一个参数，只要它不是NULL就可以了，并不影响我的测试，所以我采用了ASP.NET Unit Test的办法来获取一个HttpContext，这个方法实现起来是最简单的，但是会有一些问题，后面会提及到。<br />
<span id="more-233"></span><br />
首先新建一个WEB项目，然后把被测的Http Module挂到这个新建的WEB项目中。然后就可以配置ASP.NET单元测试，把原来一般的单元测试改造成为一个ASP.NET的单元测试。步骤如下，在测试方法前面加上以下属性</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>HostType<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ASP.NET&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>UrlToTest<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://localhost:6988/Default.aspx&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>AspNetDevelopmentServerHost<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;D:<span style="color: #008080; font-weight: bold;">\\</span>MS_Code<span style="color: #008080; font-weight: bold;">\\</span>Public<span style="color: #008080; font-weight: bold;">\\</span>UrlRouter.WebTest&quot;</span>, <span style="color: #666666;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></pre></td></tr></table></div>

<p>[UrlToTest] &#8212; 这个属性指定了运行该单元测试时的URL</p>
<p>[HostType] &#8212; 一般的单元测试是在VSTest的宿主进程下运行，所以是没有HttpContext的；如果作为ASP.NET单元测试，那么必须要在ASP.NET宿主进程下运行</p>
<p>[AspNetDevelopmentServerHost] &#8212; 由于我使用了ASP.NET Development Server 作为测试的主机服务器，而不是IIS；所以我需要设置这个属性来指定Web应用程序的完整路径。这里我需要把目录指向刚才新建的WEB项目的路径中。这样子就可以在单元测试中直接使用HttpContext.Current来获取一个HttpContext了。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>HostType<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ASP.NET&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>AspNetDevelopmentServerHost<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;D:<span style="color: #008080; font-weight: bold;">\\</span>MS_Code<span style="color: #008080; font-weight: bold;">\\</span>Public<span style="color: #008080; font-weight: bold;">\\</span>UrlRouter.WebTest&quot;</span>, <span style="color: #666666;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>UrlToTest<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://localhost:6988/Default.aspx&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Fuseaction_PUT<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    handler <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UrlMapHandler<span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Current</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">IsNotNull</span><span style="color: #000000;">&#40;</span>handler<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    result <span style="color: #008000;">=</span> handler.<span style="color: #0000FF;">ExecuteUrlMap</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    StringAssert.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>result.<span style="color: #0000FF;">Url</span>, <span style="color: #666666;">&quot;expected string&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>这样子初步解决了需要HttpContext的困难，但是会有以下问题：</p>
<ol>
<li>对HttpContext的内容不可控</li>
<li>需要运行ASP.NET Development Server</li>
<li>不能进行调试</li>
</ol>
<p><strong><span style="font-size: medium; color: #3366ff;">2. 直接创建HttpContext实例</span></strong></p>
<p>基于刚才提到的3个不足，我决定直接创建一个HttpContext来解决问题。还好，HttpContext有公开的构造函数，这个构造函数需要接受一个HttpWorkerRequest作为参数，HttpWorkerRequest是一个抽象类，就是不能直接对之实例化啦，不过好消息是，微软有一个简单的类“SimpleWorkerRequest”，这个类实现了HttpWorkerRequest类；下面是一段简单的代码</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">TextWriter tw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringWriter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
HttpWorkerRequest wr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SimpleWorkerRequest
<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;default.aspx&quot;</span>, <span style="color: #666666;">&quot;friendId=1300000000&quot;</span>, tw<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
HttpContext.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpContext<span style="color: #000000;">&#40;</span>wr<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>这样子就能在单元测试里面用到HttpContext了，好处有：</p>
<ol>
<li>可以控制HttpContext的内容了，对于URL重写这部分来说，会比较关心请求路径，还有<span class="parameter">queryString</span>，这两个可以在实例化SimpleWorkerRequest的时候，作为参数传递进去。</li>
<li>不需要ASP.NET Development Server。这个很重要，因为可以满足一个自包含(self-contained)的单元测试的要求</li>
<li>可以调试</li>
</ol>
<p><strong><span style="font-size: medium; color: #3366ff;">3.改良后的方案</span></strong></p>
<p>到此，世界很美好，测试进行的很顺利。好日子没过多久，我又遇到一个问题，这个URL重写模块，还应用了一个规则引擎，其中进行了大量对HOST判断的操作，很不幸，用刚才的方法创建的HttpContext，它返回的Request.Url中，HOST永远都是127.0.0.1，很郁闷。这时候，一个所有做.NET的人都需要的屠龙刀派上用场了！.NET Reflector，用Reflector打开System.Web.dll，找到System.Web.Hosting这个命名空间，然后找到SimpleWorkerRequest这个类，发现里面的GetLocalAddress()方法，发现里面就是Hard-Code了一个地址：127.0.0.1；好了，知道问题在哪里了，着手写一个新的类，这个类继承SimpleWorkerRequest，然后重写他的GetLocalAddress()方法：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyWorkerRequest <span style="color: #008000;">:</span> SimpleWorkerRequest
<span style="color: #000000;">&#123;</span> 
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> localAdd <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> MyWorkerRequest<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> page, <span style="color: #FF0000;">string</span> query, TextWriter output, <span style="color: #FF0000;">string</span> address<span style="color: #000000;">&#41;</span>
        <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>page, query, output<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>  
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">localAdd</span> <span style="color: #008000;">=</span> address<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> GetLocalAddress<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">localAdd</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>这样子，实例化HttpContext的时候就是这样子：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Thread.<span style="color: #0000FF;">GetDomain</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SetData</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;.appPath&quot;</span>, <span style="color: #666666;">&quot;c:<span style="color: #008080; font-weight: bold;">\\</span>inetpub<span style="color: #008080; font-weight: bold;">\\</span>wwwroot<span style="color: #008080; font-weight: bold;">\\</span>webapp<span style="color: #008080; font-weight: bold;">\\</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Thread.<span style="color: #0000FF;">GetDomain</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SetData</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;.appVPath&quot;</span>, <span style="color: #666666;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
TextWriter tw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringWriter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">String</span> address <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;home.myspace.cn&quot;</span><span style="color: #008000;">;</span>
HttpWorkerRequest wr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyWorkerRequest
<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;default.aspx&quot;</span>, <span style="color: #666666;">&quot;friendId=1300000000&quot;</span>, tw, address<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
HttpContext.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpContext<span style="color: #000000;">&#40;</span>wr<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>需要注意的是，对于.appPath和.appVPath的设置是必须的，因为在SimpleWorkerRequest的构造函数中，会取这两个数值。<br />
经过这样的改造，基本上已经满足了测试的需求了。</p>
<p><strong><span style="color: #3366ff;">总结一下：</span></strong></p>
<ol>
<li>这个事情居然用了1天多时间，后来发现解决的办法就在我订阅的博客里面就有，以后遇到什么难题先找一个Google Reader。</li>
<li>多用.NET Reflector，Visual Studio经常让你看meta data，那些meta data对我们来说一点用处都没有，还是Reflector好啊。</li>
</ol>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://magustest.com/blog/feed/" title="RSS"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F&amp;title=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95&amp;bodytext=%E5%9C%BA%E6%99%AF%EF%BC%9A%E6%9C%80%E8%BF%91%E5%9C%A8%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%AA.NET%E7%9A%84Http%20Module%EF%BC%8C%E8%BF%99%E4%B8%AAModule%E6%98%AF%E7%94%A8%E6%9D%A5%E5%81%9AURL%E9%87%8D%E5%86%99%E7%9A%84%E3%80%82%E5%88%9A%E5%BC%80%E5%A7%8B%E8%BF%9B%E5%B1%95%E7%9A%84%E6%AF%94%E8%BE%83%E9%A1%BA%E5%88%A9%EF%BC%8C%E5%9B%A0%E4%B8%BA%E8%AF%A5Module%E9%87%8C%E9%9D%A2%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%82%E6%95%B0%E5%9F%BA%E6%9C%AC%E4%B8%8A%E9%83%BD%E6%98%AFString%EF%BC%8C%E5%90%8E%E6%9D%A5%E8%BF%99%E4%B8%AAModule%E8%BF%9B%E8%A1%8C%E4%BA%86%E4%B8%80%E4%B8%8B%E9%87%8D%E6%9E%84%EF%BC%8C%E6%89%80%E6%9C%89%E5%8F%82%E6%95%B0%E9%83%BD%E5%8F%98%E6%88%90%E4%BA%86H" title="Digg"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F&amp;title=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95&amp;notes=%E5%9C%BA%E6%99%AF%EF%BC%9A%E6%9C%80%E8%BF%91%E5%9C%A8%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%AA.NET%E7%9A%84Http%20Module%EF%BC%8C%E8%BF%99%E4%B8%AAModule%E6%98%AF%E7%94%A8%E6%9D%A5%E5%81%9AURL%E9%87%8D%E5%86%99%E7%9A%84%E3%80%82%E5%88%9A%E5%BC%80%E5%A7%8B%E8%BF%9B%E5%B1%95%E7%9A%84%E6%AF%94%E8%BE%83%E9%A1%BA%E5%88%A9%EF%BC%8C%E5%9B%A0%E4%B8%BA%E8%AF%A5Module%E9%87%8C%E9%9D%A2%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%82%E6%95%B0%E5%9F%BA%E6%9C%AC%E4%B8%8A%E9%83%BD%E6%98%AFString%EF%BC%8C%E5%90%8E%E6%9D%A5%E8%BF%99%E4%B8%AAModule%E8%BF%9B%E8%A1%8C%E4%BA%86%E4%B8%80%E4%B8%8B%E9%87%8D%E6%9E%84%EF%BC%8C%E6%89%80%E6%9C%89%E5%8F%82%E6%95%B0%E9%83%BD%E5%8F%98%E6%88%90%E4%BA%86H" title="del.icio.us"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F&amp;t=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95" title="Facebook"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F&title=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95" title="豆瓣"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban.png" title="豆瓣" alt="豆瓣" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F&title=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95&n=1" title="豆瓣九点"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban9.png" title="豆瓣九点" alt="豆瓣九点" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95&amp;link=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F" title="FriendFeed"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F&amp;title=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95" title="Live"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://ping.fm/ref/?link=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F&amp;title=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95&amp;body=%E5%9C%BA%E6%99%AF%EF%BC%9A%E6%9C%80%E8%BF%91%E5%9C%A8%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%AA.NET%E7%9A%84Http%20Module%EF%BC%8C%E8%BF%99%E4%B8%AAModule%E6%98%AF%E7%94%A8%E6%9D%A5%E5%81%9AURL%E9%87%8D%E5%86%99%E7%9A%84%E3%80%82%E5%88%9A%E5%BC%80%E5%A7%8B%E8%BF%9B%E5%B1%95%E7%9A%84%E6%AF%94%E8%BE%83%E9%A1%BA%E5%88%A9%EF%BC%8C%E5%9B%A0%E4%B8%BA%E8%AF%A5Module%E9%87%8C%E9%9D%A2%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%82%E6%95%B0%E5%9F%BA%E6%9C%AC%E4%B8%8A%E9%83%BD%E6%98%AFString%EF%BC%8C%E5%90%8E%E6%9D%A5%E8%BF%99%E4%B8%AAModule%E8%BF%9B%E8%A1%8C%E4%BA%86%E4%B8%80%E4%B8%8B%E9%87%8D%E6%9E%84%EF%BC%8C%E6%89%80%E6%9C%89%E5%8F%82%E6%95%B0%E9%83%BD%E5%8F%98%E6%88%90%E4%BA%86H" title="Ping.fm"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://shuqian.qq.com/post?jumpback=1&title=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95&uri=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F" title="QQ书签"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/qq.png" title="QQ书签" alt="QQ书签" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=%E5%9C%A8C%23%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8HttpContext%E7%9A%84%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95%20-%20http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fusing-httpcontext-in-unittest%2F" title="Twitter"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>

<p>Related posts:<ol><li><a href='http://magustest.com/blog/net/introduction-to-dotnet-memcached-client/' rel='bookmark' title='Permanent Link: .NET平台上的Memcached客户端介绍'>.NET平台上的Memcached客户端介绍</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/three-different-test-fixture-setup-approach/' rel='bookmark' title='Permanent Link: 单元测试中三种准备Test Fixture的方法比较'>单元测试中三种准备Test Fixture的方法比较</a></li>
<li><a href='http://magustest.com/blog/net/serialize-deserialize-implement-deep-copy/' rel='bookmark' title='Permanent Link: 利用序列化和反序列化实现深拷贝'>利用序列化和反序列化实现深拷贝</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/softwaretesting/using-httpcontext-in-unittest/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
