Click here to Skip to main content
11,710,472 members (90,078 online)
Click here to Skip to main content

Tagged as

Intercept Method Calls in C# based on Attributes

, 18 Mar 2011 CPOL 7.4K 1
Rate this:
Please Sign up or sign in to vote.
Interceping Method Calls based on Attributes
See http://stackoverflow.com/questions/25803/how-do-i-intercept-a-method-call-in-c/5345043#5345043[^] For more information on the problem at hand and other solutions...
My Solution is as follows

[WebMethod]
public object InvokeMethod(string methodName, Dictionary<string, object> methodArguments)
{
  try
  {
    string lowerMethodName = '_' + methodName.ToLowerInvariant();
    List<object> tempParams = new List<object>();
    foreach (MethodInfo methodInfo in serviceMethods.Where(methodInfo => methodInfo.Name.ToLowerInvariant() == lowerMethodName))
    {
      ParameterInfo[] parameters = methodInfo.GetParameters();
      if (parameters.Length != methodArguments.Count()) continue;
      else foreach (ParameterInfo parameter in parameters)
      {
        object argument = null;
        if (methodArguments.TryGetValue(parameter.Name, out argument))
        {
          if (parameter.ParameterType.IsValueType)
          {
            System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(parameter.ParameterType);
            argument = tc.ConvertFrom(argument);
 
          }
          tempParams.Insert(parameter.Position, argument);
 
        }
        else goto ContinueLoop;
      }
 
      foreach (object attribute in methodInfo.GetCustomAttributes(true))
      {
        if (attribute is YourAttributeClass)
        {
          RequiresPermissionAttribute attrib = attribute as YourAttributeClass;
          YourAttributeClass.YourMethod();//Mine throws an ex
        }
      }
 
      return methodInfo.Invoke(this, tempParams.ToArray());
ContinueLoop:
      continue;
    }
    return null;
  }
  catch
  {
    throw;
  }
}
[WebMethod]
   public void BroadcastMessage(string Message)
   {
       //MessageBus.GetInstance().SendAll("<span class='system'>Web Service Broadcast: <b>" + Message + "</b></span>");
       //return;
       InvokeMethod("BroadcastMessage", new Dictionary<string, object>() { {"Message", Message} });
   }
 
   [RequiresPermission("editUser")]
   void _BroadcastMessage(string Message)
   {
       MessageBus.GetInstance().SendAll("<span class='system'>Web Service Broadcast: <b>" + Message + "</b></span>");
       return;
   }

License

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

Share

About the Author

jfriedman
Software Developer (Senior) ASTI Transportation Inc.
United States United States
Livin in a lonely world, caught the midnight train going anywhere... Only thing is it was a runaway train... and it ain't ever goin back...

v//

You may also be interested in...

Comments and Discussions

 
GeneralNot quite user friendly Pin
Steve Wellens19-Mar-11 2:28
memberSteve Wellens19-Mar-11 2:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 18 Mar 2011
Article Copyright 2011 by jfriedman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid