Click here to Skip to main content
13,042,838 members (78,231 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 17 Mar 2011

Intercept Method Calls in C# based on Attributes

, 18 Mar 2011
Rate this:
Please Sign up or sign in to vote.
Interceping Method Calls based on Attributes
See[^] For more information on the problem at hand and other solutions...
My Solution is as follows

public object InvokeMethod(string methodName, Dictionary<string, object> methodArguments)
    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());
    return null;
   public void BroadcastMessage(string Message)
       //MessageBus.GetInstance().SendAll("<span class='system'>Web Service Broadcast: <b>" + Message + "</b></span>");
       InvokeMethod("BroadcastMessage", new Dictionary<string, object>() { {"Message", Message} });
   void _BroadcastMessage(string Message)
       MessageBus.GetInstance().SendAll("<span class='system'>Web Service Broadcast: <b>" + Message + "</b></span>");


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


About the Author

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...


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    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Mar 2011
Article Copyright 2011 by jfriedman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid