/// <summary> /// only keys not matching with existent listable entity keys will be created /// </summary> /// <param name="opts"></param> /// <returns></returns> public virtual async Task AddMultipleAsync(Dictionary <string, TEntityOptionsBase> opts, string googleMapListableEntityTypeName) { if (opts.Count > 0) { Dictionary <string, JsObjectRef> jsObjectRefs = await _jsObjectRef.AddMultipleAsync( googleMapListableEntityTypeName, opts.ToDictionary(e => e.Key, e => (object)e.Value)); Dictionary <string, TEntityBase> objs = jsObjectRefs.ToDictionary(e => e.Key, e => { //Alternate if there are more constructors //var ctor = typeof(TEntityBase).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(c => !c.GetParameters().Any()); var ctor = typeof(TEntityBase).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(); BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance; return((TEntityBase)ctor.Invoke(new object[] { e.Value })); //Old version which didnt catched internal consturctors //return Activator.CreateInstance(typeof(TEntityBase), flags, null, e.Value) as TEntityBase; }); //Someone can try to create element yet inside listable entities... really not the best approach... but manage it List <string> alreadyCreated = BaseListableEntities.Keys.Intersect(objs.Select(e => e.Key)).ToList(); await RemoveMultipleAsync(alreadyCreated); //Now we can add all required object as NEW object foreach (string key in objs.Keys) { BaseListableEntities.Add(key, objs[key]); } } }
/// <summary> /// only keys not matching with existent listable entity keys will be created /// </summary> /// <param name="opts"></param> /// <returns></returns> public virtual async Task AddMultipleAsync(Dictionary <string, TEntityOptionsBase> opts, string googleMapListableEntityTypeName) { if (opts.Count > 0) { Dictionary <string, JsObjectRef> jsObjectRefs = await _jsObjectRef.AddMultipleAsync( googleMapListableEntityTypeName, opts.ToDictionary(e => e.Key, e => (object)e.Value)); Dictionary <string, TEntityBase> objs = jsObjectRefs.ToDictionary(e => e.Key, e => Activator.CreateInstance(typeof(TEntityBase), e.Value) as TEntityBase); //Someone can try to create element yet inside listable entities... really not the best approach... but manage it List <string> alreadyCreated = BaseListableEntities.Keys.Intersect(objs.Select(e => e.Key)).ToList(); await RemoveMultipleAsync(alreadyCreated); //Now we can add all required object as NEW object foreach (string key in objs.Keys) { BaseListableEntities.Add(key, objs[key]); } } }