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

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -