简单实现ConfigurationManager.AppSettings[""]效果存储系统变量
代码一:存储变量和常量的Class.

Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Collections.Specialized;
5
6
namespace TestTemp.ConsoleApp
7

{
8
public class Config
9
{
10
string[] keys = new string[]
{ "Name", "Age", "Location", "Email", "Key", "One", "Two", "Three", "Four" };
11
Dictionary<string, string> tmp = new Dictionary<string, string>();
12
public Config()
13
{
14
for (int i = 0; i < keys.Length; i++)
15
{
16
17
tmp.Add(keys[i], keys[i].ToUpper());
18
}
19
}
20
public int Count
21
{
22
get
23
{
24
return tmp.Keys.Count;
25
}
26
}
27
28
public string this[string index]
29
{
30
get
31
{
32
if (tmp.ContainsKey(index))
33
{
34
return tmp[index];
35
}
36
else
{ return ""; }
37
38
}
39
}
40
41
}
42
}
43
代码二,实现静态访问:

Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace TestTemp.ConsoleApp
6

{
7
public static class Test
8
{
9
public static Config AppSettings
10
{
11
get
{ return new Config(); }
12
}
13
}
14
}
15
代码三,使用:

Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.IO;
5
using System.Management;
6
using System.Text.RegularExpressions;
7
8
9
namespace TestTemp.ConsoleApp
10

{
11
class Program
12
{
13
14
static void Main(string[] args)
15
{
16
17
Console.Write(Test.AppSettings["Name"]);
18
Console.Read();
19
}
20
}
21
}
很简单,找了n久的资料都没有具体说实现静态索引的BT办法.只能通过这样实现
1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Collections.Specialized;5

6
namespace TestTemp.ConsoleApp7


{8
public class Config9

{10

string[] keys = new string[]
{ "Name", "Age", "Location", "Email", "Key", "One", "Two", "Three", "Four" };11
Dictionary<string, string> tmp = new Dictionary<string, string>();12
public Config()13

{14
for (int i = 0; i < keys.Length; i++)15

{16

17
tmp.Add(keys[i], keys[i].ToUpper());18
}19
}20
public int Count21

{22
get23

{24
return tmp.Keys.Count;25
}26
}27

28
public string this[string index]29

{30
get31

{32
if (tmp.ContainsKey(index))33

{34
return tmp[index];35
}36

else
{ return ""; }37

38
}39
}40

41
}42
}43

1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace TestTemp.ConsoleApp6


{7
public static class Test8

{9
public static Config AppSettings10

{11

get
{ return new Config(); }12
}13
}14
}15

1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.IO;5
using System.Management;6
using System.Text.RegularExpressions;7

8

9
namespace TestTemp.ConsoleApp10


{11
class Program12

{13

14
static void Main(string[] args)15

{16
17
Console.Write(Test.AppSettings["Name"]);18
Console.Read();19
} 20
}21
}很简单,找了n久的资料都没有具体说实现静态索引的BT办法.只能通过这样实现
H.Wong
-2010
浙公网安备 33010602011771号