C# 如何通过存储在变量中的名称调用字符串资源?
我想通过存储在变量中的名称调用.resx中的字符串类型资源 假设我有一个带有以下列的C# 如何通过存储在变量中的名称调用字符串资源?,c#,.net,vb.net,visual-studio,resx,C#,.net,Vb.net,Visual Studio,Resx,我想通过存储在变量中的名称调用.resx中的字符串类型资源 假设我有一个带有以下列的DataGridView1: | Kiwi | Apple | Grape | 'these are the columns name 我有一个Core.resx来存储我的资源: | Name | Value | Comment | 'note how the name of the resource | DGV_Kiwi | Hairy Kiwi | | 'is "D
DataGridView1
:
| Kiwi | Apple | Grape | 'these are the columns name
我有一个Core.resx
来存储我的资源:
| Name | Value | Comment | 'note how the name of the resource
| DGV_Kiwi | Hairy Kiwi | | 'is "DGV_" & column.Name
| DGV_Apple | Red Apple | |
| DGV_Grape | White Grape | |
加载DataGridView1
时,我想将头更改为资源文件的值。当然,我可以通过以下方式来实现:
DataGridView1.Columns(0).HeaderText = My.Resources.Core.DGV_Kiwi
DataGridView1.Columns(1).HeaderText = My.Resources.Core.DGV_Apple
DataGridView1.Columns(2).HeaderText = My.Resources.Core.DGV_Grape
但是我很懒,我的真实项目中有很多专栏。所以我想用一个循环来做这个
这是我尝试的第一件事:
For Each Col As DataGridViewColumn In Me.DataGridView1.Columns
Col.HeaderText = My.Resources.Core.Col.Name
Next
当然,这是行不通的:
Col不是Core的成员
我也试过:
For Each Col As DataGridViewColumn In Me.DGVComps.Columns
Col.HeaderText = My.Resources.ResourceManager.GetObject("Core.DGV_" & Col.Name)
Next
但是My.Resources.ResourceManager.GetObject(“Core.DGV_”&Col.Name)
返回Nothing
那么这可能吗?还是我必须直呼我的资源的名字
PS:我已经在VB.NET中给出了这个例子,但我也可以在C#中实现。使用
Col.HeaderText = My.Resources.Core.ResourceManager.GetString("DGV_" & Col.Name)
“核心”不是资源项名称的一部分,而是资源包本身的名称
使用My.Resources.ResourceManager
时,可以访问项目的默认资源包。您可以在项目的“属性”对话框中的Visual Studio中访问它