I’d like to use a resource file and want to read values from it during runtime.
Now I got something like:
using System;
using System.Collections;
using System.Threading;
using System.Resources;
using System.Reflection;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace MFConsoleApplication1
{
public class Program
{
public static void Main()
{
ResourceManager rm = new ResourceManager("Resources", Assembly.GetExecutingAssembly());
}
}
}
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace FEZ_Domino_Application1
{
public class Program
{
public static void Main()
{
// Blink board LED
bool ledState = false;
OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);
while (true)
{
// Sleep for 500 milliseconds
Thread.Sleep(500);
// toggle LED state
ledState = !ledState;
led.Write(ledState);
Debug.Print(
Resources.GetString(Resources.StringResources.String1));
}
}
}
}
That’s working for me as well, but is not what I want.
I Want to get a specific resource, which name is inside a string.
So not Resources.StringResources.String1
string key=“string1”;
But something like Resources.StringResources(key)
AFAICT, you have to cook that yourself, because we don’t have Enum.GetNames.
Basically, create a switch method that returns the Enum from a name and use the Enum.
private static Properties.Resources.StringResources GetByName(string name)
{
switch (name.ToLower())
{
case "string1":
return Properties.Resources.StringResources.String1;
default:
throw new InvalidOperationException("Invalid resource name");
}
}
string s = Properties.Resources.GetString(GetByName("string1"));
Debug.Print(s);