Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取要存储在字典中的所有(Properties.Resources)_C#_Visual Studio - Fatal编程技术网

C# 获取要存储在字典中的所有(Properties.Resources)

C# 获取要存储在字典中的所有(Properties.Resources),c#,visual-studio,C#,Visual Studio,基于一个ID,我想自动将一个图像加载到我的GUI中。为此,我希望能够使用C从Visual Studio 2008中的Resources.resx文件中获取所有图像。我知道如果我知道它们是什么,我可以一次获取一个: Image myPicture = Properties.Resources.[name of file]; 然而,我所寻找的是沿着这些路线 foreach(Bitmap myPicture in Properties.Resources) {Do something...} 好的

基于一个ID,我想自动将一个图像加载到我的GUI中。为此,我希望能够使用C从Visual Studio 2008中的Resources.resx文件中获取所有图像。我知道如果我知道它们是什么,我可以一次获取一个:

Image myPicture = Properties.Resources.[name of file];
然而,我所寻找的是沿着这些路线

foreach(Bitmap myPicture in Properties.Resources) {Do something...}

好的,这似乎是工作,但我会欢迎其他答案

ResourceManager rm = Properties.Resources.ResourceManager;

ResourceSet rs = rm.GetResourceSet(new CultureInfo("en-US"), true, true);

if (rs != null)
{
   IDictionaryEnumerator de = rs.GetEnumerator();
   while (de.MoveNext() == true)
   {
      if (de.Entry.Value is Image)
      {
         Bitmap bitMap = de.Entry.Value as Bitmap;
      }
   }
}
只需使用Linq tm即可

ResourceManager rm = Properties.Resources.ResourceManager;

ResourceSet rs = rm.GetResourceSet(new CultureInfo("en-US"), true, true);

if (rs != null)
{
   var images = 
     from entry in rs.Cast<DictionaryEntry>() 
     where entry.Value is Image 
     select entry.Value;

   foreach (Image img in images)
   {
     // do your stuff
   } 
}

我喜欢这样,尤其是当resource.resx文件中有其他数据类型时。我还没有研究Linq,所以我假设这将生成执行此任务的代码。从上面的答案中,你知道速度有什么不同吗?我猜这将是微不足道的,但总是最好问。谢谢你的回答!没有codegen,这是代码。至于速度,应该在同一个范围内。