代码一:存储变量和常量的Class.
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Collections.Specialized;
5
6namespace 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
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace 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
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using System.Management;
6using System.Text.RegularExpressions;
7
8
9namespace 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办法.只能通过这样实现