<?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>Jennal's Blog &#187; C#</title>
	<atom:link href="http://jennal.cn/category/net/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://jennal.cn</link>
	<description>Jennal的技术博客</description>
	<lastBuildDate>Thu, 05 Jan 2012 14:02:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>王翔的《设计模式》读书笔记</title>
		<link>http://jennal.cn/2009/11/18/%e7%8e%8b%e7%bf%94%e7%9a%84%e3%80%8a%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e3%80%8b%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0/</link>
		<comments>http://jennal.cn/2009/11/18/%e7%8e%8b%e7%bf%94%e7%9a%84%e3%80%8a%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e3%80%8b%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 04:37:41 +0000</pubDate>
		<dc:creator>Jennal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[依赖注入]]></category>
		<category><![CDATA[设计模式]]></category>
		<category><![CDATA[读书笔记]]></category>

		<guid isPermaLink="false">http://jennal.cn/2009/11/18/%e7%8e%8b%e7%bf%94%e7%9a%84%e3%80%8a%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e3%80%8b%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0/</guid>
		<description><![CDATA[今天读王翔的《设计模式 &#8211; 基于C#的工程化实现及扩展》，刚把第一章读完。作为一个C#初学者和工作者，发现了55页，也就是第一章的最后一段关于依赖注入的实例代码中有一个很明显的错误。本来想直接联系作者的，可是实在找不到他的联系方式，就发在自己博客上了。如果同样读过这本书的人能看到，不胜荣幸。
代码如下：

&#91;AttributeUsage&#40;AttributeTargets.Class&#41;, AllowMultiple=true&#93;
sealed class DecoratorAttribute : Attribute
&#123;
    public readonly object Injector;
    private Type type;
&#160;
    public DecoratorAttribute&#40;Type type&#41;
    &#123;
        if &#40;type == null&#41; throw new ArgumentNullException&#40;&#34;type&#34;&#41;;
        this.type = type;
 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F07%2F09%2F%25E6%25B5%25AA%25E6%25BD%25AE%25E4%25B9%258B%25E5%25B7%2585%25E7%25AC%25AC%25E5%258D%2581%25E5%2585%25AD%25E7%25AB%25A0-%25E5%258D%25B0%25E9%2592%259E%25E6%259C%25BA%25E2%2580%2594%25E2%2580%2594%25E6%259C%2580%25E4%25BD%25B3%25E7%259A%2584%25E5%2595%2586%25E4%25B8%259A%25E6%25A8%25A1%25E5%25BC%258F%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F11%2F18%2F%25E7%258E%258B%25E7%25BF%2594%25E7%259A%2584%25E3%2580%258A%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F%25E3%2580%258B%25E8%25AF%25BB%25E4%25B9%25A6%25E7%25AC%2594%25E8%25AE%25B0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">浪潮之巅第十六章 印钞机——最佳的商业模式</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F07%2F25%2Fweb-designer-developer-should-know-15-sites%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F11%2F18%2F%25E7%258E%258B%25E7%25BF%2594%25E7%259A%2584%25E3%2580%258A%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F%25E3%2580%258B%25E8%25AF%25BB%25E4%25B9%25A6%25E7%25AC%2594%25E8%25AE%25B0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">[转]Web 设计与开发者必须知道的 15 个站点</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2010%2F01%2F30%2F%25E3%2580%258A%25E5%258A%259F%25E5%25A4%25AB%25E4%25B9%258B%25E7%258E%258B%25E3%2580%258B%25E8%25A7%2582%25E5%2590%258E%25E6%2584%259F%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F11%2F18%2F%25E7%258E%258B%25E7%25BF%2594%25E7%259A%2584%25E3%2580%258A%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F%25E3%2580%258B%25E8%25AF%25BB%25E4%25B9%25A6%25E7%25AC%2594%25E8%25AE%25B0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">《功夫之王》观后感</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2008%2F10%2F02%2Fjavascript%25E5%25AD%25A6%25E4%25B9%25A0%25E7%25AC%2594%25E8%25AE%25B0-%25E7%25B1%25BB%25E7%259A%2584%25E7%25BB%25A7%25E6%2589%25BF%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F11%2F18%2F%25E7%258E%258B%25E7%25BF%2594%25E7%259A%2584%25E3%2580%258A%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F%25E3%2580%258B%25E8%25AF%25BB%25E4%25B9%25A6%25E7%25AC%2594%25E8%25AE%25B0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Javascript学习笔记–类的继承</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>今天读王翔的《设计模式 &#8211; 基于C#的工程化实现及扩展》，刚把第一章读完。作为一个C#初学者和工作者，发现了55页，也就是第一章的最后一段关于依赖注入的实例代码中有一个很明显的错误。本来想直接联系作者的，可是实在找不到他的联系方式，就发在自己博客上了。如果同样读过这本书的人能看到，不胜荣幸。</p>
<p>代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>AttributeUsage<span style="color: #009900;">&#40;</span>AttributeTargets.<span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#41;</span>, AllowMultiple<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#93;</span>
sealed <span style="color: #000000; font-weight: bold;">class</span> DecoratorAttribute <span style="color: #339933;">:</span> <span style="color: #003399;">Attribute</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> readonly object Injector<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Type type<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> DecoratorAttribute<span style="color: #009900;">&#40;</span>Type type<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>type <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ArgumentNullException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;type&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">type</span> <span style="color: #339933;">=</span> type<span style="color: #339933;">;</span>
        Injector <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Assembler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">Create</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Type Type <span style="color: #009900;">&#123;</span> get <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">type</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> AttributeHelper
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> T Injector<span style="color: #009900;">&#40;</span>object target<span style="color: #009900;">&#41;</span>
        where T <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">class</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>target <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ArgumentNullException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;target&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Type targetType <span style="color: #339933;">=</span> target.<span style="color: #006633;">GetType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        object<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> attributes <span style="color: #339933;">=</span> targetType.<span style="color: #006633;">GetCustomAttributes</span><span style="color: #009900;">&#40;</span>
            typeof<span style="color: #009900;">&#40;</span>DecoratorAttribute<span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>attributes <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>attributes.<span style="color: #006633;">Length</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">foreach</span> <span style="color: #009900;">&#40;</span>DecoratorAttribute attribute in
            <span style="color: #009900;">&#40;</span>DecoratorAttribute <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>attributes<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>attribute.<span style="color: #006633;">Type</span> <span style="color: #339933;">==</span> typeof<span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span>attribute.<span style="color: #006633;">Injector</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#91;</span>Decorator<span style="color: #009900;">&#40;</span>typeof<span style="color: #009900;">&#40;</span>ITimeProvider<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span>
<span style="color: #000000; font-weight: bold;">class</span> Client
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> GetYear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        ITimeProvider provider <span style="color: #339933;">=</span>
            AttributeHelper.<span style="color: #006633;">Injector</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> provider.<span style="color: #006633;">CurrentDate</span>.<span style="color: #006633;">Year</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>这段代码是依赖注入。使用Attribute把外部对接口的实现注入到类中。通过修改Attribute来改变依赖。<br />
其中第29行和第35行中，有一个明显的错误。<br />
先从第35行说起</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>Decorator<span style="color: #009900;">&#40;</span>typeof<span style="color: #009900;">&#40;</span>ITimeProvider<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span></pre></div></div>

<p>这行代码说明要注入ITimeProvider类型，但是我们知道接口是不能被直接实例化的。所以这里的ITimeProvider应该改成TimeProvider。<br />
但是这个时候第35行的条件判断就出现问题了，因为我们要得到的是ITimeProvider，而Decorator给的参数是TimeProvider，这两个类型是不会相等的。<br />
这个时候我们还必须修改第35行的代码为</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>attribute.<span style="color: #006633;">Type</span>.<span style="color: #006633;">GetInterfaces</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">Contains</span><span style="color: #009900;">&#40;</span>typeof<span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>通过寻找实现接口的实例，才能够真正找到我们要的Decorator。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F07%2F09%2F%25E6%25B5%25AA%25E6%25BD%25AE%25E4%25B9%258B%25E5%25B7%2585%25E7%25AC%25AC%25E5%258D%2581%25E5%2585%25AD%25E7%25AB%25A0-%25E5%258D%25B0%25E9%2592%259E%25E6%259C%25BA%25E2%2580%2594%25E2%2580%2594%25E6%259C%2580%25E4%25BD%25B3%25E7%259A%2584%25E5%2595%2586%25E4%25B8%259A%25E6%25A8%25A1%25E5%25BC%258F%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F11%2F18%2F%25E7%258E%258B%25E7%25BF%2594%25E7%259A%2584%25E3%2580%258A%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F%25E3%2580%258B%25E8%25AF%25BB%25E4%25B9%25A6%25E7%25AC%2594%25E8%25AE%25B0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">浪潮之巅第十六章 印钞机——最佳的商业模式</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F07%2F25%2Fweb-designer-developer-should-know-15-sites%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F11%2F18%2F%25E7%258E%258B%25E7%25BF%2594%25E7%259A%2584%25E3%2580%258A%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F%25E3%2580%258B%25E8%25AF%25BB%25E4%25B9%25A6%25E7%25AC%2594%25E8%25AE%25B0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">[转]Web 设计与开发者必须知道的 15 个站点</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2010%2F01%2F30%2F%25E3%2580%258A%25E5%258A%259F%25E5%25A4%25AB%25E4%25B9%258B%25E7%258E%258B%25E3%2580%258B%25E8%25A7%2582%25E5%2590%258E%25E6%2584%259F%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F11%2F18%2F%25E7%258E%258B%25E7%25BF%2594%25E7%259A%2584%25E3%2580%258A%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F%25E3%2580%258B%25E8%25AF%25BB%25E4%25B9%25A6%25E7%25AC%2594%25E8%25AE%25B0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">《功夫之王》观后感</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2008%2F10%2F02%2Fjavascript%25E5%25AD%25A6%25E4%25B9%25A0%25E7%25AC%2594%25E8%25AE%25B0-%25E7%25B1%25BB%25E7%259A%2584%25E7%25BB%25A7%25E6%2589%25BF%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F11%2F18%2F%25E7%258E%258B%25E7%25BF%2594%25E7%259A%2584%25E3%2580%258A%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F%25E3%2580%258B%25E8%25AF%25BB%25E4%25B9%25A6%25E7%25AC%2594%25E8%25AE%25B0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Javascript学习笔记–类的继承</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://jennal.cn/2009/11/18/%e7%8e%8b%e7%bf%94%e7%9a%84%e3%80%8a%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e3%80%8b%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>关于C#反射很棒的文章</title>
		<link>http://jennal.cn/2009/09/28/%e5%85%b3%e4%ba%8ec%e5%8f%8d%e5%b0%84%e5%be%88%e6%a3%92%e7%9a%84%e6%96%87%e7%ab%a0/</link>
		<comments>http://jennal.cn/2009/09/28/%e5%85%b3%e4%ba%8ec%e5%8f%8d%e5%b0%84%e5%be%88%e6%a3%92%e7%9a%84%e6%96%87%e7%ab%a0/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 01:18:28 +0000</pubDate>
		<dc:creator>Jennal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[reflect]]></category>

		<guid isPermaLink="false">http://jennal.cn/?p=241</guid>
		<description><![CDATA[中文翻译：http://www.infoq.com/cn/articles/expression-compiler
英文原文：http://www.infoq.com/articles/expression-compiler
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#动态加载DLL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F08%2F26%2Fc%25E8%25AA%259E%25E8%25A8%2580%25E6%2595%2599%25E7%25A8%258B-%25EF%25BC%2588%25E4%25B8%2580%25EF%25BC%2589%25E7%2592%25B0%25E5%25A2%2583%25E9%2585%258D%25E7%25BD%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C語言教程 — （一）環境配置</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#的疑问</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一个C#睡前故事</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>中文翻译：<a href="http://www.infoq.com/cn/articles/expression-compiler">http://www.infoq.com/cn/articles/expression-compiler</a></p>
<p>英文原文：<a href="http://www.infoq.com/articles/expression-compiler">http://www.infoq.com/articles/expression-compiler</a></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#动态加载DLL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F08%2F26%2Fc%25E8%25AA%259E%25E8%25A8%2580%25E6%2595%2599%25E7%25A8%258B-%25EF%25BC%2588%25E4%25B8%2580%25EF%25BC%2589%25E7%2592%25B0%25E5%25A2%2583%25E9%2585%258D%25E7%25BD%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C語言教程 — （一）環境配置</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#的疑问</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一个C#睡前故事</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://jennal.cn/2009/09/28/%e5%85%b3%e4%ba%8ec%e5%8f%8d%e5%b0%84%e5%be%88%e6%a3%92%e7%9a%84%e6%96%87%e7%ab%a0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#动态加载DLL</title>
		<link>http://jennal.cn/2009/09/24/c%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bddll/</link>
		<comments>http://jennal.cn/2009/09/24/c%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bddll/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 09:20:33 +0000</pubDate>
		<dc:creator>Jennal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[reflect]]></category>
		<category><![CDATA[动态加载]]></category>

		<guid isPermaLink="false">http://jennal.cn/?p=239</guid>
		<description><![CDATA[利用反射进行动态加载和调用.
Assembly ass=Assembly.LoadFrom(DllPath); //利用dll的路径加载,同时将此程序集所依赖的程序集加载进来,需后辍名.dll
Assembly.LoadFile 只加载指定文件，并不会自动加载依赖程序集.Assmbly.Load无需后辍名
加载dll后,需要使用dll中某类.
Type type=ass.GetType(“TypeName”);//利用类型的命名空间和名称获得类型
需要实例化类型,才可以使用,参数可以人为的指定,也可以无参数,静态实例可以省略
Object obj = Activator.CreateInstance(type,params[]);//利用指定的参数实例话类型
调用类型中的某个方法:
需要首先得到此方法
MethodInfo mi=type.GetMethod(“MehtodName”);//通过方法名称获得方法
然后对方法进行调用,多态性利用参数进行控制
mi.Invoke(obj,params[]);//根据参数直线方法,返回值就是原方法的返回值
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">关于C#反射很棒的文章</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F08%2F26%2Fc%25E8%25AA%259E%25E8%25A8%2580%25E6%2595%2599%25E7%25A8%258B-%25EF%25BC%2588%25E4%25B8%2580%25EF%25BC%2589%25E7%2592%25B0%25E5%25A2%2583%25E9%2585%258D%25E7%25BD%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C語言教程 — （一）環境配置</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一个C#睡前故事</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2008%2F10%2F03%2Fnet-making-gains-against-java-survey-says%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">.NET Making Gains Against Java, Survey Says</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>利用反射进行动态加载和调用.<br />
Assembly ass=Assembly.LoadFrom(DllPath); //利用dll的路径加载,同时将此程序集所依赖的程序集加载进来,需后辍名.dll<br />
Assembly.LoadFile 只加载指定文件，并不会自动加载依赖程序集.Assmbly.Load无需后辍名</p>
<p>加载dll后,需要使用dll中某类.<br />
Type type=ass.GetType(“TypeName”);//利用类型的命名空间和名称获得类型</p>
<p>需要实例化类型,才可以使用,参数可以人为的指定,也可以无参数,静态实例可以省略<br />
Object obj = Activator.CreateInstance(type,params[]);//利用指定的参数实例话类型</p>
<p>调用类型中的某个方法:<br />
需要首先得到此方法<br />
MethodInfo mi=type.GetMethod(“MehtodName”);//通过方法名称获得方法</p>
<p>然后对方法进行调用,多态性利用参数进行控制<br />
mi.Invoke(obj,params[]);//根据参数直线方法,返回值就是原方法的返回值</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">关于C#反射很棒的文章</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F08%2F26%2Fc%25E8%25AA%259E%25E8%25A8%2580%25E6%2595%2599%25E7%25A8%258B-%25EF%25BC%2588%25E4%25B8%2580%25EF%25BC%2589%25E7%2592%25B0%25E5%25A2%2583%25E9%2585%258D%25E7%25BD%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C語言教程 — （一）環境配置</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一个C#睡前故事</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2008%2F10%2F03%2Fnet-making-gains-against-java-survey-says%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">.NET Making Gains Against Java, Survey Says</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://jennal.cn/2009/09/24/c%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bddll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#的疑问</title>
		<link>http://jennal.cn/2009/04/02/c%e7%9a%84%e7%96%91%e9%97%ae/</link>
		<comments>http://jennal.cn/2009/04/02/c%e7%9a%84%e7%96%91%e9%97%ae/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 01:36:05 +0000</pubDate>
		<dc:creator>Jennal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#的疑问]]></category>

		<guid isPermaLink="false">http://jennal.cn/?p=136</guid>
		<description><![CDATA[
1
2
3
4
5
6
class User&#123;
    public IPAddress IP;
&#125;
User user = null;
if&#40; user == null &#124;&#124; user.IP == null &#41; //这句会发生莫名的错误，程序直接退出，是C#的问题吗？
&#123;&#125;

<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F08%2F26%2Fc%25E8%25AA%259E%25E8%25A8%2580%25E6%2595%2599%25E7%25A8%258B-%25EF%25BC%2588%25E4%25B8%2580%25EF%25BC%2589%25E7%2592%25B0%25E5%25A2%2583%25E9%2585%258D%25E7%25BD%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C語言教程 — （一）環境配置</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">关于C#反射很棒的文章</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#动态加载DLL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一个C#睡前故事</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> User<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span> IPAddress IP<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
User user <span style="color: #000080;">=</span> null<span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> user <span style="color: #000080;">==</span> null <span style="color: #000040;">||</span> user.<span style="color: #007788;">IP</span> <span style="color: #000080;">==</span> null <span style="color: #008000;">&#41;</span> <span style="color: #666666;">//这句会发生莫名的错误，程序直接退出，是C#的问题吗？</span>
<span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F08%2F26%2Fc%25E8%25AA%259E%25E8%25A8%2580%25E6%2595%2599%25E7%25A8%258B-%25EF%25BC%2588%25E4%25B8%2580%25EF%25BC%2589%25E7%2592%25B0%25E5%25A2%2583%25E9%2585%258D%25E7%25BD%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C語言教程 — （一）環境配置</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F28%2F%25E5%2585%25B3%25E4%25BA%258Ec%25E5%258F%258D%25E5%25B0%2584%25E5%25BE%2588%25E6%25A3%2592%25E7%259A%2584%25E6%2596%2587%25E7%25AB%25A0%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">关于C#反射很棒的文章</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#动态加载DLL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">一个C#睡前故事</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://jennal.cn/2009/04/02/c%e7%9a%84%e7%96%91%e9%97%ae/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>一个C#睡前故事</title>
		<link>http://jennal.cn/2009/03/23/%e4%b8%80%e4%b8%aac%e7%9d%a1%e5%89%8d%e6%95%85%e4%ba%8b/</link>
		<comments>http://jennal.cn/2009/03/23/%e4%b8%80%e4%b8%aac%e7%9d%a1%e5%89%8d%e6%95%85%e4%ba%8b/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 12:01:31 +0000</pubDate>
		<dc:creator>Jennal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[回调]]></category>
		<category><![CDATA[异步]]></category>
		<category><![CDATA[线程]]></category>

		<guid isPermaLink="false">http://jennal.cn/?p=129</guid>
		<description><![CDATA[从 前，在南方一块奇异的土地上，有个工人名叫彼得，他非常勤奋，对他的老板总是百依百顺。但是他的老板是个吝啬的人，从不信任别人，坚决要求随时知道彼得的 工作进度，以防止他偷懒。但是彼得又不想让老板呆在他的办公室里站在背后盯着他，于是就对老板做出承诺：无论何时，只要我的工作取得了一点进展我都会及时 让你知道。彼得通过周期性地使用“带类型的引用”(原文为：“typed reference” 也就是delegate？？)“回调”他的老板来实现他的承诺，如下： 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Worker &#123;
	public void Advise&#40;Boss boss&#41; &#123; _boss = boss; &#125;
	public void DoWork&#40;&#41; &#123;
		Console.WriteLine&#40;“工作: 工作开始”&#41;;
		if&#40; _boss != null &#41; _boss.WorkStarted&#40;&#41;;
&#160;
		Console.WriteLine&#40;“工作: 工作进行中”&#41;;
		if&#40; _boss != null &#41; _boss.WorkProgressing&#40;&#41;;
&#160;
		Console.WriteLine&#40;&#34;工作: 工作完成&#34;&#41;;
		if&#40; _boss != null &#41; &#123;
			int grade = _boss.WorkCompleted&#40;&#41;;
			Console.WriteLine&#40;“工人的工作得分＝” + grade&#41;;
		&#125;
	&#125;
	private Boss _boss;
&#125;
&#160;
class Boss &#123;
	public void WorkStarted&#40;&#41; &#123; /* 老板不关心。 */ &#125;
	public void [...]<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#动态加载DLL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2008%2F10%2F03%2Fnet-making-gains-against-java-survey-says%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">.NET Making Gains Against Java, Survey Says</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F08%2F26%2Fc%25E8%25AA%259E%25E8%25A8%2580%25E6%2595%2599%25E7%25A8%258B-%25EF%25BC%2588%25E4%25B8%2580%25EF%25BC%2589%25E7%2592%25B0%25E5%25A2%2583%25E9%2585%258D%25E7%25BD%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C語言教程 — （一）環境配置</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#的疑问</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">从 前，在南方一块奇异的土地上，有个工人名叫彼得，他非常勤奋，对他的老板总是百依百顺。但是他的老板是个吝啬的人，从不信任别人，坚决要求随时知道彼得的 工作进度，以防止他偷懒。但是彼得又不想让老板呆在他的办公室里站在背后盯着他，于是就对老板做出承诺：无论何时，只要我的工作取得了一点进展我都会及时 让你知道。彼得通过周期性地使用“带类型的引用”(原文为：“typed reference” 也就是delegate？？)“回调”他的老板来实现他的承诺，如下： </p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Worker <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Advise<span style="color: #008000;">&#40;</span>Boss boss<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> _boss <span style="color: #000080;">=</span> boss<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工作<span style="color: #008080;">:</span> 工作开始”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> _boss <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> _boss.<span style="color: #007788;">WorkStarted</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工作<span style="color: #008080;">:</span> 工作进行中”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> _boss <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> _boss.<span style="color: #007788;">WorkProgressing</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;工作: 工作完成&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> _boss <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">int</span> grade <span style="color: #000080;">=</span> _boss.<span style="color: #007788;">WorkCompleted</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工人的工作得分＝” <span style="color: #000040;">+</span> grade<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">private</span> Boss _boss<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Boss <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> WorkStarted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* 老板不关心。 */</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> WorkProgressing<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/*老板不关心。 */</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> WorkCompleted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“时间差不多！”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* 总分为10 */</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Universe <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Worker  peter <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Worker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Boss        boss <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Boss<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		peter.<span style="color: #007788;">Advise</span><span style="color: #008000;">&#40;</span>boss<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		peter.<span style="color: #007788;">DoWork</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“Main<span style="color: #008080;">:</span> 工人工作完成”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Console.<span style="color: #007788;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong>接口</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 现在，彼得成了一个特殊的人，他不但能容忍吝啬的老板，而且和他周围的宇宙也有了密切的联系，以至于他认为宇宙对他的工作进度也感兴趣。不幸的是，他必须也给宇宙添加一个特殊的回调函数Advise来实现同时向他老板和宇宙报告工作进度。彼得想要把潜在的通知的列表和这些通知的实现方法分离开来，于是他决定把方法分离为一个接口：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">interface IWorkerEvents <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">void</span> WorkStarted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> WorkProgressing<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> WorkCompleted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Worker <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Advise<span style="color: #008000;">&#40;</span>IWorkerEvents events<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> _events <span style="color: #000080;">=</span> events<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;工作工作开始&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> _events <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> _events.<span style="color: #007788;">WorkStarted</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;工作: 工作进行中&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>_events <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> _events.<span style="color: #007788;">WorkProgressing</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;工作: 工作完成&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>_events <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">int</span> grade <span style="color: #000080;">=</span> _events.<span style="color: #007788;">WorkCompleted</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
			Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;工人的工作得分＝&quot;</span> <span style="color: #000040;">+</span> grade<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">private</span> IWorkerEvents _events<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Boss <span style="color: #008080;">:</span> IWorkerEvents <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> WorkStarted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* 老板不关心。 */</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> WorkProgressing<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #ff0000; font-style: italic;">/* 老板不关心。 */</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> WorkCompleted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“时间差不多！”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* 总分为10 */</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong>委托</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 不幸的是，每当彼得忙于通过接口的实现和老板交流时，就没有机会及时通知宇宙了。至少他应该忽略身在远方的老板的引用，好让其他实现了IWorkerEvents的对象得到他的工作报告。（”At least he&#8217;d abstracted the reference of his boss far away from him so that others who implemented the IWorkerEvents interface could be notified of his work progress” 原话如此，不理解到底是什么意思:)）</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 他的老板还是抱怨得很厉害。“彼得！”他老板吼道，“你为什么在工作一开始和工作进行中都来烦我？！我不关心这些事件。你不但强迫我实现了这些方法，而且还在浪费我宝贵的工作时间来处理你的事件，特别是当我外出的时候更是如此！你能不能不再来烦我？”</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 于是，彼得意识到接口虽然在很多情况都很有用，但是当用作事件时，“粒度”不够好。他希望能够仅在别人想要时才通知他们，于是他决定把接口的方法分离为单独的委托，每个委托都像一个小的接口方法：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">delegate <span style="color: #0000ff;">void</span> WorkStarted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
delegate <span style="color: #0000ff;">void</span> WorkProgressing<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
delegate <span style="color: #0000ff;">int</span> WorkCompleted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Worker <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工作<span style="color: #008080;">:</span> 工作开始”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> started <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> started<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工作<span style="color: #008080;">:</span> 工作进行中”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> progressing <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> progressing<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;“工作: 工作完成”&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> completed <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">int</span> grade <span style="color: #000080;">=</span> completed<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工人的工作得分＝” <span style="color: #000040;">+</span> grade<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">public</span> WorkStarted started<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">public</span> WorkProgressing progressing<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">public</span> WorkCompleted completed<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Boss <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> WorkCompleted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Better...&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* 总分为10 */</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Universe <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Worker  peter <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Worker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Boss        boss <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Boss<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		peter.<span style="color: #007788;">completed</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> WorkCompleted<span style="color: #008000;">&#40;</span>boss.<span style="color: #007788;">WorkCompleted</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		peter.<span style="color: #007788;">DoWork</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“Main<span style="color: #008080;">:</span> 工人工作完成”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Console.<span style="color: #007788;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong>静态监听者</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 这样，彼得不会再拿他老板不想要的事件来烦他老板了，但是他还没有把宇宙放到他的监听者列表中。因为宇宙是个包涵一切的实体，看来不适合使用实例方法的委托（想像一下，实例化一个“宇宙”要花费多少资源…..），于是彼得就需要能够对静态委托进行挂钩，委托对这一点支持得很好：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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
20
21
22
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Universe <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> WorkerStartedWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Universe notices worker starting work&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> WorkerCompletedWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Universe pleased with worker's work&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">7</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		Worker  peter <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Worker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Boss        boss <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Boss<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		peter.<span style="color: #007788;">completed</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> WorkCompleted<span style="color: #008000;">&#40;</span>boss.<span style="color: #007788;">WorkCompleted</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		peter.<span style="color: #007788;">started</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> WorkStarted<span style="color: #008000;">&#40;</span>Universe.<span style="color: #007788;">WorkerStartedWork</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		peter.<span style="color: #007788;">completed</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> WorkCompleted<span style="color: #008000;">&#40;</span>Universe.<span style="color: #007788;">WorkerCompletedWork</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		peter.<span style="color: #007788;">DoWork</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“Main<span style="color: #008080;">:</span> 工人工作完成”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Console.<span style="color: #007788;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong>事件</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 不幸的是，宇宙太忙了，也不习惯时刻关注它里面的个体，它可以用自己的委托替换了彼得老板的委托。这是把彼得的Worker类的的委托字段做成public的一个无意识的副作用。同样，如果彼得的老板不耐烦了，也可以决定自己来激发彼得的委托（真是一个粗鲁的老板）：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Peter's boss taking matters into his own hands</span>
<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> peter.<span style="color: #007788;">completed</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> peter.<span style="color: #007788;">completed</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p> 彼得不想让这些事发生，他意识到需要给每个委托提供“注册”和“反注册”功能，这样监听者就可以自己添加和移除委托，但同时又不能清空整个列表也不能随意激发彼得的事件了。彼得并没有来自己实现这些功能，相反，他使用了event关键字让C#编译器为他构建这些方法：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Worker <span style="color: #008000;">&#123;</span>
	...
	<span style="color: #0000ff;">public</span> event WorkStarted started<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">public</span> event WorkProgressing progressing<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">public</span> event WorkCompleted completed<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 彼得知道event关键字在委托的外边包装了一个property，仅让C#客户通过+= 和 -=操作符来添加和移除，强迫他的老板和宇宙正确地使用事件。</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	Worker  peter <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Worker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Boss        boss <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Boss<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	peter.<span style="color: #007788;">completed</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> WorkCompleted<span style="color: #008000;">&#40;</span>boss.<span style="color: #007788;">WorkCompleted</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	peter.<span style="color: #007788;">started</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> WorkStarted<span style="color: #008000;">&#40;</span>Universe.<span style="color: #007788;">WorkerStartedWork</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	peter.<span style="color: #007788;">completed</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> WorkCompleted<span style="color: #008000;">&#40;</span>Universe.<span style="color: #007788;">WorkerCompletedWork</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	peter.<span style="color: #007788;">DoWork</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“Main<span style="color: #008080;">:</span> 工人工作完成”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Console.<span style="color: #007788;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong><span style="background: white none repeat scroll 0% 50%; line-height: 185%; font-family: verdana; color: #444444;">“</strong><strong>收获</strong><strong><span style="background: white none repeat scroll 0% 50%; line-height: 185%; font-family: verdana; color: #444444;">”</strong><strong>所有结果</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 到这时，彼得终于可以送一口气了，他成功地满足了所有监听者的需求，同时避免了与特定实现的紧耦合。但是他注意到他的老板和宇宙都为它的工作打了分，但是他仅仅接收了一个分数。面对多个监听者，他想要“收获”所有的结果，于是他深入到代理里面，轮询监听者列表，手工一个个调用：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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="cpp" style="font-family:monospace;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	...
	<span style="color: #007788;">Console</span>.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;“工作: 工作完成”&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> completed <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		foreach<span style="color: #008000;">&#40;</span> WorkCompleted wc in completed.<span style="color: #007788;">GetInvocationList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">int</span> grade <span style="color: #000080;">=</span> wc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工人的工作得分＝” <span style="color: #000040;">+</span> grade<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong>异步通知：激发</strong><strong><span style="background: white none repeat scroll 0% 50%; line-height: 185%; font-family: verdana; color: #444444;"> &amp; </strong><strong>忘掉</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 同时，他的老板和宇宙还要忙于处理其他事情，也就是说他们给彼得打分所花费的事件变得非常长：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Boss <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> WorkCompleted<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		System.<span style="color: #007788;">Threading</span>.<span style="color: #007788;">Thread</span>.<span style="color: #007788;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Better...&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">6</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* 总分为10 */</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Universe <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> WorkerCompletedWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		System.<span style="color: #007788;">Threading</span>.<span style="color: #007788;">Thread</span>.<span style="color: #007788;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Universe is pleased with worker's work&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">7</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	...
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p> 很不幸，彼得每次通知一个监听者后必须等待它给自己打分，现在这些通知花费了他太多的工作事件。于是他决定忘掉分数，仅仅异步激发事件：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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="cpp" style="font-family:monospace;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	...
	<span style="color: #007788;">Console</span>.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;“工作: 工作完成”&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> completed <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		foreach<span style="color: #008000;">&#40;</span> WorkCompleted wc in completed.<span style="color: #007788;">GetInvocationList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			wc.<span style="color: #007788;">BeginInvoke</span><span style="color: #008000;">&#40;</span>null, null<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong>异步通知：轮询</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 这使得彼得可以通知他的监听者，然后立即返回工作，让进程的线程池来调用这些代理。随着时间的过去，彼得发现他丢失了他工作的反馈，他知道听取别人的赞扬和努力工作一样重要，于是他异步激发事件，但是周期性地轮询，取得可用的分数。</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	...
	<span style="color: #007788;">Console</span>.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;“工作: 工作完成”&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> completed <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		foreach<span style="color: #008000;">&#40;</span> WorkCompleted wc in completed.<span style="color: #007788;">GetInvocationList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			IAsyncResult res <span style="color: #000080;">=</span> wc.<span style="color: #007788;">BeginInvoke</span><span style="color: #008000;">&#40;</span>null, null<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>res.<span style="color: #007788;">IsCompleted</span> <span style="color: #008000;">&#41;</span> System.<span style="color: #007788;">Threading</span>.<span style="color: #007788;">Thread</span>.<span style="color: #007788;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">int</span> grade <span style="color: #000080;">=</span> wc.<span style="color: #007788;">EndInvoke</span><span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工人的工作得分＝” <span style="color: #000040;">+</span> grade<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong>异步通知：委托</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 不幸地，彼得有回到了一开始就想避免的情况中来，比如，老板站在背后盯着他工作。于是，他决定使用自己的委托作为他调用的异步委托完成的通知，让他自己立即回到工作，但是仍可以在别人给他的工作打分后得到通知：</p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left">

<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="cpp" style="font-family:monospace;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoWork<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	...
	<span style="color: #007788;">Console</span>.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;“工作: 工作完成”&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> completed <span style="color: #000040;">!</span><span style="color: #000080;">=</span> null <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		foreach<span style="color: #008000;">&#40;</span> WorkCompleted wc in completed.<span style="color: #007788;">GetInvocationList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			wc.<span style="color: #007788;">BeginInvoke</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> AsyncCallback<span style="color: #008000;">&#40;</span>WorkGraded<span style="color: #008000;">&#41;</span>, wc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> WorkGraded<span style="color: #008000;">&#40;</span>IAsyncResult res<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	WorkCompleted wc <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>WorkCompleted<span style="color: #008000;">&#41;</span>res.<span style="color: #007788;">AsyncState</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> grade <span style="color: #000080;">=</span> wc.<span style="color: #007788;">EndInvoke</span><span style="color: #008000;">&#40;</span>res<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Console.<span style="color: #007788;">WriteLine</span><span style="color: #008000;">&#40;</span>“工人的工作得分＝” <span style="color: #000040;">+</span> grade<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; margin-bottom: 3pt; text-align: center; line-height: 185%;" align="center"><strong>宇宙中的幸福</strong><strong></strong></p>
<p class="MsoNormal" style="background: white none repeat scroll 0% 50%; text-align: left;" align="left"> 彼得、他的老板和宇宙最终都满足了。彼得的老板和宇宙可以收到他们感兴趣的事件通知，减少了实现的负担和非必需的往返“差旅费”。彼得可以通知他们，而不管他们要花多长时间来从目的方法中返回，同时又可以异步地得到他的结果。彼得知道，这并不*十分*简单，因为当他异步激发事件时，方法要在另外一个线程中执行，彼得的目的方法完成的通知也是一样的道理。但是，迈克和彼得是好朋友，他很熟悉线程的事情，可以在这个领域提供指导。</p>
<p> 他们永远幸福地生活下去……&lt;完&gt;</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F09%2F24%2Fc%25E5%258A%25A8%25E6%2580%2581%25E5%258A%25A0%25E8%25BD%25BDdll%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#动态加载DLL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2008%2F10%2F03%2Fnet-making-gains-against-java-survey-says%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">.NET Making Gains Against Java, Survey Says</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F08%2F26%2Fc%25E8%25AA%259E%25E8%25A8%2580%25E6%2595%2599%25E7%25A8%258B-%25EF%25BC%2588%25E4%25B8%2580%25EF%25BC%2589%25E7%2592%25B0%25E5%25A2%2583%25E9%2585%258D%25E7%25BD%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C語言教程 — （一）環境配置</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjennal.cn%2F2009%2F04%2F02%2Fc%25E7%259A%2584%25E7%2596%2591%25E9%2597%25AE%2F&from=http%3A%2F%2Fjennal.cn%2F2009%2F03%2F23%2F%25E4%25B8%2580%25E4%25B8%25AAc%25E7%259D%25A1%25E5%2589%258D%25E6%2595%2585%25E4%25BA%258B%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">C#的疑问</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://jennal.cn/2009/03/23/%e4%b8%80%e4%b8%aac%e7%9d%a1%e5%89%8d%e6%95%85%e4%ba%8b/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

