| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using MongoDB.Bson.Serialization.Attributes;
- namespace Base
- {
- public class Env: Object
- {
- [BsonElement, BsonIgnoreIfNull]
- private Dictionary<string, object> values = new Dictionary<string, object>();
- public object this[string key]
- {
- get
- {
- return this.values[key];
- }
- set
- {
- if (this.values == null)
- {
- this.values = new Dictionary<string, object>();
- }
- this.values[key] = value;
- }
- }
- public T Get<T>(string key)
- {
- if (this.values == null || !this.values.ContainsKey(key))
- {
- return default(T);
- }
- object value = values[key];
- try
- {
- return (T)value;
- }
- catch (InvalidCastException e)
- {
- throw new GameException($"不能把{value.GetType()}转换为{typeof(T)}", e);
- }
- }
- public void Set(string key, object obj)
- {
- if (this.values == null)
- {
- this.values = new Dictionary<string, object>();
- }
- this.values[key] = obj;
- }
- public bool ContainKey(string key)
- {
- if (this.values == null)
- {
- return false;
- }
- return this.values.ContainsKey(key);
- }
- public void Remove(string key)
- {
- if (this.values == null)
- {
- return;
- }
- this.values.Remove(key);
- if (this.values.Count == 0)
- {
- this.values = null;
- }
- }
- public void Add(string key, object value)
- {
- if (this.values == null)
- {
- this.values = new Dictionary<string, object>();
- }
- this.values[key] = value;
- }
- public IEnumerator GetEnumerator()
- {
- return this.values.GetEnumerator();
- }
- }
- }
|