c# - Convert values from a Dictionary<string, object> with T -
i have class inherits dictionary this
public class managedsettings : dictionary<string, object> because want able this
public object this[string key, bool myvar = true] { get{...} set{...} } so user can do
managersettingvar["hiskey"] = hisvalue; but want allow user this
managersettingvar<bool>["hiskey"] so value key "hiskey" comes casted bool already. possible i'm suggesting? tried using t haven't been lucky yet. like
public t this<t>[string key, bool myvar = true]
no, not possible. indexers cannot made generic.
you can create generic getvalue function:
public t getvalue<t>(string key) { return convert.changetype(this[key], typeof(t)); } or, of values need casted:
public t getvalue<t>(string key) { return (t)this[key]; }
Comments
Post a Comment