4

.NET のリソース ファイル (resx ファイル) 内のすべての文字列を列挙したいと考えています。これにより、これらすべてのキーと値のペアを含む JavaScript オブジェクトが生成されます。サテライト アセンブリに対しては、次のようなコードを使用してこれを行います (これは VB.NET ですが、サンプル コードは問題ありません)。

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

ただし、悲しいことに、.resx ファイルに対してこれを行う方法はまだ見つかっていません。resx ファイル内のすべてのローカライズ文字列を列挙するにはどうすればよいですか?

アップデート:

Dennis Myren のコメントとhereのアイデアに従って、ResXResourceManager を作成しました。これで、埋め込みリソースで行ったのと同じことを .resx ファイルで行うことができます。これがコードです。Microsoft は必要なコンストラクターを非公開にしたので、リフレクションを使用してアクセスします。これを使用するときは、完全な信頼が必要です。

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
4

2 に答える 2

3

System.Resources.ResXResourceReaderを使用します(System.Windows.Forms.dllにあります)

于 2008-11-10T16:42:08.030 に答える
1

http://tonesdotnetblog.wordpress.com/2010/02/14/string-enumerations-and-resource-files-in-c/には、リソースファイルから文字列を読み取ることができる列挙に属性を設定することを含む代替ソリューションがあります。これは、代替のカスタムツールを使用します。

于 2010-02-16T05:38:46.120 に答える