Example #1
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;
        }
Example #2
0
        private 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);
        }
Example #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);
                }
            }
        }
Example #4
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 <see cref="NullableTypeReader{T}"/> 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">If <paramref name="reader"/> should replace the default <see cref="TypeReader"/> for <typeparamref name="T"/> if one exists.</param>
 public void AddTypeReader <T>(TypeReader reader, bool replaceDefault)
 => AddTypeReader(typeof(T), reader, replaceDefault);
Example #5
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 <see cref="NullableTypeReader{T}"/> 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);
        public static TypeReader Create(Type type, TypeReader reader)
        {
            var constructor = typeof(NullableTypeReader <>).MakeGenericType(type).GetTypeInfo().DeclaredConstructors.First();

            return((TypeReader)constructor.Invoke(new object[] { reader }));
        }