开发者

how to retrieve the value of a kvp in c sharp, using the text of the key?

开发者 https://www.devze.com 2023-04-09 14:15 出处:网络
I have various values and names stored in a db table, that my code will use for various tasks. The开发者_运维知识库se are read into a kvp , so that I can get to the right value from its key when I ne

I have various values and names stored in a db table, that my code will use for various tasks.

The开发者_运维知识库se are read into a kvp , so that I can get to the right value from its key when I need to.

As far as I've seen so far, getting to the value using the index of the key is easy, but can I get in via the name of the key instead

kvp["name of key"].value

for instance?

I know any key/values stored in the config files can be accessed as

ConfigurationManager.AppSettings["queryLoadJobDataDetails"]

but as I need to read these values in as I go, adding them to web.config is a bit pointless!


I'm just Guessing what you need here:

 using System.Collections.Generic;


 //
 IDictionary<string, object> kvps = new Dictionary<string, object>();

 kvps.Add("something", 42);
 kvps.Add("else", "entirely");

 object i = kvps["something"];
 string s = kvps["else"] as string;

Also look at TryGetValue:

 object v;
 if (kvps.TryGetValue("missing", ref v))
     Console.WriteLine("Not missing: {0}", v);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号