Exemple #1
0
        private void _InvokeModuleMethod(string ModuleName, string FunctionName, NameValuePair[] variables, ModuleCallReturnFunction ReturnFunction, bool async, bool throwOnError, out object returnValue)
        {
            returnValue = null;
            Log.Trace("Attempting to invoke method " + FunctionName + " in Module " + ModuleName);
            bool found = false;

            foreach (IModule module in _modules)
            {
                if (module.ModuleName == ModuleName)
                {
                    found = true;
                    Log.Trace("Located module " + ModuleName + " in attempt to invoke method");
                    MethodInfo method     = null;
                    object[]   parameters = new object[0];
                    if (variables == null)
                    {
                        Log.Trace("Attempting to locate method " + FunctionName + " in module " + ModuleName + " without any parameters");
                        method = module.GetType().GetMethod(FunctionName, Type.EmptyTypes);
                    }
                    else
                    {
                        foreach (MethodInfo mi in module.GetType().GetMethods())
                        {
                            if (mi.Name == FunctionName)
                            {
                                bool contains = true;
                                Log.Trace("Located method " + mi.ToString() + " in module " + ModuleName + ", checking to see if variables were passed.");
                                foreach (ParameterInfo pi in mi.GetParameters())
                                {
                                    bool found_var = false;
                                    foreach (NameValuePair nvp in variables)
                                    {
                                        if (nvp.Name == pi.Name)
                                        {
                                            found_var = true;
                                            break;
                                        }
                                    }
                                    if (!found_var)
                                    {
                                        contains = false;
                                        break;
                                    }
                                }
                                if ((contains) && (mi.GetParameters().Length == variables.Length))
                                {
                                    Log.Trace("Located proper method in module invoke using parameters supplied");
                                    method = mi;
                                    Log.Trace("Constructing parameter list for module " + ModuleName + " method " + FunctionName);
                                    parameters = new object[variables.Length];
                                    int x = 0;
                                    foreach (ParameterInfo p in mi.GetParameters())
                                    {
                                        foreach (NameValuePair nvp in variables)
                                        {
                                            if (nvp.Name == p.Name)
                                            {
                                                parameters[x] = nvp.Value;
                                                break;
                                            }
                                        }
                                        x++;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    if (method == null)
                    {
                        Log.Error(new ModuleFunctionNotFoundException(ModuleName, FunctionName));
                        if (throwOnError)
                        {
                            throw new ModuleFunctionNotFoundException(ModuleName, FunctionName);
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        try
                        {
                            if (!async && ReturnFunction == null)
                            {
                                if (method.ReturnType.ToString().ToLower() == "void")
                                {
                                    Log.Trace("Invoking module method with no return");
                                    method.Invoke(module, parameters);
                                }
                                else
                                {
                                    Log.Trace("Invoking module method and collecting return value");
                                    returnValue = method.Invoke(module, parameters);
                                }
                            }
                            else
                            {
                                InvokeModuleCallFunction asyncCall = new InvokeModuleCallFunction(AsyncInvokeMethod);
                                if (async)
                                {
                                    Log.Trace("Attempting to invoke " + FunctionName + " in module " + ModuleName + " asynchronously.");
                                    asyncCall.BeginInvoke(method, module, parameters, ReturnFunction, new AsyncCallback(AsyncInvokeMethodComplete), null);
                                }
                                else
                                {
                                    Log.Trace("Attempting to invoke " + FunctionName + " in module " + ModuleName + " synchronously.");
                                    asyncCall.Invoke(method, module, parameters, ReturnFunction);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error(e);
                            if (throwOnError)
                            {
                                throw new ModuleFunctionCallException(e, ModuleName, FunctionName);
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                }
            }
            if (!found)
            {
                Log.Error(new ModuleNotFoundException(ModuleName));
                if (throwOnError)
                {
                    throw new ModuleNotFoundException(ModuleName);
                }
            }
        }
 private void _InvokeModuleMethod(string ModuleName,string FunctionName,NameValuePair[] variables,ModuleCallReturnFunction ReturnFunction,bool async,bool throwOnError,out object returnValue)
 {
     returnValue = null;
     Log.Trace("Attempting to invoke method " + FunctionName + " in Module " + ModuleName);
     bool found = false;
     foreach (IModule module in _modules)
     {
         if (module.ModuleName == ModuleName)
         {
             found = true;
             Log.Trace("Located module " + ModuleName+" in attempt to invoke method");
             MethodInfo method = null;
             object[] parameters = new object[0];
             if (variables == null)
             {
                 Log.Trace("Attempting to locate method " + FunctionName + " in module " + ModuleName + " without any parameters");
                 method = module.GetType().GetMethod(FunctionName, Type.EmptyTypes);
             }
             else
             {
                 foreach (MethodInfo mi in module.GetType().GetMethods()){
                     if (mi.Name == FunctionName)
                     {
                         bool contains = true;
                         Log.Trace("Located method " + mi.ToString() + " in module " + ModuleName + ", checking to see if variables were passed.");
                         foreach (ParameterInfo pi in mi.GetParameters())
                         {
                             bool found_var = false;
                             foreach (NameValuePair nvp in variables)
                             {
                                 if (nvp.Name == pi.Name)
                                 {
                                     found_var = true;
                                     break;
                                 }
                             }
                             if (!found_var)
                             {
                                 contains = false;
                                 break;
                             }
                         }
                         if ((contains)&&(mi.GetParameters().Length==variables.Length))
                         {
                             Log.Trace("Located proper method in module invoke using parameters supplied");
                             method = mi;
                             Log.Trace("Constructing parameter list for module " + ModuleName + " method " + FunctionName);
                             parameters = new object[variables.Length];
                             int x = 0;
                             foreach (ParameterInfo p in mi.GetParameters())
                             {
                                 foreach (NameValuePair nvp in variables)
                                 {
                                     if (nvp.Name == p.Name)
                                     {
                                         parameters[x] = nvp.Value;
                                         break;
                                     }
                                 }
                                 x++;
                             }
                             break;
                         }
                     }
                 }
             }
             if (method == null)
             {
                 Log.Error(new ModuleFunctionNotFoundException(ModuleName, FunctionName));
                 if (throwOnError)
                     throw new ModuleFunctionNotFoundException(ModuleName, FunctionName);
                 else
                     return;
             }
             else
             {
                 try
                 {
                     if (!async && ReturnFunction == null)
                     {
                         if (method.ReturnType.ToString().ToLower() == "void")
                         {
                             Log.Trace("Invoking module method with no return");
                             method.Invoke(module, parameters);
                         }
                         else
                         {
                             Log.Trace("Invoking module method and collecting return value");
                             returnValue = method.Invoke(module, parameters);
                         }
                     }
                     else
                     {
                         InvokeModuleCallFunction asyncCall = new InvokeModuleCallFunction(AsyncInvokeMethod);
                         if (async)
                         {
                             Log.Trace("Attempting to invoke " + FunctionName + " in module " + ModuleName + " asynchronously.");
                             asyncCall.BeginInvoke(method, module, parameters, ReturnFunction, new AsyncCallback(AsyncInvokeMethodComplete), null);
                         }
                         else
                         {
                             Log.Trace("Attempting to invoke " + FunctionName + " in module " + ModuleName + " synchronously.");
                             asyncCall.Invoke(method, module, parameters, ReturnFunction);
                         }
                     }
                 }
                 catch (Exception e)
                 {
                     Log.Error(e);
                     if (throwOnError)
                         throw new ModuleFunctionCallException(e, ModuleName, FunctionName);
                     else
                         return;
                 }
             }
         }
     }
     if (!found)
     {
         Log.Error(new ModuleNotFoundException(ModuleName));
         if (throwOnError)
             throw new ModuleNotFoundException(ModuleName);
     }
 }