ich übergebe eine Farbe als string an meine App.
Dort wird aus dem Enum System.Drawing.Color die passende Farbe herausgesucht und gesetzt.
Das funktioniert, sofern die App als Debug bereitgestellt wird.
Wird sie als Release kompiliert, erhalte ich folgende Meldung:
Fehler |
Type provided must be an Enum. Parameter name: enumType |
Der entsprechende Code:
(Es handelt sich dabei noch um ein Relikt, welches Win CE kompatibel ist. Es gibt sicherlich mittlerweile elegantere Umsetzungen)
public static class Colors
{
public static readonly Dictionary<Color, string> ColorNames = new Dictionary<Color, string>();
public static readonly Dictionary<string, Color> ColorValues = new Dictionary<string, Color>();
static Colors()
{
var fi = typeof(Color).GetFields(BindingFlags.Static | BindingFlags.Public);
for (var iEnum = 0; iEnum < fi.Length; iEnum++)
{
AddColor(fi[iEnum].Name, (Color)fi[iEnum].GetValue(null));
}
var pi = typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public);
for (var iEnum = 0; iEnum < pi.Length; iEnum++)
{
if (pi[iEnum].PropertyType == typeof(Color))
{
AddColor(pi[iEnum].Name, (Color)pi[iEnum].GetValue(null, null));
}
}
pi = typeof(SystemColors).GetProperties(BindingFlags.Static | BindingFlags.Public);
for (var iEnum = 0; iEnum < pi.Length; iEnum++)
{
if (pi[iEnum].PropertyType == typeof(Color))
{
AddColor(pi[iEnum].Name,(Color)pi[iEnum].GetValue(null, null));
}
}
}
private static void AddColor(string name, Color color)
{
if (ColorNames.ContainsKey(color))
return;
//Not supported:
if (color == Color.Transparent)
return;
ColorNames.Add(color, name);
ColorValues.Add(name, color);
}
public static string ColorToString(Color color)
{
string name;
if (!ColorNames.TryGetValue(color, out name))
{
name = "Empty";
}
return name;
}
public static Color StringToColor(string colorName)
{
Color color;
if (ColorValues.TryGetValue(colorName, out color))
return color;
if (ColorValues.TryGetValue(string.Format("Color [{0}]", colorName), out color))
return color;
try
{
color = (Color)Enum.Parse(typeof(Color), colorName, true);
}
catch (Exception ex)
{
MessageMgt.CaptureError(ex.Message, "Color " + colorName);
color = Color.Empty;
}
return color;
}
}
Der Fehler tritt bei
Enum.Parse(typeof(Color), colorName, true);auf. Z.B. wenn man "Green" oder "AliceBlue" übergibt.
Der Debugger hilft mir dabei leider nicht weiter, da der Fehler schließlich nur auftritt, wenn ich ohne Debug-Informationen kompiliere.
Hätte jemand eine Idee woran das liegen könnte?
Oder wie ich es selbst herausfinden kann?