Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

Build ReST based Web Services in .NET/C#

, 12 Jul 2009
A ReST based Web Service for C#.
PoC.Web.ReSTService.zip
PoC.Web.ReSTService
obj
Debug
PoC.Web.ReSTService.dll
Refactor
PoC.Web.ReSTService.dll
Properties
PoC_Web_ReSTService.zip
PoC.Web.ReSTService.dll
PoC.Web.ReSTService.dll
TestWebApp.zip
tstWebApplication1
App_Data
bin
PoC.Web.ReSTService.dll
tstWebApplication1.dll
Properties
tstWebApplication1.csproj.user
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace PoC.Web.Services.Utilities
{
    internal class ReflectionUtil
    {
        public static MethodInfo FindMethod(Type inType, string methodName, object[] args, HttpVerb verb)
        {
            int matchMethodCounter = 0;
            MethodInfo retMethod = null;
            foreach (System.Reflection.MethodInfo method in inType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase))
            {
                bool isMatch = true;
                bool isExactMatch = true;
                try
                {
                    if (method.GetCustomAttributes(typeof(ReSTMethod), false).Length == 0) continue;
                    bool isVerbSupported = false;
                    foreach (ReSTMethod methodAttrib in (ReSTMethod[])method.GetCustomAttributes(typeof(ReSTMethod), false))
                    {
                        isVerbSupported |= methodAttrib.Verb == verb;
                    }
                    if (!isVerbSupported) continue;

                    if (string.Compare(method.Name, methodName, StringComparison.OrdinalIgnoreCase) != 0) continue;
                    ParameterInfo[] methodParam = method.GetParameters();
                    if (methodParam.Length == 0 && args.Length == 0) return method;
                    //if (methodParam.Length == 0) throw new InvalidOperationException("Method doesn't support any parameter");
                    //No support for "params"
                    if (methodParam[methodParam.Length - 1].GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0)
                        throw new InvalidOperationException("Param Array not allowed.");
                    if (methodParam.Length > args.Length)
                    {
                        object[] resizeArgs = new object[args.Length];
                        System.Array.Copy(args, resizeArgs, args.Length);
                        args = resizeArgs;
                    }


                    for (int i = 0; i < methodParam.Length; i++)
                    {
                        Type paramType = methodParam[i].ParameterType;
                        object paramValue = args[i];
                        if ((paramValue == null && paramType.IsValueType)
                            || (paramValue != null && !paramType.IsAssignableFrom(paramValue.GetType())))
                        {
                            isMatch = false;
                            break;
                        }
                        if (paramValue == null || paramType != paramValue.GetType())
                        {
                            isExactMatch = false;
                        }
                    }
                }
                catch (InvalidCastException)
                {
                    isMatch = false;
                }
                if (isMatch)
                {
                    if(isExactMatch)
                        return method;
                    ++matchMethodCounter;
                    retMethod = method;
                    if (matchMethodCounter > 0) throw new AmbiguousMatchException("Ambiguous action.");
                }
                
            }
            return retMethod;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Parag.Gadkari

United States United States
Loves coding...

You may also be interested in...

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 12 Jul 2009
Article Copyright 2009 by Parag.Gadkari
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid