private static MessageParameterAttribute ConvertFromServiceModelMessageParameterAttribute(object attr)
        {
            var    messageParameter = new MessageParameterAttribute();
            string tmpStr           = GetProperty <string>(attr, nameof(MessageParameterAttribute.Name));

            if (!string.IsNullOrEmpty(tmpStr))
            {
                messageParameter.Name = tmpStr;
            }

            return(messageParameter);
        }
        static MessagePartDescription CreatePartCore(
            MessageParameterAttribute mpa, string defaultName,
            string defaultNamespace)
        {
            string pname = null;

            if (mpa != null && mpa.Name != null)
            {
                pname = mpa.Name;
            }
            if (pname == null)
            {
                pname = defaultName;
            }
            return(new MessagePartDescription(pname, defaultNamespace));
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the wire parameter name, which may be overridden by a <see cref="MessageParameterAttribute"/>.
        /// </summary>
        /// <param name="parameter">The parameter to get the wire version of the parameter name of.</param>
        /// <returns>The wire name of the parameter.</returns>
        private static string GetWireParameterName(ParameterInfo parameter)
        {
            string name = parameter.Name;

            object[] attrs = parameter.GetCustomAttributes(true);
            foreach (object attr in attrs)
            {
                MessageParameterAttribute mpa = attr as MessageParameterAttribute;
                if (mpa != null)
                {
                    name = mpa.Name;
                    break;
                }
            }

            return(name);
        }
Esempio n. 4
0
        private NameValueCollection GetParameters(MethodInfo methodInfo, IMethodCallMessage message, HttpMethod verb)
        {
            ParameterInfo[] parameterInfos = methodInfo.GetParameters();

            if (parameterInfos.Length != message.Args.Length)
            {
                throw new InvalidOperationException("Number of parameters does not match the method signature.");
            }

            if (parameterInfos.Length == 0)
            {
                return(null);
            }

            NameValueCollection parameters = new NameValueCollection(message.Args.Length);

            for (int i = 0; i < parameterInfos.Length; i++)
            {
                object current = message.Args[i];

                if (current == null)
                {
                    continue;
                }

                ParameterInfo parameterInfo = parameterInfos[i];
                Type          type          = parameterInfo.ParameterType;
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    type = type.GetGenericArguments()[0];
                }
                if (type.IsPrimitive || type == typeof(string) || type == typeof(Guid) || type.IsEnum || type.IsArray)
                {
                    string name = parameterInfo.Name;

                    MessageParameterAttribute attribute = (MessageParameterAttribute)Attribute.GetCustomAttribute(parameterInfo, typeof(MessageParameterAttribute));

                    if (attribute != null && !string.IsNullOrEmpty(attribute.Name))
                    {
                        name = attribute.Name;
                    }

                    string value = !parameterInfo.ParameterType.IsArray ?
                                   current.ToString() :
                                   string.Join(",", ((IEnumerable)current).Cast <object>().ToArray());

                    parameters.Add(name, value);
                }
                else if (type.IsClass && (verb == HttpMethod.Get || verb == HttpMethod.Delete))
                {
                    foreach (PropertyInfo propertyInfo in parameterInfo.ParameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        if (!propertyInfo.CanRead || !propertyInfo.CanWrite)
                        {
                            continue;
                        }

                        Type propertyType = propertyInfo.PropertyType;

                        if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            propertyType = propertyType.GetGenericArguments()[0];
                        }

                        object objectValue = propertyInfo.GetValue(current, null);

                        if (objectValue == null)
                        {
                            continue;
                        }

                        if (propertyType.IsPrimitive || propertyType == typeof(string) || propertyType == typeof(Guid) || propertyType.IsEnum || propertyType.IsArray)
                        {
                            DataMemberAttribute attribute = (DataMemberAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(DataMemberAttribute));
                            string name = attribute != null && !string.IsNullOrEmpty(attribute.Name)
                                                                                          ? attribute.Name
                                                                                          : propertyInfo.Name;

                            string value = !propertyType.IsArray
                                                                                           ? objectValue.ToString()
                                                                                           : string.Join(",", ((IEnumerable)objectValue).Cast <object>());

                            parameters.Add(name, value);
                        }
                        else if (propertyType.IsClass && typeof(IList).IsAssignableFrom(propertyType))
                        {
                            DataMemberAttribute attribute = (DataMemberAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(DataMemberAttribute));
                            string name = attribute != null && !string.IsNullOrEmpty(attribute.Name)
                                                                                          ? attribute.Name
                                                                                          : propertyInfo.Name;

                            IList list = (IList)objectValue;

                            string value = string.Join(",", list.Cast <object>());

                            parameters.Add(name, value);
                        }
                    }
                }
            }

            return(parameters);
        }