Exemplo n.º 1
0
        public HandlerTypeDescriptor(Type handlerType)
        {
            var method = MethodAttribute.From(handlerType) !;

            Method    = method.Method;
            Direction = method.Direction;
            if (handlerType.IsGenericTypeDefinition && handlerType.IsPublic)
            {
                var parameter   = handlerType.GetTypeInfo().GenericTypeParameters[0];
                var constraints = parameter.GetGenericParameterConstraints();
                if (constraints.Length == 1)
                {
                    handlerType = handlerType.MakeGenericType(handlerType.GetTypeInfo().GenericTypeParameters[0].GetGenericParameterConstraints()[0]);
                }
            }

            HandlerType   = handlerType;
            InterfaceType = HandlerTypeDescriptorHelper.GetHandlerInterface(handlerType);

            // This allows for us to have derived types
            // We are making the assumption that interface given here
            // if a GTD will have a constraint on the first generic type parameter
            // that is the real base type for this interface.
            if (InterfaceType.IsGenericType)
            {
                ParamsType = InterfaceType.GetGenericArguments()[0];
            }

            HasParamsType  = ParamsType != null;
            IsNotification = handlerType
                             .GetInterfaces()
                             .Any(z => z.IsGenericType && typeof(IJsonRpcNotificationHandler <>).IsAssignableFrom(z.GetGenericTypeDefinition()));
            IsRequest = !IsNotification;

            var requestInterface = ParamsType?
                                   .GetInterfaces()
                                   .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IRequest <>));

            if (requestInterface != null)
            {
                ResponseType = requestInterface.GetGenericArguments()[0];
            }
            HasResponseType = ResponseType != null && ResponseType != typeof(Unit);

            var processAttributes = HandlerType
                                    .GetCustomAttributes(true)
                                    .Concat(HandlerType.GetCustomAttributes(true))
                                    .Concat(InterfaceType.GetInterfaces().SelectMany(x => x.GetCustomAttributes(true)))
                                    .Concat(HandlerType.GetInterfaces().SelectMany(x => x.GetCustomAttributes(true)))
                                    .OfType <ProcessAttribute>()
                                    .ToArray();

            RequestProcessType = processAttributes
                                 .FirstOrDefault()?.Type;
        }
        /// <summary>
        /// Parses and assigns a value to a field
        /// </summary>
        /// <param name="TargetObject">The target object to assign values to</param>
        /// <param name="Field">The field to assign the value to</param>
        /// <param name="ArgumentText">The full argument text</param>
        /// <param name="ValueText">Argument text</param>
        /// <param name="PreviousArgumentText">The previous text used to configure this field</param>
        /// <returns>True if the value was assigned to the field, false otherwise</returns>
        private static bool ApplyArgument(object TargetObject, FieldInfo Field, string ArgumentText, string ValueText, string PreviousArgumentText)
        {
            // The value type for items of this field
            Type ValueType = Field.FieldType;

            // Check if the field type implements ICollection<>. If so, we can take multiple values.
            Type CollectionType = null;

            foreach (Type InterfaceType in Field.FieldType.GetInterfaces())
            {
                if (InterfaceType.IsGenericType && InterfaceType.GetGenericTypeDefinition() == typeof(ICollection <>))
                {
                    ValueType      = InterfaceType.GetGenericArguments()[0];
                    CollectionType = InterfaceType;
                    break;
                }
            }

            // Try to parse the value
            object Value;

            if (!TryParseValue(ValueType, ValueText, out Value))
            {
                Log.TraceWarning("Unable to parse value for argument '{0}'.", ArgumentText);
                return(false);
            }

            // Try to assign values to the target field
            if (CollectionType == null)
            {
                // Check if this field has already been assigned to. Output a warning if the previous value is in conflict with the new one.
                if (PreviousArgumentText != null)
                {
                    object PreviousValue = Field.GetValue(TargetObject);
                    if (!PreviousValue.Equals(Value))
                    {
                        Log.TraceWarning("Argument '{0}' conflicts with '{1}'; ignoring.", ArgumentText, PreviousArgumentText);
                    }
                    return(false);
                }

                // Set the value on the target object
                Field.SetValue(TargetObject, Value);
                return(true);
            }
            else
            {
                // Call the 'Add' method on the collection
                CollectionType.InvokeMember("Add", BindingFlags.InvokeMethod, null, Field.GetValue(TargetObject), new object[] { Value });
                return(true);
            }
        }
Exemplo n.º 3
0
        public HandlerTypeDescriptor(Type handlerType)
        {
            var method = handlerType.GetCustomAttribute <MethodAttribute>();

            Method        = method.Method;
            Direction     = method.Direction;
            HandlerType   = handlerType;
            InterfaceType = HandlerTypeDescriptorHelper.GetHandlerInterface(handlerType);

            ParamsType    = InterfaceType.IsGenericType ? InterfaceType.GetGenericArguments()[0] : typeof(EmptyRequest);
            HasParamsType = ParamsType != null && ParamsType != typeof(EmptyRequest);

            IsNotification = typeof(IJsonRpcNotificationHandler).IsAssignableFrom(handlerType) || handlerType
                             .GetInterfaces().Any(z =>
                                                  z.IsGenericType && typeof(IJsonRpcNotificationHandler <>).IsAssignableFrom(z.GetGenericTypeDefinition()));
            IsRequest = !IsNotification;

            var requestInterface = ParamsType?
                                   .GetInterfaces()
                                   .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IRequest <>));

            if (requestInterface != null)
            {
                ResponseType = requestInterface.GetGenericArguments()[0];
            }
            HasResponseType = ResponseType != null && ResponseType != typeof(Unit);

            var processAttributes = HandlerType
                                    .GetCustomAttributes(true)
                                    .Concat(HandlerType.GetCustomAttributes(true))
                                    .Concat(InterfaceType.GetInterfaces().SelectMany(x => x.GetCustomAttributes(true)))
                                    .Concat(HandlerType.GetInterfaces().SelectMany(x => x.GetCustomAttributes(true)))
                                    .OfType <ProcessAttribute>()
                                    .ToArray();

            RequestProcessType = processAttributes
                                 .FirstOrDefault()?.Type;
        }