コード例 #1
0
        public void FindTargets()
        {
            if (!initialized)
            {
                initialized = true;
            }
            else
            {
                return;
            }

            Log.Debug("Finding Dynvoke targets...");

            List <Type> attributedClasses = new List <Type> ();
            List <Type> parameterDynObjs  = new List <Type> ();

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (null != type.GetCustomAttribute <DynvokeClassAttribute> (true))
                    {
                        if (!type.IsAbstract || !type.IsSealed)
                        {
                            Log.Warn("Skipping [" + type.FullName + "] because it isn't static!");

                            continue;
                        }

                        attributedClasses.Add(type);
                    }
                }
            }

            foreach (Type attributedClass in attributedClasses)
            {
                string controllerName = GetControllerName(attributedClass);

                DynvokeClassAttribute classAttr = attributedClass.GetCustomAttribute <DynvokeClassAttribute> (true);

                Log.Debug("Found class [" + attributedClass.FullName + "]");

                if (classAttr.AltName != null)
                {
                    Log.Debug("Using name [" + classAttr.AltName + "]");
                }

                //static classes require static methods, dont look at object methods...
                foreach (MethodInfo method in attributedClass.GetMethods())
                {
                    if (method.DeclaringType != attributedClass)
                    {
                        continue;
                    }

                    DynvokeMethodAttribute methodAttr = method.GetCustomAttribute <DynvokeMethodAttribute> (true);

                    if (classAttr.AttributedMethodsOnly)
                    {
                        if (methodAttr == null)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (!method.IsPublic && methodAttr == null)
                        {
                            continue;
                        }
                    }

                    Log.Debug("  Method [" + method.Name + "]");

                    string actionName = GetActionName(method);
                    string key        = GetKey(controllerName, actionName);

                    if (methodAttr != null && methodAttr.AltName != null)
                    {
                        Log.Debug("  Using name [" + methodAttr.AltName + "]");
                    }

                    if (method.ReturnParameter.ParameterType == typeof(void))
                    {
                        Log.Debug("    void return");
                    }
                    else
                    {
                        Log.Debug("    returns [" + method.ReturnParameter.ParameterType.Name + "]");
                    }

                    Type returnType = method.ReturnParameter.ParameterType;

                    Dictionary <string, Type> Parameters         = new Dictionary <string, Type> ();
                    Dictionary <string, Type> InjectedParameters = new Dictionary <string, Type>();
                    List <string>             ParamOrder         = new List <string> ();

                    ParameterInfo[] methodParams = method.GetParameters();

                    if (methodParams.Length == 0)
                    {
                        Log.Debug("    no parameters");
                    }

                    foreach (ParameterInfo parameter in methodParams)
                    {
                        string paramName = parameter.Name.ToLower();
                        Type   paramType = parameter.ParameterType;

                        ParamOrder.Add(paramName);

                        if (this.ParamInjectors.ContainsKey(paramName) && this.ParamInjectors[paramName].Type == paramType)
                        {
                            Log.Debug("    [" + paramName + "] (" + paramType.Name + ") <- Injected");

                            InjectedParameters.Add(paramName, paramType);
                            continue;
                        }

                        if (this.ParamReplacersInternal.ContainsKey(paramName) && this.ParamReplacersInternal[paramName].InternalType == paramType)
                        {
                            ParamReplacmentContainer replCont = this.ParamReplacersInternal[paramName];

                            Log.Debug("    [" + paramName + "] (" + paramType.Name + ") <- Replaced with [" + replCont.ExternalType + "] (" + replCont.ExternalParamName + ")");

                            paramName = replCont.ExternalParamName;
                            paramType = replCont.ExternalType;
                        }
                        else
                        {
                            Log.Debug("    [" + paramName + "] (" + paramType.Name + ")");
                        }

                        if (null != paramType.GetCustomAttribute <DynvokeObjectAttribute>(true) && !parameterDynObjs.Contains(paramType))
                        {
                            parameterDynObjs.Add(paramType);
                        }

                        Parameters.Add(paramName, paramType);
                    }

                    DynvokeTarget target = new DynvokeTarget()
                    {
                        Method             = method,
                        InteralParamOrder  = ParamOrder,
                        ExternalParameters = Parameters,
                        InjectedParameters = InjectedParameters,

                        ControllerName = controllerName,
                        ActionName     = actionName
                    };

                    Targets.Add(key, target);
                }
            }

            foreach (Type paramDynObj in parameterDynObjs)
            {
                DynvokeObject dynObj = new DynvokeObject()
                {
                    Name = paramDynObj.Name
                };

                foreach (PropertyInfo prop in paramDynObj.GetProperties())
                {
                    dynObj.PropertyNamesAndTypes.Add(prop.Name, prop.PropertyType.Name);
                }

                this.DynvokeObjects.Add(dynObj);
            }

            Log.Debug("Completed Dynvoke search");
        }
コード例 #2
0
ファイル: DynvokeMethod.cs プロジェクト: nullfocus/Dynvoke
 public DynvokeMethod(DynvokeTarget target)
 {
     this.Target = target;
 }