编程语言
首页 > 编程语言> > c# – ASP.NET * .resx序列化

c# – ASP.NET * .resx序列化

作者:互联网

我正在为一个I18N项目做贡献,并且调用将我们的* .resx文件序列化为JSON对象(无论出于何种原因).

我想知道的是:

>有没有办法获取给定* .resx文件的所有有效键的列表,以便我们可以使用HttpContext.GetGlobalResourceObject来获取令牌?
>如果这不起作用,有人想出一个聪明的解决方案吗?

解决方法:

  Sub ReadRessourceFile()
       ''#Requires Assembly System.Windows.Forms 
        Dim rsxr As System.Resources.ResXResourceReader = New System.Resources.ResXResourceReader("items.resx")

        ''# Iterate through the resources and display the contents to the console.    
        Dim d As System.Collections.DictionaryEntry
        For Each d In rsxr
            Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString())
        Next d

        ''#Close the reader. 
        rsxr.Close()
    End Sub

然后,您需要将其添加到Serializable Dictionary,然后可以使用System.Web.Extensions.dll将其序列化为JSON.

Public Class JSONHelper

Public Shared Function Serialize(Of T)(ByVal obj As T) As String
    Dim JSONserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()  
    Return JSONserializer.Serialize(obj)
End Function

Public Shared Function Deserialize(Of T)(ByVal json As String) As T
    Dim obj As T = Activator.CreateInstance(Of T)()
    Dim JSONserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
    obj = JSONserializer.Deserialize(Of T)(json)
    Return obj
End Function

End Class

编辑:C#:

public void ReadRessourceFile()
{
    //Requires Assembly System.Windows.Forms '
    System.Resources.ResXResourceReader rsxr = new System.Resources.ResXResourceReader("items.resx");

    // Iterate through the resources and display the contents to the console. '    
    System.Collections.DictionaryEntry d = default(System.Collections.DictionaryEntry);
    foreach (DictionaryEntry d_loopVariable in rsxr) {
        d = d_loopVariable;
        Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString());
    }

    //Close the reader. '
    rsxr.Close();
}

和JSON助手:

public class JSONHelper
{

    public static string Serialize<T>(T obj)
    {
        System.Web.Script.Serialization.JavaScriptSerializer JSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        return JSONserializer.Serialize(obj);
    }

    public static T Deserialize<T>(string json)
    {
        T obj = Activator.CreateInstance<T>();
        System.Web.Script.Serialization.JavaScriptSerializer JSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        obj = JSONserializer.Deserialize<T>(json);
        return obj;
    }

}

标签:c,serialization,asp-net,internationalization,resx
来源: https://codeday.me/bug/20190526/1158912.html