コード例 #1
0
        public static WADLParam CreateWADLParam(ParameterInfo parameter, ref XmlSchemaSet schemas, string encoding = Encodings.json)
        {
            WADLParam param;

            System.Type type = parameter.ParameterType;
            switch (System.Type.GetTypeCode(type))
            {
            case TypeCode.Object:    //we generate representations for complex types
                WADLRepresentation representation = WADLRepresentation.CreateWADLRepresentation(parameter, out schemas);
                param               = CreateWADLParam(parameter);
                param.Type          = representation.Path;
                param.Encoding      = encoding;
                param.ParameterInfo = parameter;
                break;

            //but we do not generate representations for primitive types.
            default:
                param = CreateWADLParam(parameter);
                break;
            }

            return(param);
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static WADLMethod[] GetWADLMethods(Type type, WADLParameterAddedHandler onWADLParameterAdded, WADLRepresentationAddedHandler onWADLRepresentationAdded)
        {
            State EmptyWADLMethodFlag = State.None;
            var   headers             = WADLParam.GetWADLParams(type);

            return((from method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                    select method).Where <MethodInfo>(delegate(MethodInfo member)
            {
                return WADLMethod.GetWADLMethod(member) != null;
            }).Aggregate <MethodInfo, List <WADLMethod> >(new List <WADLMethod>(), delegate(List <WADLMethod> list, MethodInfo member)
            {
                WADLMethod method = WADLMethod.GetWADLMethod(member);
                method.doc = WADLDoc.GetWADLDoc(member);

                if (String.IsNullOrEmpty(method.MethodName))
                {
                    if ((EmptyWADLMethodFlag & State.EmptyWADLMethod) == State.EmptyWADLMethod)
                    {
                        //throw new NotSupportedException("only one WADLMethod can be defined without a MethodName");
                    }
                    else
                    {
                        EmptyWADLMethodFlag = State.EmptyWADLMethod | State.EmptyWADLMethodCurrent;
                    }
                }

                WADLParam.GetWADLParams(method.MethodInfo, delegate(WADLParam wadlParam, XmlSchemaSet schemas)
                {
                    if ((EmptyWADLMethodFlag & State.EmptyWADLMethodCurrent) == State.EmptyWADLMethodCurrent && wadlParam.Style == ParamStyles.Template)
                    {
                        EmptyWADLMethodFlag |= State.EmptyWADLMethodCurrentWithWADLPathVariable;
                    }

                    if (method.Parameters.Exists(delegate(WADLParam param)
                    {
                        return param.Name.ToLower() == wadlParam.Name.ToLower() && param.Style == wadlParam.Style;
                    }))
                    {
                        throw new NotSupportedException(String.Format("Duplicate WADLParam {0}", wadlParam.Name));
                    }
                    else
                    {
                        method.Parameters.Add(wadlParam);
                        if (onWADLParameterAdded != null)
                        {
                            onWADLParameterAdded(wadlParam, schemas);
                        }
                    }
                });

                WADLParam[] methodheaders = WADLParam.GetWADLParams(member);
                headers.Union <object>(methodheaders.Except <object>(headers, ((IEqualityComparer <object>) new WADLParam()))
                                       ).Aggregate <object, List <WADLParam> >(method.Headers, delegate(List <WADLParam> aggregator, object header){
                    aggregator.Add((WADLParam)header);
                    return aggregator;
                });

                if ((EmptyWADLMethodFlag & State.EmptyWADLMethodCurrent) == State.EmptyWADLMethodCurrent)
                {
                    if ((EmptyWADLMethodFlag & State.EmptyWADLMethodCurrentWithWADLPathVariable) == State.EmptyWADLMethodCurrentWithWADLPathVariable)
                    {
                        EmptyWADLMethodFlag = EmptyWADLMethodFlag ^ State.EmptyWADLMethodCurrent;
                    }
                    //else //obsolete?
                    //throw new InvalidOperationException("WADLMethods without a MethodName must define at least one WADLParam Template");
                }

                WADLRepresentation.GetWADLRepresentations(member.ReturnParameter, delegate(WADLRepresentation rep, XmlSchemaSet schemas)
                {
                    method.Representations.Add(rep);
                    onWADLRepresentationAdded(rep, schemas);
                });

                WADLRepresentation.GetWADLRepresentations(member.DeclaringType, delegate(WADLRepresentation rep, XmlSchemaSet schemas)
                {
                    if (!method.Representations.Contains(rep, (IEqualityComparer <WADLRepresentation>) new WADLRepresentation()))
                    {
                        method.Representations.Add(rep);
                        onWADLRepresentationAdded(rep, schemas);
                    }
                });



                list.Add(method);
                return list;
            }).ToArray <WADLMethod>());
        }