2017-04-17 44 views
0

我的項目中有全局和本地嵌入式資源,如圖所示。Assembly.GetExecutingAssembly()。GetManifestResourceNames()從App_LocalResources返回資源嗎?

Resources files in my project with build action as embedded resources

我有一個函數ResourceText如下

   public static string GLOBAL_RESOURCES = "SampleClient.App_GlobalResources.Global.resources"; 

       /// <summary> 
       /// Used in JavaScript/front code to return resource translations for current page or global resource file 
       /// </summary> 
       /// <param name="pResourceKey"></param> 
       /// <returns></returns> 
       /// <remarks></remarks> 
       public string ResourceText(string pResourceKey, bool pGlobalResource = false) 
       { 
        if (string.IsNullOrEmpty(pResourceKey)) throw new ArgumentNullException("ResourceKey cannot be empty"); 

        if (pGlobalResource) 
        { 
         // Find the value from the global resource 

         ResourceManager tResourceManager = new System.Resources.ResourceManager(GLOBAL_RESOURCES.Replace(".resources", ""), this.GetType().BaseType.Assembly); 
         tResourceManager.IgnoreCase = true; 

         string tTranlsation = tResourceManager.GetString(pResourceKey); 

         return string.IsNullOrEmpty(tTranlsation) ? pResourceKey : tTranlsation; 
        } 
        else 
        { 
         string[] tAssemblyNames = Assembly.GetExecutingAssembly().GetManifestResourceNames(); 
         try 
         { 
          if (tAssemblyNames.Length >= 1) // There is a local file associated 
          { 
           // Get value from the local resource 
           string tAssemblyName = this.Page.GetType().BaseType.FullName.Insert(this.Page.GetType().BaseType.FullName.LastIndexOf(".") + 1, "App_LocalResources."); 


           string tResName = (from n in tAssemblyNames where n.Contains(tAssemblyName + ".aspx") select n).First().Replace(".resources", ""); 
           ResourceManager tResourceManager = new System.Resources.ResourceManager(tResName, this.GetType().BaseType.Assembly); 

           tResourceManager.IgnoreCase = true; 


           string tTranlsation = tResourceManager.GetString(pResourceKey); 
           return string.IsNullOrEmpty(tTranlsation) ? pResourceKey : tTranlsation; 
          } 
         } 

         catch (Exception ex) 
         { 
          throw (ex); 
          // Check the local resources 
         } 

        } 
        // Fall back 
        return pResourceKey; 
       } 

被稱爲在我的aspx頁面

<input type="search" id="inputCustomerGroupGridSearch" placeholder="<%= ResourceText("PlaceholderSearch")%>" /> 
      <button type="button" id="buttonNewCustomerGroup" style="float: right" class="PrimaryButton"><%=ResourceText("ButtonNew")%></button> 

當我調試的功能ResourceText,線代碼

string[] tAssemblyNames = Assembly.GetExecutingAssembly().GetManifestResourceNames(); 

僅返回「SampleClient.App_GlobalResources.Global.resources」而非「SampleClient.Modules.Customers.App_LocalResources.Customers.resouces」。爲什麼App_LocalResources中的資源不是由Assembly.GetExecutingAssembly()。GetManifestResourceNames()返回的?

回答

0

解決:我在我的bin文件夾中添加了強類型的程序集App_LocalResources.resources.dll。然後它在執行Assembly.GetExecutingAssembly()時被列在資源名稱中。GetManifestResourceNames();

所以,我可以調用我的ResourceText函數全局和本地資源。