2014-10-06 46 views
0

存在我使用下面的代码来检索C#“GetResourceSet”的方法[在System.Resources]类返回空 - 即使它在收集

 CultureInfo cultureInfo = new System.Globalization.CultureInfo("zh-CN"); // just hard coded to make question simpler 
     ResourceManager rm = new ResourceManager("SchedulingSystem", Assembly.GetExecutingAssembly()); 

     var entry = rm.GetResourceSet(cultureInfo, true, true) 
     .OfType<DictionaryEntry>() 
     .FirstOrDefault(e => e.Value.ToString() == input);//input => "No_Records" 

     var key = entry.Key.ToString(); 
     return key; 

key和值的资源文件中的值返回为null,但如果我在Visual Studio中使用“快速查看”,则可以看到图像中显示的值,在快速查看对象中可以看到输入“No_Records”。

我也提到堆栈溢出quesitons像[Why does ResourceManager.GetResourceSet return null on the first request after a build? (C#) enter image description here

我会在我的C#代码错过了什么?

+1

有一个在你的代码中简单的拼写错误,你要比较e.Key为*输入*,不e.Value – 2014-10-06 18:52:04

+0

感谢解决。非常感谢汉斯。 – iShareHappyCode 2014-10-06 18:54:18

回答

0

我不知道,如果的ResourceSet字典键是区分或文化敏感,但也有您的实施可能存在两个问题:

  1. 您正在使用的价值并不重要

  2. 您正在执行直接比较价值而不是减轻文化和区分大小写的差异。

更改为以下应该可以解决你的问题:

.FirstOrDefault(e => e.Key.ToString().Equals(input, StringComparison.InvariantCultureIgnoreCase)); 
+0

感谢胜任 – iShareHappyCode 2014-10-06 19:01:47

相关问题