【问题标题】:MVC Global Resource FIle [duplicate]MVC 全局资源文件 [重复]
【发布时间】:2015-08-06 11:05:05
【问题描述】:

我的 Global Resx 文件名为 Appointment.resx

我正在使用以下代码访问 c# 中的键值:

 string str = Resources.Appointment.AppointmentID;

现在,我的问题是如何使用键获取 Comments 值。

注意:评论只是 resx 文件中的一列。

请帮忙。 谢谢。

【问题讨论】:

  • 只是想知道为什么要使用“评论”字段?它应该是对密钥的引用,因此当您查看评论时,它应该会告诉您有关密钥的信息。例如在哪里使用它。

标签: c# asp.net asp.net-mvc


【解决方案1】:

您应该可以通过 ResXDataNode 类获得评论:http://msdn.microsoft.com/en-us/library/system.resources.resxdatanode.aspx

这是代码:

// Enumerate the resources in the file.
      ResXResourceReader rr = new ResXResourceReader(resxFilename);
      rr.UseResXDataNodes = true;
      IDictionaryEnumerator dict = rr.GetEnumerator();
      while (dict.MoveNext()) {
         ResXDataNode node = (ResXDataNode) dict.Value;
         Console.WriteLine("{0,-20} {1,-20} {2}", 
                           node.Name + ":", 
                           node.GetValue((ITypeResolutionService) null), 
                           ! String.IsNullOrEmpty(node.Comment) ? "// " + node.Comment : "");
      }

您需要在阅读器上设置 UseResXDataNodes 标志:http://msdn.microsoft.com/en-us/library/system.resources.resxresourcereader.useresxdatanodes.aspx

但请注意 - 这种方式似乎只适用于磁盘上的 .RESX 文件。

【讨论】:

    【解决方案2】:

    我认为ResXResourceReader 应该可以。

    ResXResourceReader rr = new ResXResourceReader(resxFilename);
    rr.UseResXDataNodes = true; // this is important!
    
    var resXDataNodes = rr.GetEnumerator().Select(i => i.Value).OfType<ResXDataNode>();
    
    foreach(var resXDataNode in resXDataNodes)
    {
       var comment = resXDataNode.Comment;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多