C# 基础系列--程序集三

   接着前面的的内容。程序集版本控制

运行时执行以下几步来解析程序集绑定请求:

检查原程序集引用,以确定该程序集的版本是否被绑定。

检查所有适用的配置文件以应用版本策略。

通过原程序集引用和配置文件中指定的任何重定向来确定正确的程序集,并且确定应绑定到调用程序集的版本。

检查全局程序集缓存和在配置文件中指定的基本代码,然后使用在运行时如何定位程序集中解释的探测规则检查该应用程序的目录和子目录。

  

我找到了两个关键字 ”指定程序集的位置“和”重定向程序集版本

节约大家的时间,我就挑部分出来

”重定向程序集版本

即使是自称向后兼容的新版本程序集,也会中断应用程序。 在发生这种情况时,可以在应用程序配置文件中使用下列设置,以使运行时跳过发行者策略:

<publisherPolicy apply="no" />

  

重定向程序集版本

若要将一个版本重定向到另一个版本,请使用 <bindingRedirect> 元素。 oldVersion 特性可以指定单个版本,也可以指定多个版本范围。 例如,<bindingRedirect oldVersion="1.1.0.0-1.2.0.0" newVersion="2.0.0.0"/> 指定运行时应使用版本 2.0.0.0 代替 1.1.0.0 和 1.2.0.0 之间的程序集版本。

  这个貌似在那里看到过类似的,翻了下代码,果然mvc中出现过

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

 指定程序集的位置

只有在计算机配置文件或也重定向程序集版本的发行者策略文件中,才可以使用 <codeBase> 元素。 在运行时确定要使用哪一程序集版本时,它应用确定版本的文件中的基本代码设置。 如果未指出基本代码,那么运行时就以通常的方法探测程序集。 有关详细信息,请参见运行时如何定位程序集。

下面的示例说明如何指定程序集的位置。

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="myAssembly"
                           publicKeyToken="32ab4ba45e0a69a1"
                           culture="en-us" />
         <codeBase version="2.0.0.0"
                   href="http://www.litwareinc.com/myAssembly.dll"/>
       </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

 

运行时通过探测的方法来查找没有基本代码的程序集。 有关探测的更多信息,请参见运行时如何定位程序集。

可以在应用程序配置文件中使用 <probing> 元素,来指定在查找程序集时运行时应搜索的子目录。 下面的示例说明如何指定运行时应搜索的目录。

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <probing privatePath="bin;bin2\subbin;bin3"/>
      </assemblyBinding>
   </runtime>
</configuration>

  

我承认我又开始无聊了,准备对之前的项目继续搞搞。

新建App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
        <assemblyIdentity name="AssemblyLibrary"
                          culture="neutral"
                          publicKeyToken="525b694786e1bcdf" />
        <!--<publisherPolicy apply="no" />-->
         <!--<codeBase version="1.0.0.2"
                   href="D:\documents\visual studio 2010\Projects\ConsolTest\ConsolTest\bin\Debug\AssemblyLibrary.dll"/>-->
         <!--<bindingRedirect oldVersion="1.0.0.0-1.0.0.1" newVersion="1.0.0.2" />-->
        </dependentAssembly>
      </assemblyBinding>
  </runtime>
</configuration>

  

  昨天晚上的实验内容我先删掉了,早上到公司修改了上一遍的内容,不好意思,其实很早的时候用ILSpy反编译的时候,就可以看得出程序集的信息是不一样。不过通过一系列实验,更能验证程序集信息这点。

  上班~~晚上要加班。。。有空,就对这篇进行更新

  晚上随便试了下,注释点的3个,有效的只有

<bindingRedirect oldVersion="1.0.0.0-1.0.0.1" newVersion="1.0.0.2" />
其他的为什么写和没有好像没有多大关系,不得而知。

昨天晚上睡觉前,查了下资料,才明白codeBase 这个关键字只用用于公有程序集,probing 私有程序集和公有程序集
  -----2012年7月13日 星期五修改


各位朋友程序集这个章就到这里了。下一章就是很基础的 类 。如果对程序集还有意思的朋友,可以看看的什么程序集全局等等,个人觉得用到可能性很低就不写这些了

 

 

posted @ 2012-07-13 09:49  望江南  阅读(756)  评论(0编辑  收藏  举报