Example #1
0
 public void RegisterEventHandlerImplicitly(object eventHandler)
 {
     foreach (var type in eventHandler.GetType().GetInterfaces().Where(
                  contract => contract.GetGenericTypeDefinition() == typeof(IHandleDomainEvent <>)))
     {
         var eventHandlers = eventHandlerLists.GetOrAdd(type.GetGenericArguments()[0], (typeId) => new List <object> {
         });
         eventHandlers.Add(eventHandler);
     }
 }
Example #2
0
        public static void AddBigtxtMsg(BigBody bigBody, Action <string> addBigtxtMsgCallback)
        {
            BigtxtHelper helper = bigtxtDic.GetOrAdd(bigBody.partName, new BigtxtHelper()
            {
                partName = bigBody.partName
            });

            helper.callBack += addBigtxtMsgCallback;

            if (!helper.bodyList.Any(b => b.partOrder == bigBody.partOrder))
            {
                helper.bodyList.Add(bigBody);
            }
            if (helper.bodyList.Count == helper.bodyList.First().partTotal)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < helper.bodyList.Count; i++)
                {
                    var body = helper.bodyList.First(b => b.partOrder == i);
                    sb.Append(body.text);
                }
                helper.CancellationTokenSource.Cancel();
                helper.callBack?.Invoke(sb.ToString());
                // bigtxtDic.TryRemove(bigBody.partName, out helper);
            }
            else
            {
                if (!helper.IsWatched)
                {
                    helper.RaiseTimeoutHandle(helper, addBigtxtMsgCallback);
                }
            }
        }
        static void Main(string[] args)
        {
            Task.Factory.StartNew(AddParis).Wait();
            AddParis();

            capitals["Russia"] = "Leningrad";
            capitals.AddOrUpdate("Russia", "Moscow", (key, oldValue) => $"old value was {oldValue} now is => Moscow");
            Console.WriteLine($"The capital of Russia is {capitals["Russia"]}");

            //capitals["Sweden"] = "Upsala";
            string cptSweden = capitals.GetOrAdd("Sweden", "Stockholm");

            Console.WriteLine($"The capital of Sweden is {cptSweden}");

            const string toRemove  = "Russia";
            string       removed   = null;
            bool         isRemoved = capitals.TryRemove(toRemove, out removed);

            if (isRemoved)
            {
                Console.WriteLine($"We removed {removed}");
            }
            else
            {
                Console.WriteLine($"Missing key with value {toRemove} in capitals dicitionary");
            }
        }
Example #4
0
        /// <summary>
        /// 将dynamic类型的对象传递到view页面
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        internal static object ToDynamic(object entity)
        {
            var entityType  = entity.GetType();
            var dynamicType = s_dynamicTypes.GetOrAdd(entityType, s_dynamicTypeCreator);

            var dynamicObject = Activator.CreateInstance(dynamicType);

            foreach (var entityProperty in entityType.GetProperties())
            {
                var value = entityProperty.GetValue(entity, null);
                dynamicType.GetField(entityProperty.Name).SetValue(dynamicObject, value);
            }

            return(dynamicObject);
        }
        private void SetDecoderIfMissing(string typeId, string label, string schemaString, IDictionary <string, IList <string> > properties)
        {
            // If the table is a collection, it does not have a proper type so ignore it

            if (typeId == "<collection>")
            {
                return;
            }

            knownTypes.GetOrAdd(typeId, (s) =>
            {
                return(new KineticaType(label, schemaString, properties));
            });
            typeNameLookup[label] = typeId;
        }
Example #6
0
        void SetUpPropertyCache(IList itemsSource)
        {
            var typeArg = itemsSource.GetType().GenericTypeArguments;

            if (typeArg.Count() == 0)
            {
                throw new ArgumentException("ItemsSource must be GenericType.");
            }

            // If the type is a system built-in-type, it doesn't create GetProperty.
            if (IsBuiltInType(typeArg[0]))
            {
                _getters = null;
                return;
            }

            _getters = DisplayValueCache.GetOrAdd(typeArg[0], CreateGetProperty);
        }
Example #7
0
        private string GetScript(string name) => _scripts.GetOrAdd(name,
                                                                   key =>
        {
            var resourceNames      = s_assembly.GetManifestResourceNames();
            var resourceStreamName = $"{typeof(SqliteStreamStore).Namespace}.Scripts.{key}.sql";
            using (var stream = s_assembly.GetManifestResourceStream(resourceStreamName))
            {
                if (stream == null)
                {
                    throw new System.Exception($"Embedded resource, {name}, not found. BUG!");
                }

                using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
                {
                    return(reader
                           .ReadToEnd());
                }
            }
        });
        /// <summary>
        /// Creates a memoizer with one parameter for the the specified task provider. The task provider is guaranteed to be executed only once per parameter instance.
        /// </summary>
        /// <typeparam name="TResult">The return value type</typeparam>
        /// <typeparam name="TParam"></typeparam>
        /// <param name="func">A function that will call the create the task.</param>
        /// <returns>A function that will return a task </returns>
        public static FuncAsync <TParam, TResult> AsMemoized <TParam, TResult>(this FuncAsync <TParam, TResult> func)
        {
#if HAS_NO_CONCURRENT_DICT
            var values = new SynchronizedDictionary <TParam, FuncAsync <TResult> >();
#else
            var values = new System.Collections.Concurrent.ConcurrentDictionary <TParam, FuncAsync <TResult> >();
#endif
            // It's safe to use default(TParam) as this won't get called anyway if TParam is a value type.
            var nullValue = Funcs.CreateAsyncMemoized(ct => func(ct, default(TParam)));

            return((ct, param) =>
            {
                if (param == null)
                {
                    return nullValue(ct);
                }
                else
                {
                    var memoizedFunc = values.GetOrAdd(param, p => Funcs.CreateAsyncMemoized(c => func(c, p)));

                    return memoizedFunc(ct);
                }
            });
        }