Ejemplo n.º 1
0
        private static async Task <IEnumerable> ReadMultiple <TObj>(TypeReader reader, ITurnContext context, IEnumerable <string> args, IServiceProvider services)
        {
            var objs = new List <TObj>();

            foreach (var arg in args)
            {
                var read = await ReadSingle(reader, context, arg.Trim(), services).ConfigureAwait(false);

                if (read != null)
                {
                    objs.Add((TObj)read);
                }
            }
            return(objs.ToImmutableArray());
        }
Ejemplo n.º 2
0
        internal ParameterInfo(ParameterBuilder builder, CommandInfo command, CommandService service)
        {
            Command = command;

            Name        = builder.Name;
            Summary     = builder.Summary;
            IsOptional  = builder.IsOptional;
            IsRemainder = builder.IsRemainder;
            IsMultiple  = builder.IsMultiple;

            Type         = builder.ParameterType;
            DefaultValue = builder.DefaultValue;

            Preconditions = builder.Preconditions.ToImmutableArray();
            Attributes    = builder.Attributes.ToImmutableArray();

            _reader = builder.TypeReader;
        }
Ejemplo n.º 3
0
        internal static TypeReader GetTypeReader(CommandService service, Type paramType, Type typeReaderType, IServiceProvider services)
        {
            var        readers = service.GetTypeReaders(paramType);
            TypeReader reader  = null;

            if (readers != null)
            {
                if (readers.TryGetValue(typeReaderType, out reader))
                {
                    return(reader);
                }
            }

            //We dont have a cached type reader, create one
            reader = ReflectionUtils.CreateObject <TypeReader>(typeReaderType.GetTypeInfo(), service, services);
            service.AddTypeReader(paramType, reader, false);

            return(reader);
        }
Ejemplo n.º 4
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 nullable <see cref="TypeReader" /> 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">
        ///     Defines whether the <see cref="TypeReader"/> should replace the default one for <see cref="Type" /> if
        ///     it exists.
        /// </param>
        public void AddTypeReader(Type type, TypeReader reader, bool replaceDefault)
        {
            if (replaceDefault && HasDefaultTypeReader(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);
                }
            }
        }
        public static TypeReader Create(Type type, TypeReader reader)
        {
            var constructor = typeof(NullableTypeReader <>).MakeGenericType(type).GetTypeInfo().DeclaredConstructors.First();

            return((TypeReader)constructor.Invoke(new object[] { reader }));
        }
Ejemplo n.º 6
0
 /// <summary>
 ///     Adds a custom <see cref="TypeReader" /> to this <see cref="CommandService" /> for the supplied object
 ///     type.
 ///     If <typeparamref name="T" /> is a <see cref="ValueType" />, a nullable <see cref="TypeReader" /> will
 ///     also be added.
 /// </summary>
 /// <typeparam name="T">The object type to be read by the <see cref="TypeReader"/>.</typeparam>
 /// <param name="reader">An instance of the <see cref="TypeReader" /> to be added.</param>
 /// <param name="replaceDefault">
 ///     Defines whether the <see cref="TypeReader"/> should replace the default one for
 ///     <see cref="Type" /> if it exists.
 /// </param>
 public void AddTypeReader <T>(TypeReader reader, bool replaceDefault)
 => AddTypeReader(typeof(T), reader, replaceDefault);
Ejemplo n.º 7
0
 //Type Readers
 /// <summary>
 ///     Adds a custom <see cref="TypeReader" /> to this <see cref="CommandService" /> for the supplied object
 ///     type.
 ///     If <typeparamref name="T" /> is a <see cref="ValueType" />, a nullable <see cref="TypeReader" /> will
 ///     also be added.
 ///     If a default <see cref="TypeReader" /> exists for <typeparamref name="T" />, a warning will be logged
 ///     and the default <see cref="TypeReader" /> will be replaced.
 /// </summary>
 /// <typeparam name="T">The object type to be read by the <see cref="TypeReader"/>.</typeparam>
 /// <param name="reader">An instance of the <see cref="TypeReader" /> to be added.</param>
 public void AddTypeReader <T>(TypeReader reader)
 => AddTypeReader(typeof(T), reader);