﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-huangyi5209-文章分类-C#</title><link>http://www.cppblog.com/huangyi5209/category/16668.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 21 Apr 2011 16:12:36 GMT</lastBuildDate><pubDate>Thu, 21 Apr 2011 16:12:36 GMT</pubDate><ttl>60</ttl><item><title>Passing a JSON array to a WCF service 传递json数组到wcf service （自定义数据类型）</title><link>http://www.cppblog.com/huangyi5209/articles/144755.html</link><dc:creator>huangyi5209</dc:creator><author>huangyi5209</author><pubDate>Thu, 21 Apr 2011 14:14:00 GMT</pubDate><guid>http://www.cppblog.com/huangyi5209/articles/144755.html</guid><wfw:comment>http://www.cppblog.com/huangyi5209/comments/144755.html</wfw:comment><comments>http://www.cppblog.com/huangyi5209/articles/144755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/huangyi5209/comments/commentRss/144755.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huangyi5209/services/trackbacks/144755.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Sunday, October 10, 2010Passing a JSON array to a WCF service While the following information is available on the Internet, I had to piece it together from various sources, so for my own...&nbsp;&nbsp;<a href='http://www.cppblog.com/huangyi5209/articles/144755.html'>阅读全文</a><img src ="http://www.cppblog.com/huangyi5209/aggbug/144755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huangyi5209/" target="_blank">huangyi5209</a> 2011-04-21 22:14 <a href="http://www.cppblog.com/huangyi5209/articles/144755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C#中的Delegates与Event Handler</title><link>http://www.cppblog.com/huangyi5209/articles/144742.html</link><dc:creator>huangyi5209</dc:creator><author>huangyi5209</author><pubDate>Thu, 21 Apr 2011 11:47:00 GMT</pubDate><guid>http://www.cppblog.com/huangyi5209/articles/144742.html</guid><wfw:comment>http://www.cppblog.com/huangyi5209/comments/144742.html</wfw:comment><comments>http://www.cppblog.com/huangyi5209/articles/144742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/huangyi5209/comments/commentRss/144742.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huangyi5209/services/trackbacks/144742.html</trackback:ping><description><![CDATA[<wbr>&nbsp;
<div></div>
<div id=entrybody>
<div id=entryadbox></div>
<span id=google_text></span><span id=span_google_text></span><br>
<div style="PADDING-BOTTOM: 8px; MARGIN: 0px 5px; PADDING-LEFT: 3px; PADDING-RIGHT: 3px; PADDING-TOP: 0px">
<div style="WIDTH: auto; HEIGHT: auto" id=entryad><span id=google3_text></span><span id=span_google3_text></span></div>
<style> .Six685 { display:none; } </style>
<font face=Tahoma><span style="FONT-WEIGHT: bold">1. 使用Delegates进行事件处理，一般遵循下面的模式：</span><br><br>我们将事件的发起者(Event Source)称为Publisher，将事件的处理者(Event Handler)称为Subscriber.<br><img title="C中的Delegates与Event Handler - huangyi5209 - huangyi5209的博客" border=1 alt="" src="http://www.webasp.net/article/uploadfile/200612/2006122214959786.jpg" ilo-full-src="../uploadfile/200612/2006122214959786.jpg"><br style="FONT-FAMILY: Tahoma"><br style="FONT-WEIGHT: bold"><span style="FONT-WEIGHT: bold">2. 具体的做法，用一个简单的例子来看如何进行事件的封装处理！</span><br><br></font>
<table border=1 cellSpacing=1 summary="" cellPadding=1 width=780>
    <tbody>
        <tr>
            <td><span style="FONT-FAMILY: Tahoma; COLOR: rgb(0,0,128); FONT-SIZE: 9pt"><br>鲨鱼正在网上潜水，突然他决定出门(Event)，他会有什么动作呢(Event Handler)？<br>&nbsp; ---&gt; 决定出门了....（一个外部调用...sharkMind.DecideGetOut()）<br>&nbsp;&nbsp;&nbsp;&nbsp; ---&gt; 鲨鱼的脑子产生一些生物电... （一个事件激发了... OnGetOut）<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ---&gt; 谁来做处理，丢给神经系统吧...（一个delegate要负责传递指令....GetOutEventHandler）
            <div></div>
            <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ---&gt; 具体如何做，当然是鲨鱼的身体啦...（sharkBody.OpenDoor()）<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ---&gt; 一个事件可能需要好几个动作处理哦，所以我需要：开门，放下东西，关门！<br><br></span></td>
        </tr>
    </tbody>
</table>
<br>2.1 纵观一下整个事件的处理过程...<br>
<table border=1 cellSpacing=1 summary="" cellPadding=1 width=780>
    <tbody>
        <tr style="FONT-WEIGHT: bold">
            <td bgColor=#cccccc>filename: Program.cs</td>
        </tr>
        <tr>
            <td>
            <pre style="FONT-FAMILY: courier new; FONT-SIZE: 9pt"><span style="COLOR: rgb(0,0,0)"><br>  </span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(0,0,0)"> Program {<br>    </span><span style="COLOR: rgb(0,0,255)">static</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> Main(</span><span style="COLOR: rgb(0,0,255)">string</span><span style="COLOR: rgb(0,0,0)">[] args) { <span></span> <br> </span><span style="COLOR: rgb(0,128,0)">     </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">鲨鱼的脑袋，算是一个事件发起者，the Publisher. </span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">      GetOutPublisher sharkMind </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> GetOutPublisher();<br><br>      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">鲨鱼的身体，算是一个事件处理者，the Subscriber.</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">      GetOutSubscriber sharkBody </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> GetOutSubscriber();<br><br>      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">建立鲨鱼的神经系统单元</span><span style="COLOR: rgb(0,128,0)">
            <p>软件开发网 www.mscto.com</p>
            <br></span><span style="COLOR: rgb(0,0,0)">      GetOutEventHandler eventAction1 </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> GetOutEventHandler(sharkBody.OpenTheDoor);<br>      GetOutEventHandler eventAction2 </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> GetOutEventHandler(sharkBody.PutDownWhat);<br>      GetOutEventHandler eventAction3 </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> GetOutEventHandler(sharkBody.CloseTheDoor);<br><br>      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">把鲨鱼的神经系统单元与脑子联系起来, 即为将事件处理动作注册到事件委托中.</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">      sharkMind.OnGetOutHandler </span><span style="COLOR: rgb(0,0,0)">+=</span><span style="COLOR: rgb(0,0,0)"> eventAction1; <span></span> <br>      sharkMind.OnGetOutHandler </span><span style="COLOR: rgb(0,0,0)">+=</span><span style="COLOR: rgb(0,0,0)"> eventAction2;<br>      sharkMind.OnGetOutHandler </span><span style="COLOR: rgb(0,0,0)">+=</span><span style="COLOR: rgb(0,0,0)"> eventAction3;<br><br>      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">外面天气不错，我决定出去走走，于是产生一系列的事件处理！</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">      sharkMind.DecideGetOut();<br></span><span style="COLOR: rgb(0,0,0)">    }<br>  }</span><font color=#363636>  </font></pre>
            </td>
        </tr>
    </tbody>
</table>
<br>2.2 剖析整个组成...先从delegate开始，为了能够封装事件，我们需要先定义一个delegate：<br>
<pre style="FONT-FAMILY: courier new; FONT-SIZE: 9pt"><span style="COLOR: rgb(0,0,255)">   delegate</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> GetOutEventHandler(Object source, GetOutEventArgs e);</span>
<p>&#160;</p>
</pre>
<span style="FONT-FAMILY: Tahoma">2.3 假定你的事件处理的时候，还需要传递一些参数，所以我们还需定义一个EventArgs，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 就算出门的时候带什么东西吧~~~ ^ ^!<br>
<table border=1 cellSpacing=1 summary="" cellPadding=1 width=780>
    <tbody>
        <tr style="FONT-FAMILY: Tahoma; FONT-WEIGHT: bold">
            <td bgColor=#cccccc>filename：GetOutEventArgs.cs</td>
        </tr>
        <tr>
            <td>
            <pre style="FONT-FAMILY: courier new; FONT-SIZE: 9pt"><span style="COLOR: rgb(0,0,0)"><br>  </span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(0,0,0)"> GetOutEventArgs : System.EventArgs {<br>    </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> 出门带啥东西呢?</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">    </span><span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">string</span><span style="COLOR: rgb(0,0,0)"> _takeWhat;
            <p>&#160;</p>
            <br><br>    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> GetOutEventArgs() {<br>      </span><span style="COLOR: rgb(0,0,255)">this</span><span style="COLOR: rgb(0,0,0)">._takeWhat </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Nothing</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">;<br>    }<br><br>    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> GetOutEventArgs(</span><span style="COLOR: rgb(0,0,255)">string</span><span style="COLOR: rgb(0,0,0)"> strTakeWhat) {<br>      </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)"> (strTakeWhat.Equals(</span><span style="COLOR: rgb(0,0,0)">""</span><span style="COLOR: rgb(0,0,0)">)) {<br>        </span><span style="COLOR: rgb(0,0,255)">this</span><span style="COLOR: rgb(0,0,0)">._takeWhat </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Nothing</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">;
            <p>&#160;</p>
            <br>      } </span><span style="COLOR: rgb(0,0,255)">else</span><span style="COLOR: rgb(0,0,0)"> {<br>        </span><span style="COLOR: rgb(0,0,255)">this</span><span style="COLOR: rgb(0,0,0)">._takeWhat </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> strTakeWhat;<br>      }<br>    }<br><br>    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">string</span><span style="COLOR: rgb(0,0,0)"> TakeWhat {<br>      </span><span style="COLOR: rgb(0,0,255)">get</span><span style="COLOR: rgb(0,0,0)"> {<br>        </span><span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,0)"> _takeWhat;<br>      }<br>      </span><span style="COLOR: rgb(0,0,255)">set</span><span style="COLOR: rgb(0,0,0)"> {<br>        _takeWhat </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> value;<br>      }<br>    }<br>  }</span><font color=#363636> </font>
            <p>&#160;</p>
            </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>2.4 对应出去，我们就局限下面的响应动作了：<br>
<table border=1 cellSpacing=1 summary="" cellPadding=1 width=780>
    <tbody>
        <tr style="FONT-FAMILY: Tahoma; FONT-WEIGHT: bold">
            <td bgColor=#cccccc>filename：GetOutSubscribe.cs</td>
        </tr>
        <tr>
            <td>
            <pre style="FONT-FAMILY: courier new; FONT-SIZE: 9pt">
            <div><span style="COLOR: rgb(0,0,0)"><br>  </span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(0,0,0)"> GetOutSubscriber {<br>    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> OpenTheDoor(Object source, GetOutEventArgs e) {<br>      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> 开门...</span><span style="COLOR: rgb(0,128,0)"> <font color=#ffffff></font> <br></span><span style="COLOR: rgb(0,0,0)">      Console.WriteLine(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Open the door...</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br>    }<br><br>    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> PutDownWhat(Object source, GetOutEventArgs e) {<br>      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> 放下手中的东西...</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">      Console.WriteLine(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Put down </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)"> e.TakeWhat + "...");<br>    }<br>
            <p>&#160;</p>
            <br>    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> CloseTheDoor(Object source, GetOutEventArgs e) {<br>      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> 关门...</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">      Console.WriteLine(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Close the door...</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br>    }<br>  }</span></div>
            <span></span> </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>2.5 事件是如何产生的<br>
<table border=1 cellSpacing=1 summary="" cellPadding=1 width=780>
    <tbody>
        <tr>
            <td bgColor=#cccccc><span style="FONT-FAMILY: Tahoma; COLOR: rgb(0,0,0); FONT-WEIGHT: bold">filename：GetOutPublisher.cs</span></td>
        </tr>
        <tr>
            <td>
            <pre style="FONT-FAMILY: courier new; FONT-SIZE: 9pt">
            <div><span style="COLOR: rgb(0,0,0)"><br>  </span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(0,0,0)"> GetOutPublisher {<br>    </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">Publisher 说：喂，我有一个事件，谁来处理？</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">event</span><span style="COLOR: rgb(0,0,0)"> GetOutEventHandler OnGetOutHandler; <span></span> <br><br>    </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">Publisher 产生具体的事件</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">    </span><span style="COLOR: rgb(0,0,255)">protected</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> OnGetOut(GetOutEventArgs e) {<br>      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">有谁来负责处理事件吗?</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">      </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)"> (</span><span style="COLOR: rgb(0,0,255)">null</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">!=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">this</span><span style="COLOR: rgb(0,0,0)">.OnGetOutHandler) {<br>        </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">让他们来处理吧...</span><span style="COLOR: rgb(0,128,0)"> <font color=#ffffff></font> <br></span><span style="COLOR: rgb(0,0,0)">        OnGetOutHandler(</span><span style="COLOR: rgb(0,0,255)">this</span><span style="COLOR: rgb(0,0,0)">, e);<br>      }<br>    }<br><br>    </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">对外公开的接口，调用此方法会产生事件.</span><span style="COLOR: rgb(0,128,0)"><br></span><span style="COLOR: rgb(0,0,0)">    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> DecideGetOut() {<br>      OnGetOut(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> GetOutEventArgs());<br>    }<br><br>  }</span></div>
            <span></span> </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>2.6 当有了上面的Publisher与Subscriber后，要把它们串起来，需要遵循下面的过程：<br><br>&nbsp;&nbsp; Step 1：建立Publisher Instance，即Event Source<br>&nbsp;&nbsp; Step 2：建立Subscriber Instance，即Event Handler<br>&nbsp;&nbsp; Step 3：建立一个Delegate，来承接Subscriber的具体动作<br>&nbsp;&nbsp; Step 4：把建立的Delegate与Event进行绑定，我们称之为register<br>&nbsp;&nbsp; Step 5：外部调用，产生Event，所有的Event Handler收到通知，开始执行<br><br>&nbsp;&nbsp; <span style="FONT-WEIGHT: bold">具体可以参考2.1中的主函数的步骤!<br></span><br>2.7 运行的结果如下:<br>
<table style="COLOR: rgb(192,192,192)" border=1 cellSpacing=1 summary="" cellPadding=1 width=780>
    <tbody>
        <tr>
            <td style="FONT-FAMILY: Tahoma; FONT-SIZE: 9pt" bgColor=#000000><br>Open the door...<br>Put down Nothing...<br>Close the door...<br><br></td>
        </tr>
    </tbody>
</table>
</span></div>
<br></div>
<img src ="http://www.cppblog.com/huangyi5209/aggbug/144742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huangyi5209/" target="_blank">huangyi5209</a> 2011-04-21 19:47 <a href="http://www.cppblog.com/huangyi5209/articles/144742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>