﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>博客园-Ted's Blog</title><link>http://www.cnblogs.com/tedzhao/</link><description>记录点点滴滴、见证岁月痕迹；
励志照亮人生、技术改变命运。
</description><language>zh-cn</language><lastBuildDate>Sat, 26 Jul 2008 08:49:42 GMT</lastBuildDate><pubDate>Sat, 26 Jul 2008 08:49:42 GMT</pubDate><ttl>60</ttl><item><title>使用IDispatch::Invoke函数在C++中调用C#实现的托管类库方法 </title><link>http://www.cnblogs.com/tedzhao/archive/2008/07/23/1249845.html</link><dc:creator>ted</dc:creator><author>ted</author><pubDate>Wed, 23 Jul 2008 09:34:00 GMT</pubDate><guid>http://www.cnblogs.com/tedzhao/archive/2008/07/23/1249845.html</guid><wfw:comment>http://www.cnblogs.com/tedzhao/comments/1249845.html</wfw:comment><comments>http://www.cnblogs.com/tedzhao/archive/2008/07/23/1249845.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/tedzhao/comments/commentRss/1249845.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/tedzhao/services/trackbacks/1249845.html</trackback:ping><description><![CDATA[<p>使用IDispatch::Invoke函数在C++中调用C#实现的托管类库方法</p>
<p>原贴地址：<a title="http://blogs.msdn.com/silverlightshanghai/default.aspx" href="http://blogs.msdn.com/silverlightshanghai/default.aspx">http://blogs.msdn.com/silverlightshanghai/default.aspx</a> <br />
</p>
<p>&nbsp;</p>
<p>CLR Interop简而言之是让非托管代码与托管代码之间可以相互调用的技术。这项技术可以使开发人员重用已有的托管或非托管组建，并根据自己的需要，权衡托管代码的简易性与非托管代码的灵活性，选择适合自己实际情况的编程语言，而不用过多考虑重用的组件是用哪种语言开发的。Interop中文的意思是互通性，既然是互通，代码的调用就有两种不同的方向。本文所要讲述的是使用COM Interop技术在非托管代码方如何调用托管代码。</p>
<p><strong>1. 创建托管服务器</strong></p>
<p>首先让我们在Visual Studio 2008创建一个C#的Class Library（类库）项目，取名为MyManagedServer，在该项目中，我们要声明并实现一个接口。</p>
<p style="font-size: 11pt;">为了说明简单，该接口中只有一个方法，用于打印一些信息。其中的ComVisible属性至关重要，当它的值为true时，该接口才对COM可见。</p>
<p>&nbsp;</p>
<p>接口声明代码如下：</p>
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;System;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;System.Runtime.InteropServices;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;MyManagedServer&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img id="Codehighlighter1_94_374_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_94_374_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_94_374_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_94_374_Closed_Text').style.display='inline';" align="top"><img id="Codehighlighter1_94_374_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_94_374_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_94_374_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_94_374_Open_Text').style.display='inline';" align="top"></span><span id="Codehighlighter1_94_374_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_94_374_Open_Text"><span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;[ComVisible(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">),&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Guid(</span><span style="color: #800000;">"</span><span style="color: #800000;">79EDDA1C-F243-47C5-8954-5DEF01FA3D44</span><span style="color: #800000;">"</span><span style="color: #000000;">),&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InterfaceType(ComInterfaceType.InterfaceIsDual)]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">interface</span><span style="color: #000000;">&nbsp;IManagedFooClass&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">10</span><span style="color: #000000;"><img id="Codehighlighter1_291_368_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_291_368_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_291_368_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_291_368_Closed_Text').style.display='inline';" align="top"><img id="Codehighlighter1_291_368_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_291_368_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_291_368_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_291_368_Open_Text').style.display='inline';" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_291_368_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_291_368_Open_Text"><span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[PreserveSig,&nbsp;DispId(</span><span style="color: #800080;">1</span><span style="color: #000000;">)]&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;PrintFoo();&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top" />}</span></span></div>
<p> </p>
<p>&nbsp;</p>
<p style="font-size: 12pt;">接下来是实现该接口的类：</p>
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;System;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;System.Runtime.InteropServices;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;MyManagedServer&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;[ComVisible(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">),&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ClassInterface(ClassInterfaceType.AutoDual),&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ProgId(</span><span style="color: #800000;">"</span><span style="color: #800000;">MyManagedServer.ManagedFooClass</span><span style="color: #800000;">"</span><span style="color: #000000;">)&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;]&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;CustomCOMClient&nbsp;:&nbsp;IManagedFooClass<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;CustomCOMClient()&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;<br />
</span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[DispId(</span><span style="color: #800080;">1</span><span style="color: #000000;">)]&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;PrintFoo()&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">in&nbsp;MyManagedServer:&nbsp;CustomCOMClient.PrintFoo()</span><span style="color: #800000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">}&nbsp;&nbsp; <br />
</span></div>
<p>&nbsp;</p>
<p style="font-size: 12pt;">这里我们给这个类的ProgId属性赋一个值。等会儿在注册组件的时候，注册表中将会增加一个键值，将ProgId和runtime为我们自动生成的CLSID关联起来。</p>
<p>&nbsp;</p>
<p style="font-size: 12pt;"><strong style="font-size: 11pt;">2.  为COM Interop注册托管服务组件</strong></p>
<p style="font-size: 11pt;">注册组件可以用Visual  Studio帮我们自动注册，也可以在命令行下手动输入命令。若要使用Visual  Studio来帮我们注册组件，只需在项目属性页（鼠标右键项目名称，在下拉菜单中选择&#8220;Properties（属性）&#8221;）的Build标签页中把Register  for COM Interop项打上勾，然后再build项目就可以了。如下图所示：</p>
<p>&nbsp;<img alt="" src="http://www.cnblogs.com/images/cnblogs_com/tedzhao/RegisteCOMInterop.png" width="780" border="0" height="594" /></p>
<p style="font-size: 11pt;">此外，我们也可以先build项目，然后通过命令行的方式注册组件。只需要使用<a href="http://msdn.microsoft.com/en-us/library/tzat5yw6%28VS.71%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/tzat5yw6(VS.71).aspx">regasm.exe</a>在VS2008命令行下输入如下命令即可：</p>
<p style="font-size: 12pt;"><strong>regasm assemblyname.dll /tlb  /codebase</strong></p>
<p style="font-size: 12pt;">该命令会为我们注册组件，生成并注册对应的type  library文件。其中assemblyname.dll是项目构建生成的程序集文件。</p>
<p>&nbsp;</p>
<p style="font-size: 12pt;"><strong style="font-size: 11pt;">3.  创建非托管客户端</strong></p>
<strong style="font-size: 11pt;"></strong>
<p style="font-size: 11pt;">使用托管语言创建并注册了组建之后，我们就要使用非托管语言来尝试通过COM  Interop调用组建中的方法了。首先，在Visual Studio 2008中创建一个Visual C++ Win32 Console  Application，取名为MyNatvieClient，并将组建生成tlb文件拷贝至该项目的源代码目录中。然后在MyNativeClient.cpp中输入如下代码：</p>
<p>&nbsp;</p>
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /><span style="color: #000000;">#include&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">stdafx.h</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /><br />
</span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" />#import&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">mscorlib.tlb</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;no_namespace&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" />#import&nbsp;</span><span style="color: #800000;">"</span><span style="color: #800000;">MyManagedServer.tlb</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;no_namespace&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;_tmain(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argc,&nbsp;_TCHAR</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;argv[])&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img id="Codehighlighter1_155_1696_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_155_1696_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_155_1696_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_155_1696_Closed_Text').style.display='inline';" align="top"><img id="Codehighlighter1_155_1696_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_155_1696_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_155_1696_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_155_1696_Open_Text').style.display='inline';" align="top"></span><span id="Codehighlighter1_155_1696_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_155_1696_Open_Text"><span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;::CoInitialize(NULL);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;Get&nbsp;CLSID&nbsp;for&nbsp;CoCreateInstance&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">12</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;OLECHAR&nbsp;lpszProgID[]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;OLESTR(</span><span style="color: #800000;">"</span><span style="color: #800000;">MyManagedServer.ManagedFooClass</span><span style="color: #800000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;CLSID&nbsp;clsid;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;HRESULT&nbsp;hr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;CLSIDFromProgID(lpszProgID,&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">clsid);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(SUCCEEDED(hr))&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">16</span><span style="color: #000000;"><img id="Codehighlighter1_434_1668_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_434_1668_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_434_1668_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_434_1668_Closed_Text').style.display='inline';" align="top"><img id="Codehighlighter1_434_1668_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_434_1668_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_434_1668_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_434_1668_Open_Text').style.display='inline';" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_434_1668_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_434_1668_Open_Text"><span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">CLSIDFromProgID&nbsp;Succeeded&nbsp;\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IDispatch</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;ppv&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">0</span><span style="color: #000000;">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HRESULT&nbsp;hr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;CoCreateInstance(clsid,&nbsp;NULL,&nbsp;CLSCTX_ALL,&nbsp;IID_IDispatch,&nbsp;(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">**</span><span style="color: #000000;">)</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">ppv);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">21</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(SUCCEEDED(hr))&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">22</span><span style="color: #000000;"><img id="Codehighlighter1_665_1658_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_665_1658_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_665_1658_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_665_1658_Closed_Text').style.display='inline';" align="top"><img id="Codehighlighter1_665_1658_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_665_1658_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_665_1658_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_665_1658_Open_Text').style.display='inline';" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_665_1658_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_665_1658_Open_Text"><span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">23</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">CoCreateInstance&nbsp;Succeeded&nbsp;\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">24</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">25</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;Get&nbsp;DispId&nbsp;for&nbsp;Invoke&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">26</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISPID&nbsp;dispid;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">27</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;LPOLESTR&nbsp;szMember&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;OLESTR(</span><span style="color: #800000;">"</span><span style="color: #800000;">PrintFoo</span><span style="color: #800000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">28</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HRESULT&nbsp;hr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ppv</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">GetIDsOfNames(IID_NULL,&nbsp;(LPOLESTR</span><span style="color: #000000;">*</span><span style="color: #000000;">)</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">szMember,</span><span style="color: #800080;">1</span><span style="color: #000000;">,LOCALE_SYSTEM_DEFAULT,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">dispid);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">29</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(SUCCEEDED(hr))&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">30</span><span style="color: #000000;"><img id="Codehighlighter1_1026_1608_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1026_1608_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1026_1608_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1026_1608_Closed_Text').style.display='inline';" align="top"><img id="Codehighlighter1_1026_1608_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1026_1608_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1026_1608_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1026_1608_Open_Text').style.display='inline';" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_1026_1608_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_1026_1608_Open_Text"><span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">31</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">GetIDsOfNames&nbsp;Succeeded&nbsp;\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">32</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">33</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;There&amp;apos;s&nbsp;no&nbsp;parameter&nbsp;to&nbsp;pass&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">34</span><span style="color: #008000;"><img id="Codehighlighter1_1192_1195_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1192_1195_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1192_1195_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1192_1195_Closed_Text').style.display='inline';" align="top"><img id="Codehighlighter1_1192_1195_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1192_1195_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1192_1195_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1192_1195_Open_Text').style.display='inline';" align="top"></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISPPARAMS&nbsp;dispParams&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_1192_1195_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_1192_1195_Open_Text"><span style="color: #000000;">{</span><span style="color: #800080;">0</span><span style="color: #000000;">}</span></span><span style="color: #000000;">;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">35</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARIANT&nbsp;vtResult;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">36</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UINT&nbsp;dwArgErr;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">37</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">38</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HRESULT&nbsp;hr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ppv</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">Invoke(dispid,IID_NULL,NULL,DISPATCH_METHOD,&nbsp;<br />
</span><span style="color: #008080;">39</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">dispParams,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">vtResult,NULL,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">dwArgErr);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">40</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(SUCCEEDED(hr))&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">41</span><span style="color: #000000;"><img id="Codehighlighter1_1511_1590_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1511_1590_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_1511_1590_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_1511_1590_Closed_Text').style.display='inline';" align="top"><img id="Codehighlighter1_1511_1590_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_1511_1590_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_1511_1590_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_1511_1590_Open_Text').style.display='inline';" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_1511_1590_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_1511_1590_Open_Text"><span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">42</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Invoke&nbsp;Succeeded&nbsp;\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">43</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">44</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">45</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">46</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ppv</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">Release();&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">47</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">48</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">49</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">50</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">0</span><span style="color: #000000;">;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">51</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top" />}</span></span></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p style="font-size: 11pt;">该代码主要做了以下几件事情：</p>
<p style="font-size: 11pt;">a. 调用<a href="http://msdn.microsoft.com/en-us/library/ms678543%28VS.85%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms678543(VS.85).aspx">CoInitialize</a>进行初始化。</p>
<p style="font-size: 11pt;">b. 调用<a href="http://msdn.microsoft.com/en-us/library/ms688386.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms688386.aspx">CLSIDFromProgId</a>获得对象的CLSID，因为接下来的函数将通过CLSID来创建实例。</p>
<p style="font-size: 11pt;">c. 通过<a href="http://msdn.microsoft.com/en-us/library/ms686615.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms686615.aspx">CoCreateInstance</a>创建对象实例。这里创建的是一个IDispatch的对象实例。</p>
<p style="font-size: 11pt;">d. 调用<a href="http://msdn.microsoft.com/en-us/library/ms221306%28VS.85%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms221306(VS.85).aspx">IDispatch::GetIDsOfNames</a>以获得将要调用的方法的DispID，供接下来的函数使用。</p>
<p style="font-size: 11pt;">e. 使用<a href="http://msdn.microsoft.com/en-us/library/ms221479%28VS.85%29.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms221479(VS.85).aspx">IDispatch::Invoke</a>来调用方法。</p>
<p>&nbsp;</p>
<p style="font-size: 11pt;">在import type  library的时候我们不仅import了组建的tlb文件，同时还import了mscorlib.tlb以避免生成的临时的tlh文件中一些类型找不到的情况。</p>
<p>&nbsp;</p>
<p style="font-size: 11pt;">编译通过后运行结果，可看到命令行中打印出如下信息：</p>
<p>&nbsp;</p>
<p style="font-size: 12pt;">CLSIDFromProgID Succeeded<br />
CoCreateInstance  Succeeded<br />
GetIDsOfNames Succeeded<br />
in MyManagedServer:  CustomCOMClient.PrintFoo()<br />
Invoke Succeeded</p>
<p>&nbsp;</p><img src ="http://www.cnblogs.com/tedzhao/aggbug/1249845.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41316/" target="_blank">[新闻]腾讯CEO马化腾:中国互联网业准备再过冬</a>]]></description></item><item><title>XML和JSON(JavaScript Object Notation)</title><link>http://www.cnblogs.com/tedzhao/archive/2008/07/04/1233625.html</link><dc:creator>ted</dc:creator><author>ted</author><pubDate>Fri, 04 Jul 2008 06:50:00 GMT</pubDate><guid>http://www.cnblogs.com/tedzhao/archive/2008/07/04/1233625.html</guid><wfw:comment>http://www.cnblogs.com/tedzhao/comments/1233625.html</wfw:comment><comments>http://www.cnblogs.com/tedzhao/archive/2008/07/04/1233625.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cnblogs.com/tedzhao/comments/commentRss/1233625.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/tedzhao/services/trackbacks/1233625.html</trackback:ping><description><![CDATA[<span style="font-weight: bold;">原创文章，如需转载，请注明出处。<br />
<br />
</span>当我们在定义AJAX服务的时候，对于各种各样的请求，应该以什么样的数据格式来响应哪？<br />
当然如果是请求一段Markup代码的话，HTML就是最合适的，客户端请求到数据后，就可以直接插入到前端页面中了。那如果客户端需要的是纯粹的数据的话，我们是应该提供XML哪，还是JSON？这两者各有什么优缺点和好处哪，了解了这两者的差异后我们就可以基于实际情况进行权衡取舍了。<br />
<br />
我们从以下几个方面来进行比较：<br />
<strong>1、客户端</strong><br />
在客户端通过向服务器发送XMLHttpRequest，就可以得到请求数据了，哪什么样的数据格式更容易处理哪？<br />
&nbsp;<br />
JSON示例：<br />
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><img id="Codehighlighter1_0_115_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_0_115_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_0_115_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_0_115_Closed_Text').style.display='inline';" alt="" align="top" /><img id="Codehighlighter1_0_115_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_0_115_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_0_115_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_0_115_Open_Text').style.display='inline';" alt="" align="top" /><span id="Codehighlighter1_0_115_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_0_115_Open_Text"><span style="color: #000000;">{&nbsp;<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">author</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;:&nbsp;&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Gambardella,&nbsp;Matthew</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">title</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp; :&nbsp;&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">XML&nbsp;Developer's&nbsp;Guide</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">genre</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;&nbsp;:&nbsp;&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Computer</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top" />}</span></span></div>
<br />
对于JSON数据，只需要调用JavaScript的Eval方法就可以将JSON字符串序列化成为JavaScript对象，而后就可以直接访问了。如下所示：<br />
<br />
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /><span style="color: #0000ff;">var</span><span style="color: #000000;">&nbsp;book&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;eval(request.responseText);&nbsp;&nbsp;<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" />alert(book.author); <br />
</span></div>
<br />
XML示例：<br />
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">book</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">author</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Gambardella,&nbsp;Matthew</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">author</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">XML&nbsp;Developer's&nbsp;Guide</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">genre</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Computer</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">genre</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">book</span><span style="color: #0000ff;">&gt;</span></div>
<br />
对于XML数据，其本身是一个DOM Tree的数据结构，开发人员必须使用DOM API来访问和处理XML数据；而且DOM在各个浏览器中的实现也不尽相同，所以针对XML DOM的编程方式会变的更为复杂。参见下面的实现方式：<br />
<br />
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">var</span><span style="color: #000000;">&nbsp;book&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;request.responseXML;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #0000ff;">var</span><span style="color: #000000;">&nbsp;authors&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;book.getElementsByTagName(</span><span style="color: #000000;">"</span><span style="color: #000000;">author</span><span style="color: #000000;">"</span><span style="color: #000000;">);&nbsp;&nbsp;<br />
alert(authors[</span><span style="color: #000000;">0</span><span style="color: #000000;">].firstChild.textContent); <br />
</span></div>
<br />
另外对于XMLDOM，浏览器目前还不支持类似于XPath这样的查询语句。显然对于XMLDOM数据的访问显然要比访问JSON复杂多了。<br />
<br />
<strong>2、服务器端<br />
</strong>在服务器端需要向客户端发送数据，什么样的数据格式处理起来更容易哪？<br />
<br />
显然对于如何序列化或者犯序列化一个对象成为XML字符串，各种服务端的编程语言都有提供，而且还会有多种方式。例如.NET Framework就提供了<span id="nsrTitle">XmlSerializer类来序列化一个对象成为Xml文档，另外开发人员</span>还可以使用XmlWriter、XMLDocument来直接构建Xml字符串。<br />
<br />
而对于JSON这样一种数据格式则鲜有提供，这就需要开发人员自己动手或者使用一些Open Source的Library。在复杂对象的序列化或者反序列化上，由于Xml做的比较早应该会更稳定一些。<br />
<strong><br />
</strong>在这点上XML会优于JSON.<br />
<strong><br />
3、安全性<br />
</strong>JSON 本来是JavaScript 的一个安全的子集，不会含有赋值和调用。因此在将JSON数据装换成为JavaScript对象的时候，我们包括许多JavaScript 库都使用 <code>Eval</code> 函数。这意味着获取的JSON数据将被解析并执行，注意是<strong>执行</strong>，尤其有一些数据是来自用户输入的话，可能会带来意想不到的安全性问题。攻击者也可以利用这点发送畸形、恶意的 JSON数据，这样<code>Eval</code> 函数就会执行这些恶意代码。<br />
<strong>
<br />
</strong>从这一点上来说也要求我们如果在使用JSON作为数据交换格式的时候，必须保障JSON安全。比较常见的办法是使用正则表达式来检查JSON数据是否包含有恶意代码关键字。<br />
<br />
而XML则相对来讲更安全一些。<br />
<br />
<strong>
4、性能</strong><br />
从数据传输量上来看JSON显然要优于XML，JSON更轻量级一些，它没有像XML那样多的Open和Closing标记。同时在对数据的解析速度上，JSON也要优于XML。<br />
<br />
<strong>5. 其他</strong><br />
另外从数据格式的验证角度来看的话，XML的验证技术更成熟，而目前关于JSON的验证还比较少。<br />
<br />
<font face="Verdana"></font>
<img src ="http://www.cnblogs.com/tedzhao/aggbug/1233625.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41315/" target="_blank">[新闻]F8 Keynote Speech[多图]</a>]]></description></item><item><title>分布式计算、网格计算和云计算</title><link>http://www.cnblogs.com/tedzhao/archive/2008/07/02/1234262.html</link><dc:creator>ted</dc:creator><author>ted</author><pubDate>Wed, 02 Jul 2008 12:08:00 GMT</pubDate><guid>http://www.cnblogs.com/tedzhao/archive/2008/07/02/1234262.html</guid><wfw:comment>http://www.cnblogs.com/tedzhao/comments/1234262.html</wfw:comment><comments>http://www.cnblogs.com/tedzhao/archive/2008/07/02/1234262.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/tedzhao/comments/commentRss/1234262.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/tedzhao/services/trackbacks/1234262.html</trackback:ping><description><![CDATA[前几天与几个同事无意中聊起了云计算，还说Google也推出了相应的服务。<br />
心里就一直在想这到底是个什么东西哪，上网搜索了一把，又找到几个相近的概念，就在这里记录一下。<br />
<br />
1、分布式计算<br />
<blockquote>
所谓分布式计算是一门计算机科学，它研究如何把一个需要非常巨大的计算能力才能解决的问题分成许多小的部分，然后把这些部分分配给许多计算机进行处理，最后把这些计算结果综合起来得到最终的结果。最近的分布式计算项目已经被用于使用世界各地成千上万位志愿者的计算机的闲置计算能力，通过因特网，您可以分析来自外太空的电讯号，寻找隐蔽的黑洞，并探索可能存在的外星智慧生命；您可以寻找超过1000万位数字的梅森质数；您也可以寻找并发现对抗艾滋病病毒的更为有效的药物。这些项目都很庞大，需要惊人的计算量，仅仅由单个的电脑或是个人在一个能让人接受的时间内计算完成是决不可能的。<br />
<br />
分布式计算是利用互联网上的计算机的中央处理器的闲置处理能力来解决大型计算问题的一种计算科学。在两个或多个软件互相共享信息，这些软件既可以在同一台计算机上运行，也可以在通过网络连接起来的多台计算机上运行。<br />
</blockquote>
<br />
2、网格计算<br />
<blockquote>
首先网格计算也是一种分布式计算。<br />
<br />
网格计算通过利用大量异构计算机（通常为桌面）的未用资源（CPU周期和磁盘存储），将其作为嵌入在分布式电信基础设施中的一个虚拟的计算机集群，为解决大规模的计算问题提供了一个模型。网格计算的焦点放在支持跨管理域计算的能力，这使它与传统的计算机集群或传统的分布式计算相区别。<br />
<br />
网格计算的设计目标是解决对于任何单一的超级计算机来说仍然大得难以解决的问题，并同时保持解决多个较小的问题的灵活性。这样，网格计算就提供了一个多用户环境。它的第二个目标就是：更好的利用可用计算力，迎合大型的计算练习的断断续续的需求。<br />
<br />
有了网格计算技术，您可以将服务器、存储系统和网络联合在一起，组成一个大的系统，从而为用户提供功能强大的多系统资源来处理特定的任务。对于最终用户或应用程序来说，数据文件、应用程序和系统看起来就像是一个巨大的虚拟计算系统。<br />
<br />
网格计算与其他所有的分布式计算范例都有所区别：网格计算的本质在于以有效且优化的方式来利用组织中各种异构松耦合资源，来实现复杂的工作负载管理和信息虚拟化功能。<br />
<br />
参考资源：<br />
<a title="网格观点: 网格计算 —— 下一代分布式计算" href="http://www.ibm.com/developerworks/cn/grid/gr-heritage/index.html">网格观点: 网格计算 —— 下一代分布式计算</a><br />
<a target="_self" title="网格计算: 关键组件是什么？" href="http://www.ibm.com/developerworks/cn/grid/gr-overview/index.html">网格计算: 关键组件是什么？</a><br />
</blockquote>
<br />
3、云计算<br />
概念<br />
<blockquote>云计算(Cloud Computing)是分布式处理(Distributed Computing)、并行处理(Parallel Computing)和网格计算(Grid Computing)的发展，或者说是这些计算机科学概念的商业实现。<br />
<br />
云计算的基本原理是，通过使计算分布在大量的分布式计算机上，而非本地计算机或远程服务器中，企业数据中心的运行将更与互联网相似。这使得企业能够将资源切换到需要的应用上，根据需求访问计算机和存储系统。<br />
<br />
这可是一种革命性的举措，打个比方，这就好比是从古老的单台发电机模式转向了电厂集中供电的模式。它意味着计算能力也可以作为一种商品进行流通，就像煤气、水电一样，取用方便，费用低廉。最大的不同在于，它是通过互联网进行传输的。<br />
<br />
云计算的蓝图已经呼之欲出：在未来，只需要一台笔记本或者一个手机，就可以通过网络服务来实现我们需要的一切，甚至包括超级计算这样的任务。从这个角度而言，最终用户才是云计算的真正拥有者。<br />
<br />
云计算的应用包含这样的一种思想，把力量联合起来，给其中的每一个成员使用。<br />
</blockquote><br />
云计算的几大形式<br />
<br />
<blockquote>SAAS（软件即服务）<br />
这种类型的云计算通过浏览器把程序传给成千上万的用户。在用户眼中看来，这样会省去在服务器和软件授权上的开支；从供应商角度来看，这样只需要维持一个程序就够了，这样能够减少成本。Salesforce.com是迄今为止这类服务最为出名的公司。SAAS在人力资源管理程序和ERP中比较常用。 Google Apps和Zoho Office也是类似的服务<br />
<br />
实用计算（Utility Computing）<br />
这个主意很早就有了，但是知道最近才在Amazon.com、Sun、IBM和其它提供存储服务和虚拟服务器的公司中新生。这种云计算是为IT行业创造虚拟的数据中心使得其能够把内存、I/O设备、存储和计算能力集中起来成为一个虚拟的资源池来为整个网络提供服务。<br />
<br />
网络服务<br />
同SAAS关系密切，网络服务提供者们能够提供API让开发者能够开发更多基于互联网的应用，而不是提供单机程序。<br />
<br />
平台即服务<br />
另一种SAAS，这种形式的云计算把开发环境作为一种服务来提供。你可以使用中间商的设备来开发自己的程序并通过互联网和其服务器传到用户手中。<br />
<br />
MSP（管理服务提供商）<br />
最古老的云计算运用之一。这种应用更多的是面向IT行业而不是终端用户，常用于邮件病毒扫描、程序监控等等。<br />
<br />
商业服务平台<br />
SAAS和MSP的混合应用，该类云计算为用户和提供商之间的互动提供了一个平台。比如用户个人开支管理系统，能够根据用户的设置来管理其开支并协调其订购的各种服务。<br />
<br />
互联网整合<br />
将互联网上提供类似服务的公司整合起来，以便用户能够更方便的比较和选择自己的服务供应商。<br />
</blockquote><br />
<br />
<img src ="http://www.cnblogs.com/tedzhao/aggbug/1234262.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41314/" target="_blank">[新闻]奥运核心资源被分食 搜狐央视网谁忽悠谁？</a>]]></description></item><item><title>奥运圣火在家乡传递</title><link>http://www.cnblogs.com/tedzhao/archive/2008/06/27/1231366.html</link><dc:creator>ted</dc:creator><author>ted</author><pubDate>Fri, 27 Jun 2008 14:25:00 GMT</pubDate><guid>http://www.cnblogs.com/tedzhao/archive/2008/06/27/1231366.html</guid><wfw:comment>http://www.cnblogs.com/tedzhao/comments/1231366.html</wfw:comment><comments>http://www.cnblogs.com/tedzhao/archive/2008/06/27/1231366.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/tedzhao/comments/commentRss/1231366.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/tedzhao/services/trackbacks/1231366.html</trackback:ping><description><![CDATA[奥运圣火太原传递 祥云飘临古老龙城尽显豪情<br />
<br />
<object xcodebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" height="416" width="503"  classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" >
<param name="Movie" value="http://vhead.blog.sina.com.cn/player/bnplayer.swf?vid=http://vhead.blog.sina.com.cn/player/outer_player.swf?auto=0&vid=14596442&uid=&chn1=461&title=视频-奥运圣火太原传递 祥云飘临古老龙城尽显豪情&auto=1" />
<param name="Quality" value="high"/>
<param name="wmode" value="transparent"/><embed src="http://vhead.blog.sina.com.cn/player/bnplayer.swf?vid=http://vhead.blog.sina.com.cn/player/outer_player.swf?auto=0&vid=14596442&uid=&chn1=461&title=视频-奥运圣火太原传递 祥云飘临古老龙城尽显豪情&auto=1" width="503" height="416"  quality="high" wmode="transparent"     type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" ></embed>
</object><br />
<img src ="http://www.cnblogs.com/tedzhao/aggbug/1231366.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41313/" target="_blank">[新闻]微软推新型搜索技术"BrowseRank"挑战谷歌</a>]]></description></item><item><title>Inside ASP.NET 2.0 DataBound Control - 3</title><link>http://www.cnblogs.com/tedzhao/archive/2008/06/24/1228435.html</link><dc:creator>ted</dc:creator><author>ted</author><pubDate>Tue, 24 Jun 2008 15:18:00 GMT</pubDate><guid>http://www.cnblogs.com/tedzhao/archive/2008/06/24/1228435.html</guid><wfw:comment>http://www.cnblogs.com/tedzhao/comments/1228435.html</wfw:comment><comments>http://www.cnblogs.com/tedzhao/archive/2008/06/24/1228435.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnblogs.com/tedzhao/comments/commentRss/1228435.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/tedzhao/services/trackbacks/1228435.html</trackback:ping><description><![CDATA[ASP.NET 2.0 较之ASP.NET 1.0和1.1提供了新的数据绑定模型，添加了一系列新的数据展现、编辑控件譬如GridView、DetailView以及FormsView等控件。<br />
<br />
本文旨在阐述ASP.NET 2.0新的数据绑定控件架构，并且逐一介绍每一个控件。<br />
<br />
<strong>HierarchicalDataBoundControl<br />
</strong><br />
HierarchicalDataBoundControl是一个抽象类，从BaseDataBound类派生，用来绑定树状数据。所有用来显示或编辑树状数据的绑定控件都应该从该类派生，例如TreeView以及Menu控件。<br />
<br />
HierarchicalDataBoundControl实现的主要功能是从HierarchicalDataSourceControl中获取数据，派生类将不再需要关心如何从数据源中获取数据，而只需要关心其本身的业务逻辑即如何展示绑定数据。<br />
<br />
<strong>1. HierarchicalDataBoundControl</strong> <strong>接口<br />
</strong>
<div class="cnblogs_code"><span style="color: #008080;">&nbsp;1</span><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">abstract</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;HierarchicalDataBoundControl&nbsp;:&nbsp;BaseDataBoundControl<br />
</span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img id="Codehighlighter1_74_391_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_74_391_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_74_391_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_74_391_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_74_391_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_74_391_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_74_391_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_74_391_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_74_391_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_74_391_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;HierarchicalDataSourceView&nbsp;GetData(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;viewPath);<br />
</span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;IHierarchicalDataSource&nbsp;GetDataSource();<br />
</span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br />
</span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">override</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;PerformSelect();<br />
</span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">internal</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;PerformDataBinding();&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br />
</span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">override</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;ValidateDataSource(</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;dataSource);<br />
</span><span style="color: #008080;">10</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>
<br />
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>ValidateDataSource</strong><br />
当用户设者HierarchicalDataBoundControl的DataSource属性时，控件会调用ValidateDataSource方法来验证绑定数据源是否为合法的数据源，通过该方法的实现我们可以知道绑定数据源必须是IHierarchicalEnumerble或者IHirarhiclDataSource对象。&nbsp;</p>
<div class="cnblogs_code"><span style="color: #008080;">1</span><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">override</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;ValidateDataSource(</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;dataSource)<br />
</span><span style="color: #008080;">2</span><span style="color: #000000;"><img id="Codehighlighter1_62_242_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_62_242_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_62_242_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_62_242_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_62_242_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_62_242_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_62_242_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_62_242_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_62_242_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_62_242_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">3</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(((dataSource&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">!</span><span style="color: #000000;">(dataSource&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;IHierarchicalEnumerable))&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">!</span><span style="color: #000000;">(dataSource&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;IHierarchicalDataSource))<br />
</span><span style="color: #008080;">4</span><span style="color: #000000;"><img id="Codehighlighter1_187_240_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_187_240_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_187_240_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_187_240_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_187_240_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_187_240_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_187_240_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_187_240_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_187_240_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_187_240_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">5</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">throw</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;InvalidOperationException());<br />
</span><span style="color: #008080;">6</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">7</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>
<p>另外如果用户通过DataSourceID属性来设置数据源，当进行数据绑定时控件会尝试向数据源检索HierarchicalDataSourceView，如果检索不到，将抛出同样的异常。<br />
<br />
<strong>&nbsp;&nbsp;&nbsp; GetDataSource<br />
</strong>该方法将会从绑定数据源中检索相关联的IHierarchicalDataSource对象，而绑定数据源是用户通过DataSourceID属性指定的，也就是说GetDataSource方法只负责从DataSourceID属性指定的数据源中检索IHierarchicalDataSource对象。<br />
<br />
而控件的另外一个私有方法ConnectToHierarchicalDataSource则会调用GetDataSource方法，无论控件通过何种方式进行数据绑定，该方法都会返回与数据源关联的IHierarchicalDataSource对象。<br />
<br />
我们再来看一下IHierarchicalDataSource这样一个接口：</p>
<div class="cnblogs_code"><span style="color: #008080;">1</span><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">interface</span><span style="color: #000000;">&nbsp;IHierarchicalDataSource<br />
</span><span style="color: #008080;">2</span><span style="color: #000000;"><img id="Codehighlighter1_41_156_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_41_156_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_41_156_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_41_156_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_41_156_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_41_156_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_41_156_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_41_156_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_41_156_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_41_156_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">3</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">event</span><span style="color: #000000;">&nbsp;EventHandler&nbsp;DataSourceChanged;<br />
</span><span style="color: #008080;">4</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br />
</span><span style="color: #008080;">5</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;HierarchicalDataSourceView&nbsp;GetHierarchicalView(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;viewPath);<br />
</span><span style="color: #008080;">6</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>
<p>该接口将会返回一个HierarchicalDataSourceView对象，通过该对象我们就可以检索其背后的树状数据了。</p>
<p>&nbsp;&nbsp;&nbsp; <strong>GetData<br />
</strong>从数据源中获取HierarchicalDataSourceView 对象，另用该对象数据绑定控件可以来操作数据。HierarchicalDataSourceView类定义了Select操作，通过Select方法可以进行数据检索。</p>
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">abstract</span><span style="color: #000000;">&nbsp;<span style="color: #0000fe;">IHierarchicalEnumerable&nbsp;</span>Select()；</span></div>
<p>ASP.NET 2.0通过IHierarchicalEnumerable和IHierarchyData两个接口来组织树状的数据结构，通过这两个接口我们可以检索到树状数据的每一个节点。</p>
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">interface</span><span style="color: #000000;">&nbsp;IHierarchicalEnumerable&nbsp;:&nbsp;IEnumerable<br />
</span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img id="Codehighlighter1_55_118_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_55_118_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_55_118_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_55_118_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_55_118_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_55_118_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_55_118_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_55_118_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_55_118_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_55_118_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;IHierarchyData&nbsp;GetHierarchyData(</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;enumeratedItem);<br />
</span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">interface</span><span style="color: #000000;">&nbsp;IHierarchyData<br />
</span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img id="Codehighlighter1_151_335_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_151_335_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_151_335_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_151_335_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_151_335_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_151_335_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_151_335_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_151_335_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_151_335_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_151_335_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;IHierarchicalEnumerable&nbsp;GetChildren();<br />
</span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;IHierarchyData&nbsp;GetParent();<br />
</span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br />
</span><span style="color: #008080;">10</span><span style="color: #000000;"><img id="Codehighlighter1_250_258_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_250_258_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_250_258_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_250_258_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_250_258_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_250_258_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_250_258_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_250_258_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;HasChildren&nbsp;</span><span id="Codehighlighter1_250_258_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_250_258_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">11</span><span style="color: #000000;"><img id="Codehighlighter1_275_283_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_275_283_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_275_283_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_275_283_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_275_283_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_275_283_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_275_283_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_275_283_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;Item&nbsp;</span><span id="Codehighlighter1_275_283_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_275_283_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">12</span><span style="color: #000000;"><img id="Codehighlighter1_300_308_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_300_308_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_300_308_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_300_308_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_300_308_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_300_308_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_300_308_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_300_308_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;Path&nbsp;</span><span id="Codehighlighter1_300_308_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_300_308_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">13</span><span style="color: #000000;"><img id="Codehighlighter1_325_333_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_325_333_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_325_333_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_325_333_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_325_333_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_325_333_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_325_333_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_325_333_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;Type&nbsp;</span><span id="Codehighlighter1_325_333_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_325_333_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">14</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: #000000;"><br />
</span></div>
<p>IHierarchicalEnumerable接口负责在兄弟节点之间进行枚举迭代，而IHierarchyData接口则负责在父子节点之间进行枚举迭代。<strong><br />
</strong></p>
<p><strong>&nbsp;&nbsp;&nbsp; PerformSelect和PerformanDataBinding</strong></p>
<p>HierarchicalDataBoundControl重载了PerformSelect方法，该方法只是设定数据绑定内部标志以及触发绑定事件，而将数据绑定的操作委托给了新定义的虚方法PerformDataBinding。</p>
<p>从该类派生的子类只需要重载PerformDataBinding方法来进行数据展示及其本身业务逻辑的处理就可以了。具体实现应该是在PerformDataBinding方法中通过GetData方法获取HierarchicalDataSourceView对象，然后进行数据检索。</p>
<p><span style="font-weight: bold;">2.&nbsp; 页面生命周期<br />
</span>HierarchicalDataBoundControl在页面生命周期中定义了一些时机来进行数据绑定的处理：</p>
<p><span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; OnPagePreLoad</span><br />
如果是页面进行第一次请求的话，则控件要求进行数据绑定。另外如果是一次PostBack的话，并且控件仍然没有进行数据绑定，同时Enable ViewState，控件也会要求数据绑定。</p>
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">override</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;OnPagePreLoad(</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;sender,&nbsp;EventArgs&nbsp;e)<br />
</span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img id="Codehighlighter1_66_428_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_66_428_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_66_428_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_66_428_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_66_428_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_66_428_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_66_428_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_66_428_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_66_428_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_66_428_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnPagePreLoad(sender,&nbsp;e);<br />
</span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.Page&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)<br />
</span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img id="Codehighlighter1_134_391_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_134_391_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_134_391_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_134_391_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_134_391_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_134_391_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_134_391_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_134_391_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_134_391_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_134_391_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">!</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.Page.IsPostBack)<br />
</span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img id="Codehighlighter1_179_235_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_179_235_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_179_235_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_179_235_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_179_235_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_179_235_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_179_235_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_179_235_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_179_235_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_179_235_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.RequiresDataBinding&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">10</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.IsViewStateEnabled&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.ViewState[</span><span style="color: #800000;">"</span><span style="color: #800000;">_!DataBound</span><span style="color: #800000;">"</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">))<br />
</span><span style="color: #008080;">11</span><span style="color: #000000;"><img id="Codehighlighter1_329_385_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_329_385_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_329_385_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_329_385_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_329_385_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_329_385_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_329_385_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_329_385_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_329_385_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_329_385_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">12</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.RequiresDataBinding&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">13</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">14</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">15</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">._pagePreLoadFired&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">16</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>
<p>这一点跟DataBoundControlde处理是一样的。</p>
<p>&nbsp;&nbsp;&nbsp; <span style="font-weight: bold;">OnLoad</span><br />
在OnLoad的时候控件会向绑定数据源检索IHierarchicalDataSource对象，并且进行备份。然后如果发现控件没有进行数据绑定的话，判断页面是否为第一次请求，如果是则要求进行绑定，如果不是当控件Enable ViewState，也会要求数据绑定。</p>
<div class="cnblogs_code"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">internal</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">override</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;OnLoad(EventArgs&nbsp;e)<br />
</span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img id="Codehighlighter1_53_469_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_53_469_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_53_469_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_53_469_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_53_469_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_53_469_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_53_469_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_53_469_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_53_469_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_53_469_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.ConfirmInitState();<br />
</span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.ConnectToHierarchicalDataSource();<br />
</span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(((</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.Page&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">!</span><span style="color: #0000ff;">this</span><span style="color: #000000;">._pagePreLoadFired)&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.ViewState[</span><span style="color: #800000;">"</span><span style="color: #800000;">_!DataBound</span><span style="color: #800000;">"</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">))<br />
</span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img id="Codehighlighter1_233_447_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_233_447_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_233_447_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_233_447_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_233_447_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_233_447_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_233_447_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_233_447_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_233_447_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_233_447_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">!</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.Page.IsPostBack)<br />
</span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img id="Codehighlighter1_278_334_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_278_334_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_278_334_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_278_334_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_278_334_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_278_334_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_278_334_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_278_334_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_278_334_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_278_334_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.RequiresDataBinding&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">10</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">11</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.IsViewStateEnabled)<br />
</span><span style="color: #008080;">12</span><span style="color: #000000;"><img id="Codehighlighter1_385_441_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_385_441_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_385_441_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_385_441_Closed_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_385_441_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_385_441_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_385_441_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_385_441_Open_Text').style.display='inline';" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_385_441_Closed_Text" style="border: 1px solid #808080; display: none; background-color: #ffffff;"><img alt="" src="http://www.cnblogs.com/Images/dot.gif" /></span><span id="Codehighlighter1_385_441_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">13</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.RequiresDataBinding&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">14</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">15</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">16</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnLoad(e);<br />
</span><span style="color: #008080;">17</span><span style="color: #000000;"><img alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>
<p>&nbsp;3. 小结<br />
HierarchicalDataBoundControl定义了树状数据绑定控件的基类，其主要功能是实现了向层级的数据源检索数据，其派生类只需要针对获得的树状数据包进行自己的业务处理就可以了。<br />
<br />
&nbsp;4. 结束语<br />
这是Inside ASP.NET DataBoundControl系列文章中的第三篇，第一篇介绍了BaseDataBoundControl,后两篇分别介绍了DataBoundControl和HierarchicalDataBoundControl，前者用于绑定表状数据，后者用于绑定树状数据。<br />
<br />
以后如果有时间的话，我会写一下DataSourceControl以及DataSourceView，这两块其实是跟DataBoundControl密不可分的。<br />
<br />
有什么问题大家可以互相交流，取长补短，共同进步。</p><img src ="http://www.cnblogs.com/tedzhao/aggbug/1228435.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41312/" target="_blank">[新闻]2008年7月26日IT博客精选</a>]]></description></item><item><title>Inside ASP.NET 2.0 DataBound Control - 2</title><link>http://www.cnblogs.com/tedzhao/archive/2008/06/16/DataBoundControl_2.html</link><dc:creator>ted</dc:creator><author>ted</author><pubDate>Mon, 16 Jun 2008 13:19:00 GMT</pubDate><guid>http://www.cnblogs.com/tedzhao/archive/2008/06/16/DataBoundControl_2.html</guid><wfw:comment>http://www.cnblogs.com/tedzhao/comments/1220376.html</wfw:comment><comments>http://www.cnblogs.com/tedzhao/archive/2008/06/16/DataBoundControl_2.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnblogs.com/tedzhao/comments/commentRss/1220376.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/tedzhao/services/trackbacks/1220376.html</trackback:ping><description><![CDATA[l<span style="font-weight: bold;">原创文章，如需转载，请注明出处。<br />
</span>
<p style="font-weight: bold;">DataBoundControl</p>
<div v:shape="_x0000_s1026" class="O">
<div><span style="font-size: 111%;"><span style="color: #6ea0b0; position: absolute; left: -3.97%; top: 0.22em; font-family: &quot;Wingdings 2&quot;; font-size: 80%;"></span></span>DataBoundControl是一个抽象类，从BaseDataBound类派生，用来绑定List或者Table状数据。所有用来显示或编辑List或Table状数据的绑定控件都应该从该类派生，例如ListBox，DropDownList，CheckboxList还有GridView。<br />
<br />
DataBoundControl实现的主要功能是从数据源中获取数据，无论用户是通过设置DataSource还是通过DataSourceID进行的数据绑定。获取到的数据将以IEnumerable的方式提供给派生类，这样派生类将不再需要关心如何从数据源中获取数据，而只需要关心其本身的业务逻辑即如何展示绑定数据。<br />
<br />
<span style="font-weight: bold;">1.&nbsp; DataBoundControl接口：</span><br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">abstract</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;DataBoundControl&nbsp;:&nbsp;BaseDataBoundControl<br />
</span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img id="Codehighlighter1_62_393_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_62_393_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_62_393_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_62_393_Closed_Text').style.display='inline';" alt="" align="top"><img id="Codehighlighter1_62_393_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_62_393_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_62_393_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_62_393_Open_Text').style.display='inline';" alt="" align="top"></span><span id="Codehighlighter1_62_393_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_62_393_Open_Text"><span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img id="Codehighlighter1_101_114_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_101_114_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_101_114_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_101_114_Closed_Text').style.display='inline';" alt="" align="top"><img id="Codehighlighter1_101_114_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_101_114_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_101_114_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_101_114_Open_Text').style.display='inline';" alt="" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;DataMember&nbsp;</span><span id="Codehighlighter1_101_114_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_101_114_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img id="Codehighlighter1_171_179_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_171_179_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_171_179_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_171_179_Closed_Text').style.display='inline';" alt="" align="top"><img id="Codehighlighter1_171_179_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_171_179_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_171_179_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_171_179_Open_Text').style.display='inline';" alt="" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;DataSourceSelectArguments&nbsp;SelectArguments&nbsp;</span><span id="Codehighlighter1_171_179_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_171_179_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">&nbsp;5</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" /><br />
</span><span style="color: #008080;">&nbsp;6</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">override</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;PerformSelect();<br />
</span><span style="color: #008080;">&nbsp;7</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;PerformDataBinding(&nbsp;IEnumerable&nbsp;data);<br />
</span><span style="color: #008080;">&nbsp;8</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" /><br />
</span><span style="color: #008080;">&nbsp;9</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;DataSourceView&nbsp;GetData();<br />
</span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" alt="" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;IDataSource&nbsp;GetDataSource();<br />
</span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top" />}</span></span></div>
<br />
&nbsp;&nbsp;&nbsp;
<strong>DataMember</strong>
<ul>
</ul>
DataMember属性用来指定绑定数据列表名称，用户设置的数据源可能会包含多个不同的数据项列表。比如绑定到控件的数据源控件含有多个DataSourceView，或者绑定到控件的数据源是一个DataSet，而这个DataSet含有多个数据表格。<br />
<br />
&nbsp;&nbsp;&nbsp;
<strong>SelectArguments</strong>
<ul>
</ul>
而SelectArguments属性则定义了用于向数据源控件检索数据时使用的 DataSourceSelectArgument 对象，注意使数据源控件，也就是通过DataSourceID进行的数据绑定。<br />
<br />
通过该对象可以指定检索数据的起始行位置、最大行数，设定排序表达式，同时还可以检索数据源的总行数。<br />
<br />
下面是DataSourceSelectArgument 对象的接口：<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" alt="" align="top" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">sealed</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;DataSourceSelectArguments<br />
</span><span style="color: #008080;">&nbsp;2</span><span style="color: #000000;"><img id="Codehighlighter1_46_281_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_46_281_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_46_281_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_46_281_Closed_Text').style.display='inline';" alt="" align="top"><img id="Codehighlighter1_46_281_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_46_281_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_46_281_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_46_281_Open_Text').style.display='inline';" alt="" align="top"></span><span id="Codehighlighter1_46_281_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_46_281_Open_Text"><span style="color: #000000;">{&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080;">&nbsp;3</span><span style="color: #000000;"><img id="Codehighlighter1_81_94_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_81_94_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_81_94_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_81_94_Closed_Text').style.display='inline';" alt="" align="top"><img id="Codehighlighter1_81_94_Closed_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_81_94_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_81_94_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_81_94_Open_Text').style.display='inline';" alt="" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;StartRowIndex&nbsp;</span><span id="Codehighlighter1_81_94_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_81_94_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">&nbsp;4</span><span style="color: #000000;"><img id="Codehighlighter1_122_135_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_122_135_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_122_135_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_122_135_Closed_Text').style.display='in