AddHook() public static method

public static AddHook ( string name, object>.Func action ) : void
name string
action object>.Func
return void
Exemplo n.º 1
0
    private static void InitModules()
    {
        hooks = new List <KeyValuePair <string, Func <object, object> > >();
        var modules = GetActivatedModules().ToList();

        modules.ForEach(module =>
        {
            try
            {
                Log.Write("SiteEngine: Start Loading Module {0}", module);

                Type moduleType = Type.GetType(module, true);

                //Init function is obsolete
                var initMmethod = moduleType.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
                                  .Where(m => m.Name == "Init").FirstOrDefault();
                if (initMmethod != null)
                {
                    moduleType.InvokeMember("Init", BindingFlags.InvokeMethod, null, moduleType, new object[0]);
                }

                //Use Hook attribute
                var methods = moduleType.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
                              .Where(m => m.GetCustomAttributes(typeof(HookAttribute), true).Count() > 0);

                foreach (var method in methods)
                {
                    foreach (var attr in method.GetCustomAttributes(typeof(HookAttribute), true))
                    {
                        var func = Delegate.CreateDelegate(typeof(Func <object, object>), method) as Func <object, object>;
                        SiteEngine.AddHook(((HookAttribute)attr).Name ?? method.Name, func);
                    }
                }

                Log.Write("SiteEngine: Done Loading Module {0}\r\n", module);
            }
            catch (Exception ex)
            {
                Log.Write("SiteEngine: Failed Loading Module: {0}: Error: {1}\r\n", module, ex.Message);
            }
        });
    }