获取App_Code目录下类型信息--BlogEngine学习

 
  .net2.0  框架
  BlogEngine.Core 文件中的  Util.cs
  使用vs2005开发时,我们一般把.cs(非页面关联类)放到App_Code目录下,而App_Code目录下的类将编译成App_Code.dll程序集,如果在App_Code中启用多语言支持,就是在App_Code目录下建立子目录(比方目录名为"VB"),并且在web.config文件中的 system.web/compilation 下配置如下信息

  <system.web>
  <compilation debug="true">
  
   <codeSubDirectories >
    <add directoryName="VB"/>
   </codeSubDirectories>
  
   <assemblies>
    <add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    <add assembly="System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    <add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    <add assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    <add assembly="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
   </assemblies>
  </compilation>
.....
</system.web>
那么Asp.net除了生存App_Code.dll程序集文件外,还将生成App_SubCode_VB程序集文件,当然这里指发布网站后,
在发布目录的bin文件夹下可以找到的.如果是动态编译情况下(调试时)会生成App_Code.xxx,与App_SubCode_VB.xxx
程序集,注意(xxx为随机的,如"App_Code.kcxgvurw")

  我们可以通过方法Assembly.Load(string)来加载程序集,当指定参数为"__code"或"App_Code"字符串时可获取App_Code程序集,当指定参数为"App_SubCode_VB"字符串时可以获取App_SubCode_VB程序集的引用.
注意使用Assembly.Load方法时如果需要加载的程序集已经加载,则不会重复加载而是直接返回引用.

        /// <summary>
        /// This method returns all code assemblies in app_code
        /// If app_code has subdirectories for c#, vb.net etc
        /// Each one will come back as a separate assembly
        /// So we can support extensions in multiple languages
        /// </summary>
        /// <returns>List of code assemblies</returns>
        public static ArrayList CodeAssemblies()
        {
            ArrayList codeAssemblies = new ArrayList();
            CompilationSection s = null;
            try
            {
                string assemblyName = "__code";
                try
                {

                   //使用WebConfigurationManager获取complilation配置信息(codeSubDirectories)
                   //这个主要用来判断App_Code目录下有没有使用子目录
                   //在BlogEngine中如果配置了子目录,那么默认就忽略App_Code下的类型,而直接针对App_Code
                   //目录下的子目录来处理Extension(参靠下面代码得出)
                    s = (CompilationSection)WebConfigurationManager.GetSection("system.web/compilation");
                }
                catch (System.Security.SecurityException)
                {
                    // No read permissions on web.config due to the trust level (must be High or Full)
                }
              
                 //如果有子目录,那么只获取字目录生成的类型 
                if (s != null && s.CodeSubDirectories != null && s.CodeSubDirectories.Count > 0)
                {
                    for (int i = 0; i < s.CodeSubDirectories.Count; i++)
                    {
                        assemblyName = "App_SubCode_" + s.CodeSubDirectories[i].DirectoryName;
                        codeAssemblies.Add(Assembly.Load(assemblyName));
                    }
                }
                else
                {
                    Type t = Type.GetType("Mono.Runtime");//liuex系统中的.net
                    if (t != null) assemblyName = "App_Code";
                    codeAssemblies.Add(Assembly.Load(assemblyName));
                }
            }
            catch (System.IO.FileNotFoundException) {/*ignore - code directory has no files*/}

            return codeAssemblies;
        }

 


参考资料:

[原创]深入剖析ASP.NET的编译原理之一:动态编译(Dynamical Compilation)
BlogEngine.Net架构与源代码分析系列part9:开发扩展(上)——Extension与管理上的实现

你可能感兴趣的:(Engine)