예제 #1
0
        internal void AddNullableTypeReader(Type valueType, TypeReader valueTypeReader)
        {
            var readers        = _typeReaders.GetOrAdd(typeof(Nullable <>).MakeGenericType(valueType), x => new ConcurrentDictionary <Type, TypeReader>());
            var nullableReader = NullableTypeReader.Create(valueType, valueTypeReader);

            readers[nullableReader.GetType()] = nullableReader;
        }
예제 #2
0
        public CommandService(CommandServiceConfig config)
        {
            _caseSensitive   = config.CaseSensitiveCommands;
            _throwOnError    = config.ThrowOnError;
            _ignoreExtraArgs = config.IgnoreExtraArgs;
            _separatorChar   = config.SeparatorChar;
            _defaultRunMode  = config.DefaultRunMode;

            //_logManager = new LogManager(config.LogLevel);
            //_logManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false);
            //_cmdLogger = _logManager.CreateLogger("Command");


            _moduleLock      = new SemaphoreSlim(1, 1);
            _typedModuleDefs = new ConcurrentDictionary <Type, ModuleInfo>();
            _moduleDefs      = new HashSet <ModuleInfo>();
            _map             = new CommandMap(this);
            _typeReaders     = new ConcurrentDictionary <Type, ConcurrentDictionary <Type, TypeReader> >();

            _defaultTypeReaders = new ConcurrentDictionary <Type, TypeReader>();
            foreach (var type in PrimitiveParsers.SupportedTypes)
            {
                _defaultTypeReaders[type] = PrimitiveTypeReader.Create(type);
                _defaultTypeReaders[typeof(Nullable <>).MakeGenericType(type)] = NullableTypeReader.Create(type, _defaultTypeReaders[type]);
            }

            _defaultTypeReaders[typeof(string)] =
                new PrimitiveTypeReader <string>((string x, out string y) => { y = x; return(true); }, 0);

            var entityTypeReaders = ImmutableList.CreateBuilder <Tuple <Type, Type> >();

            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(Item), typeof(ItemTypeReader <>)));
            entityTypeReaders.Add(new Tuple <Type, Type>(typeof(User), typeof(UserTypeReader <>)));
            _entityTypeReaders = entityTypeReaders.ToImmutable();
        }
예제 #3
0
        /// <summary>
        /// Adds a custom <see cref="TypeReader"/> to this <see cref="CommandService"/> for the supplied object type.
        /// If <paramref name="type"/> is a <see cref="ValueType"/>, a <see cref="NullableTypeReader{T}"/> for the value type will also be added.
        /// </summary>
        /// <param name="type">A <see cref="Type"/> instance for the type to be read.</param>
        /// <param name="reader">An instance of the <see cref="TypeReader"/> to be added.</param>
        /// <param name="replaceDefault">If <paramref name="reader"/> should replace the default <see cref="TypeReader"/> for <paramref name="type"/> if one exists.</param>
        public void AddTypeReader(Type type, TypeReader reader, bool replaceDefault)
        {
            if (replaceDefault && _defaultTypeReaders.ContainsKey(type))
            {
                _defaultTypeReaders.AddOrUpdate(type, reader, (k, v) => reader);
                if (type.GetTypeInfo().IsValueType)
                {
                    var nullableType   = typeof(Nullable <>).MakeGenericType(type);
                    var nullableReader = NullableTypeReader.Create(type, reader);
                    _defaultTypeReaders.AddOrUpdate(nullableType, nullableReader, (k, v) => nullableReader);
                }
            }
            else
            {
                var readers = _typeReaders.GetOrAdd(type, x => new ConcurrentDictionary <Type, TypeReader>());
                readers[reader.GetType()] = reader;

                if (type.GetTypeInfo().IsValueType)
                {
                    AddNullableTypeReader(type, reader);
                }
            }
        }