2011-02-17 65 views
1

我按照從以下鏈接指引創建一個自定義的資源提供者:我使用下面的代碼 http://asp-net-whidbey.blogspot.com/2006/03/aspnet-20-custom-resource-provider.htmlC# - 如何從自定義資源提供程序讀取字符串值?

在.aspx頁,它很好地工作:

<asp:Literal ID="ltlFoo" runat="server" Text="<%$ Resources:SomeText %>" /> 

現在,我想從代碼中讀取本地化值:

string foo = Resources.GetString("SomeText"); 

問題是我不知道如何實例化資源管理器。
任何幫助將不勝感激!

編輯:
我用下面的代碼,它的偉大工程:

string foo = (string)GetGlobalResourceObject("", "SomeText"); 

是否有任何理由爲什麼我不應該使用這個代碼嗎?

回答

1

所以你的資源經理應該有一個名字,你應該可以做類似於以下的事情。

// Create a resource manager to retrieve resources. 
     ResourceManager rm = new ResourceManager("items", 
      Assembly.GetExecutingAssembly()); 


// Retrieve the value of the string resource named "welcome". 
// The resource manager will retrieve the value of the 
// localized resource using the caller's current culture setting. 
String foo = rm.GetString("SomeText"); 

Taken From MSDN Example

+0

如何獲取資源管理器的名稱? – 2011-02-17 16:55:31

相關問題