Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to implement class with collection of string/object pairs so that an object can be returned with generic method?
    primarykey
    data
    text
    <p>The values in a file are read as string and can be double, string or int or maybe even lists. An example file:</p> <pre> DatabaseName=SomeBase Classes=11;12;13 IntValue=3 //this is required! DoubleValue=4.0 </pre> <p>I was thinking something like this:</p> <pre><code>class ConfigValues { private static SomeObject _utiObject; private static string _cfgFileName = "\\SomeSettings.cfg"; private static Dictionary&lt;string, Type&gt; _settingNamesAndTypes = new Dictionary&lt;string, Type&gt;(); private static Dictionary&lt;string, object&gt; _settings = new Dictionary&lt;string, object&gt;(); private static string _directory = string.Empty; const string _impossibleDefaultValue = "987ABC654DEF321GHI"; public static T GetConfigValue&lt;T&gt;(string cfgName) { object value; if (_settings.TryGetValue(cfgName, out value)) return (T)value; else return default(T); } public static bool LoadConfig(Dictionary&lt;string, Type&gt; reqSettings, Dictionary&lt;string, Type&gt; optSettings, Dictionary&lt;string, object&gt; optDefaultValues, out string errorMsg) { errorMsg = string.Empty; try { _utiObject = new SomeObject(new string[] { "-c", CfgFileNameAndPath }); } catch (Exception e) { errorMsg = string.Format("Unable to read {0}. Exception: {1}", CfgFileNameAndPath, e.Message); return false; } foreach (KeyValuePair&lt;string, Type&gt; kVPair in reqSettings) { if (!ReadCheckAndStore(kVPair, null, out errorMsg)) return false; _settingNamesAndTypes.Add(kVPair.Key, kVPair.Value); } foreach (KeyValuePair&lt;string, Type&gt; kVPair in optSettings) { if (!ReadCheckAndStore(kVPair, optDefaultValues[kVPair.Key], out errorMsg)) return false; _settingNamesAndTypes.Add(kVPair.Key, kVPair.Value); } return true; } private static bool ReadCheckAndStore(KeyValuePair&lt;string, Type&gt; kVPair, object defaultValue, out string errorMsg) { errorMsg = string.Empty; string usedDefaultValue, value = string.Empty; /* required setting */ if (defaultValue == null) usedDefaultValue = _impossibleDefaultValue; else usedDefaultValue = defaultValue.ToString(); //all string parameters below _utiObject.GetConfigValue(kVPair.Key, usedDefaultValue, ref value); if (_impossibleDefaultValue == value) { errorMsg = string.Format("Required configuration setting {0} was not" + "found in {1}", kVPair.Key, CfgFileNameAndPath); return false; } Type type = kVPair.Value; _settings[kVPair.Key] = Convert.ChangeType(value, type); return true; } } </code></pre> <p>PS. Additional issue is default values for optional settings. It's not elegant to pass them to LoadConfig in separate Dictionary, but that is an other issue...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload