最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Enumerating all strings in resx - Stack Overflow

programmeradmin4浏览0评论

We would like to enumerate all strings in a resource file in .NET (resx file). We want this to generate a javascript object containing all these key-value pairs. We do this now for satellite assemblies with code like this (this is VB.NET, but any example code is fine):

Dim rm As ResourceManager
rm = New ResourceManager([resource name], [your assembly])
Dim Rs As ResourceSet
Rs = rm.GetResourceSet(Thread.CurrentThread.CurrentCulture, True, True)
For Each Kvp As DictionaryEntry In Rs
    [Write out Kvp.Key and Kvp.Value]
Next

However, we haven't found a way to do this for .resx files yet, sadly. How can we enumerate all localization strings in a resx file?

UPDATE:

Following Dennis Myren's ment and the ideas from here, I built a ResXResourceManager. Now I can do the same with .resx files as I did with the embedded resources. Here is the code. Note that Microsoft made a needed constructor private, so I use reflection to access it. You need full trust when using this.

Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Windows.Forms

Public Class ResXResourceManager
    Inherits ResourceManager

    Public Sub New(ByVal BaseName As String, ByVal ResourceDir As String)
        Me.New(BaseName, ResourceDir, GetType(ResXResourceSet))
    End Sub

    Protected Sub New(ByVal BaseName As String, ByVal ResourceDir As String, ByVal UsingResourceSet As Type)
        Dim BaseType As Type = Me.GetType().BaseType
        Dim Flags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance
        Dim Constructor As ConstructorInfo = BaseType.GetConstructor(Flags, Nothing, New Type() { GetType(String), GetType(String), GetType(Type) }, Nothing)
        Constructor.Invoke(Me, Flags, Nothing, New Object() { BaseName, ResourceDir, UsingResourceSet }, Nothing)
    End Sub

    Protected Overrides Function GetResourceFileName(ByVal culture As CultureInfo) As String
        Dim FileName As String
        FileName = MyBase.GetResourceFileName(culture)
        If FileName IsNot Nothing AndAlso FileName.Length > 10 Then
            Return FileName.Substring(0, FileName.Length - 10) & ".resx"
        End If
        Return Nothing
    End Function
End Class

We would like to enumerate all strings in a resource file in .NET (resx file). We want this to generate a javascript object containing all these key-value pairs. We do this now for satellite assemblies with code like this (this is VB.NET, but any example code is fine):

Dim rm As ResourceManager
rm = New ResourceManager([resource name], [your assembly])
Dim Rs As ResourceSet
Rs = rm.GetResourceSet(Thread.CurrentThread.CurrentCulture, True, True)
For Each Kvp As DictionaryEntry In Rs
    [Write out Kvp.Key and Kvp.Value]
Next

However, we haven't found a way to do this for .resx files yet, sadly. How can we enumerate all localization strings in a resx file?

UPDATE:

Following Dennis Myren's ment and the ideas from here, I built a ResXResourceManager. Now I can do the same with .resx files as I did with the embedded resources. Here is the code. Note that Microsoft made a needed constructor private, so I use reflection to access it. You need full trust when using this.

Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Windows.Forms

Public Class ResXResourceManager
    Inherits ResourceManager

    Public Sub New(ByVal BaseName As String, ByVal ResourceDir As String)
        Me.New(BaseName, ResourceDir, GetType(ResXResourceSet))
    End Sub

    Protected Sub New(ByVal BaseName As String, ByVal ResourceDir As String, ByVal UsingResourceSet As Type)
        Dim BaseType As Type = Me.GetType().BaseType
        Dim Flags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance
        Dim Constructor As ConstructorInfo = BaseType.GetConstructor(Flags, Nothing, New Type() { GetType(String), GetType(String), GetType(Type) }, Nothing)
        Constructor.Invoke(Me, Flags, Nothing, New Object() { BaseName, ResourceDir, UsingResourceSet }, Nothing)
    End Sub

    Protected Overrides Function GetResourceFileName(ByVal culture As CultureInfo) As String
        Dim FileName As String
        FileName = MyBase.GetResourceFileName(culture)
        If FileName IsNot Nothing AndAlso FileName.Length > 10 Then
            Return FileName.Substring(0, FileName.Length - 10) & ".resx"
        End If
        Return Nothing
    End Function
End Class
Share Improve this question edited Nov 14, 2008 at 13:26 Erik Hesselink asked Nov 10, 2008 at 15:57 Erik HesselinkErik Hesselink 2,53921 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Use System.Resources.ResXResourceReader (it's in System.Windows.Forms.dll)

http://tonesdotnetblog.wordpress./2010/02/14/string-enumerations-and-resource-files-in-c/ has an alternative solution that involves putting attributes on enumerations that enable reading strings from resource files. This uses an alternative custom tool.

发布评论

评论列表(0)

  1. 暂无评论