using System.Collections.Generic; using System.Linq; using MongoDB.Bson.Serialization.Attributes; namespace Base { /// /// Key Value组件用于保存一些数据 /// public class KVComponent : Component where T: Entity { [BsonElement] private readonly Dictionary kv = new Dictionary(); public void Add(string key, object value) { this.kv.Add(key, value); } public void Remove(string key) { this.kv.Remove(key); } public K Get(string key) { object k; if (!this.kv.TryGetValue(key, out k)) { return default(K); } return (K)k; } public override void Dispose() { if (this.Id == 0) { return; } base.Dispose(); } } public static class KVHelper { public static void Add(this Entity entity, string key, T value) where T : Entity { entity.GetComponent>().Add(key, value); } public static void Remove(this Entity entity, string key) where T : Entity { entity.GetComponent>().Remove(key); } public static void Get(this Entity entity, string key) where T : Entity { entity.GetComponent>().Get(key); } } }