.NET:通过反射获取资源
如何从当前程序集中动态获取所有资源?我尝试了两种方法,GetManifestResourceNames 和 GetResourceSet 但没有成功。我对 VB.net 或 C# 解决方案感到满意。
第一个方法
第一个方法仅返回长度为 1 且值为“MyNameSpace.Resources.resource”的数组。问题是该文件中有超过 1 个资源。
Dim ca As Assembly = Assembly.GetExecutingAssembly
Dim rn() As String = CurrentAssembly.GetManifestResourceNames()
第二种方法
Dim ca As Assembly = Assembly.GetExecutingAssembly
Dim crm As New ResourceManager("", ca)
''//Dim CurrentResourceManager As New ResourceManager(_
"MyNamespace.Resources.resources", CurrentAssembly)
''//Dim CurrentResourceManager As New ResourceManager( _
"My.Resources", CurrentAssembly)
Dim rs As ResourceSet = CurrentResourceManager.GetResourceSet(CultureInfo.CurrentCulture, True, True)
Dim rs As ResourceSet = crm.GetResourceSet( _
CultureInfo.CurrentCulture, True, True)
MissingManifestResourceException 未处理
找不到任何资源 适合特定文化 或中立文化。确保 “.resources”已正确嵌入或 链接到程序集“MyProgram” 在编译时,或者所有的 所需的卫星组件是 可加载且完全签名。可加载且完全签名。
解决方案(根据 Hans Passant)
从 Resources.Designer.vb 复制命名空间
<Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager
Get
If Object.ReferenceEquals(resourceMan, Nothing) Then
Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("MyNamespace.Resources", GetType(Resources).Assembly)
resourceMan = temp
End If
Return resourceMan
End Get
End Property
并将其放入代码中
Dim CurrentResourceManager As New ResourceManager( _
"MyNamespace.Resources", CurrentAssembly)
Dim rs As ResourceSet = CurrentResourceManager.GetResourceSet( _
CultureInfo.CurrentCulture, True, True)
How do I get all th resources dynamically from my current assembly? I have tried two methods, GetManifestResourceNames and GetResourceSet with no success. I am comfortable with solutions in VB.net or C#.
First Method
This first method only returns an array of length 1 with this value "MyNameSpace.Resources.resource". The problem is that there are more than 1 resource in this file.
Dim ca As Assembly = Assembly.GetExecutingAssembly
Dim rn() As String = CurrentAssembly.GetManifestResourceNames()
Second Method
Dim ca As Assembly = Assembly.GetExecutingAssembly
Dim crm As New ResourceManager("", ca)
''//Dim CurrentResourceManager As New ResourceManager(_
"MyNamespace.Resources.resources", CurrentAssembly)
''//Dim CurrentResourceManager As New ResourceManager( _
"My.Resources", CurrentAssembly)
Dim rs As ResourceSet = CurrentResourceManager.GetResourceSet(CultureInfo.CurrentCulture, True, True)
Dim rs As ResourceSet = crm.GetResourceSet( _
CultureInfo.CurrentCulture, True, True)
MissingManifestResourceException was unhandled
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
".resources" was correctly embedded or
linked into assembly "MyProgram"
at compile time, or that all the
satellite assemblies required are
loadable and fully signed.loadable and fully signed.
Solution (as per Hans Passant)
Copy the Namespace from the Resources.Designer.vb
<Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager
Get
If Object.ReferenceEquals(resourceMan, Nothing) Then
Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("MyNamespace.Resources", GetType(Resources).Assembly)
resourceMan = temp
End If
Return resourceMan
End Get
End Property
and place it in the code
Dim CurrentResourceManager As New ResourceManager( _
"MyNamespace.Resources", CurrentAssembly)
Dim rs As ResourceSet = CurrentResourceManager.GetResourceSet( _
CultureInfo.CurrentCulture, True, True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,MyNameSpace.Resources.resource 中有超过 1 个资源。这是您必须传递给 ResourceManager 的名称,空字符串不起作用。
要查看必须编写的代码类型,请启动 Windows 窗体应用程序并将一些资源添加到“项目 + 属性”、“资源”选项卡。在解决方案资源管理器中,单击“显示所有文件”图标。打开“我的项目”节点,打开“Resources.resx”节点,然后双击“Resources.Designer.vb”文件。请注意 ResourceManager 属性的代码。
Yes, there's more than 1 resource in MyNameSpace.Resources.resource. Which is the name you have to pass to ResourceManager, an empty string isn't going to work.
To see the kind of code you have to write, start a Windows Forms application and add a couple of resources to the Project + Properties, Resources tab. In the Solution Explorer, click the "Show All Files" icon. Open the My Project node, open the Resources.resx node and double-click the Resources.Designer.vb file. Note the code for the ResourceManager property.