예제 #1
0
        public ObservableList <StandAloneAction> GetStandAloneActions()
        {
            ObservableList <StandAloneAction> list = new ObservableList <StandAloneAction>();

            foreach (PluginService pluginService in GetPluginServices())
            {
                foreach (MethodInfo MI in pluginService.mStandAloneMethods)
                {
                    GingerActionAttribute token = (GingerActionAttribute)Attribute.GetCustomAttribute(MI, typeof(GingerActionAttribute), false);
                    StandAloneAction      DA    = new StandAloneAction();
                    DA.ID = token.Id;
                    // AssemblyName AN = MI.DeclaringType.Assembly.GetName();
                    DA.PluginID    = PluginID; //AN.Name;
                    DA.ServiceID   = pluginService.ServiceId;
                    DA.Description = token.Description;
                    foreach (ParameterInfo PI in MI.GetParameters())
                    {
                        if (PI.ParameterType.Name != nameof(GingerAction))
                        {
                            DA.InputValues.Add(new ActionInputValueInfo()
                            {
                                Param = PI.Name, ParamType = PI.ParameterType
                            });
                        }
                    }
                    list.Add(DA);
                }
            }
            return(list);
        }
예제 #2
0
        private KeyValuePair <MethodInfo, ParameterInfo[]>[] GetMethods(Type Type)
        {
            List <KeyValuePair <MethodInfo, ParameterInfo[]> > Result = new List <KeyValuePair <MethodInfo, ParameterInfo[]> >();

            ParameterInfo[]          ParameterInfo;
            IEnumerable <MethodInfo> Methods = Type.GetRuntimeMethods();

            foreach (MethodInfo MI in Methods)
            {
                if (MI.Name != this.name)
                {
                    continue;
                }

                ParameterInfo = MI.GetParameters();
                if (ParameterInfo.Length != this.nrParameters)
                {
                    continue;
                }

                Result.Add(new KeyValuePair <MethodInfo, ParameterInfo[]>(MI, ParameterInfo));
            }

            return(Result.ToArray());
        }
예제 #3
0
        private static MethodInfo GetFindMethod()
        {
            Type T = typeof(Database);

            foreach (MethodInfo MI in T.GetTypeInfo().GetDeclaredMethods("Find"))
            {
                ParameterInfo[] P = MI.GetParameters();
                if (P.Length != 4 ||
                    P[0].ParameterType != typeof(int) ||
                    P[1].ParameterType != typeof(int) ||
                    P[2].ParameterType != typeof(Filter) ||
                    P[3].ParameterType != typeof(string[]))
                {
                    continue;
                }

                return(MI);
            }

            return(null);
        }
예제 #4
0
        void LoadPluginServicesFromDll()
        {
            if (mServices == null)                                    // Done only once
            {
                mServices = new ObservableList <PluginServiceInfo>(); // Move down !!!!!!!!!!!!!!!
                foreach (PluginAssemblyInfo pluginAssemblyInfo in mAssembliesInfo)
                {
                    var types2 = pluginAssemblyInfo.Assembly.GetExportedTypes();
                    IEnumerable <Type> types = from x in pluginAssemblyInfo.Assembly.GetTypes() where x.GetCustomAttribute(typeof(GingerServiceAttribute)) != null select x;
                    foreach (Type type in types)
                    {
                        GingerServiceAttribute gingerServiceAttribute = (GingerServiceAttribute)Attribute.GetCustomAttribute(type, typeof(GingerServiceAttribute), false);
                        bool IsServiceSession = typeof(IServiceSession).IsAssignableFrom(type);
                        // if service impl IServiceSession then mark it so we know we need and agent Start/Stop
                        // if it is not session , then all actions are stand alone

                        PluginServiceInfo pluginServiceInfo = new PluginServiceInfo()
                        {
                            ServiceId = gingerServiceAttribute.Id, IsSession = IsServiceSession
                        };
                        // expecting to get ExcelAction, FileAction, DatabaseAction...
                        MethodInfo[] methods    = type.GetMethods();
                        Type[]       interfaces = type.GetInterfaces();
                        foreach (MethodInfo MI in methods)
                        {
                            //Check if method have token [GingerAction] or is is of interface else we ignore
                            GingerActionAttribute gingerActionAttr = (GingerActionAttribute)Attribute.GetCustomAttribute(MI, typeof(GingerActionAttribute), false);

                            if (gingerActionAttr == null && !MI.IsVirtual)   // Virtual if it is interface
                            {
                                continue;
                            }


                            PluginServiceActionInfo action = new PluginServiceActionInfo();

                            if (gingerActionAttr != null)
                            {
                                action.ActionId    = gingerActionAttr.Id;
                                action.Description = gingerActionAttr.Description;
                            }
                            else
                            {
                                // Check if the method is from interface
                                if (MI.IsVirtual)
                                {
                                    foreach (Type serviceInterface in interfaces)
                                    {
                                        /// !!!!!!!!!!!!! see new style and remove !!!!!!!!!!!!!!!!
                                        // Not sure if we need to list all method if they come from interface !!!!!!!!!!!!!! need to list only the interface

                                        //check if marked with [GingerInterface]
                                        GingerInterfaceAttribute gingerInterfaceAttr = (GingerInterfaceAttribute)Attribute.GetCustomAttribute(serviceInterface, typeof(GingerInterfaceAttribute), false);

                                        if (gingerInterfaceAttr != null)
                                        {
                                            var mm = serviceInterface.GetMethod(MI.Name);
                                            if (mm != null)
                                            {
                                                // Get the action id and desc from the interface
                                                action.Interface = serviceInterface.FullName;

                                                GingerActionAttribute interfaceGAAttr = (GingerActionAttribute)Attribute.GetCustomAttribute(mm, typeof(GingerActionAttribute), false);
                                                if (interfaceGAAttr == null)
                                                {
                                                    // no GA attr then ignore
                                                    continue;
                                                }
                                                else
                                                {
                                                    action.ActionId    = interfaceGAAttr.Id;
                                                    action.Description = interfaceGAAttr.Description;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            if (string.IsNullOrEmpty(action.ActionId))
                            {
                                continue;
                            }

                            foreach (ParameterInfo PI in MI.GetParameters())
                            {
                                if (PI.ParameterType.Name == nameof(IGingerAction))
                                {
                                    continue;
                                }

                                ActionInputValueInfo actionInputValueInfo = new ActionInputValueInfo()
                                {
                                    Param = PI.Name, ParamType = PI.ParameterType
                                };
                                actionInputValueInfo.ParamAttrs = new List <Attribute>();
                                action.InputValues.Add(actionInputValueInfo);

                                // Add Ginger param properties

                                Attribute[] attrs = Attribute.GetCustomAttributes(PI, typeof(Attribute), false);


                                // var v = PI.CustomAttributes; - not good
                                foreach (Attribute attribute in attrs)
                                {
                                    actionInputValueInfo.ParamAttrs.Add(attribute);
                                }
                            }


                            pluginServiceInfo.Actions.Add(action);
                        }

                        // Get all interfaces which are marked with attr 'GingerInterface'
                        foreach (Type PluginInterface in interfaces)
                        {
                            // decide if we need Feature for service and/or Interfaces seperate
                            // ServiceFeatureAttribute gingerInterfaceAttr = (ServiceFeatureAttribute)Attribute.GetCustomAttribute(PluginInterface, typeof(ServiceFeatureAttribute), true);
                            GingerInterfaceAttribute gingerInterfaceAttr = (GingerInterfaceAttribute)Attribute.GetCustomAttribute(PluginInterface, typeof(GingerInterfaceAttribute), true);

                            if (gingerInterfaceAttr != null)
                            {
                                pluginServiceInfo.Interfaces.Add(gingerInterfaceAttr.Id);
                            }
                        }

                        MemberInfo[] members = type.GetMembers();

                        foreach (MemberInfo mi in members)
                        {
                            if (Attribute.GetCustomAttribute(mi, typeof(ServiceConfigurationAttribute), false) is ServiceConfigurationAttribute mconfig)
                            {
                                PluginServiceConfigInfo Config = new  PluginServiceConfigInfo();
                                Config.Name        = mconfig.Name;
                                Config.Description = mconfig.Description;
                                Config.Type        = mconfig.GetType().Name;
                                // Config.DefaultValue = mconfig.DefaultValue?.ToString();

                                if (Attribute.GetCustomAttribute(mi, typeof(ValidValueAttribute), false) is ValidValueAttribute validValues)
                                {
                                    foreach (var val in validValues.ValidValue)
                                    {
                                        Config.OptionalValues.Add(val.ToString());
                                    }
                                }
                                if (Attribute.GetCustomAttribute(mi, typeof(DefaultAttribute), false) is DefaultAttribute DefaultValue)
                                {
                                    Config.DefaultValue = DefaultValue == null ? string.Empty : DefaultValue.ToString();
                                }

                                pluginServiceInfo.Configs.Add(Config);
                            }
                        }
                        mServices.Add(pluginServiceInfo);
                    }
                }
            }
        }
예제 #5
0
        void LoadPluginServicesFromDll()
        {
            if (mServices == null)
            {
                mServices = new ObservableList <PluginServiceInfo>();
                foreach (PluginAssemblyInfo asssembly in mAssembliesInfo)
                {
                    IEnumerable <Type> types = from x in asssembly.Assembly.GetTypes() where x.GetCustomAttribute(typeof(GingerServiceAttribute)) != null select x;
                    foreach (Type type in types)
                    {
                        GingerServiceAttribute gingerServiceAttribute = (GingerServiceAttribute)Attribute.GetCustomAttribute(type, typeof(GingerServiceAttribute), false);
                        bool IsServiceSession = typeof(IServiceSession).IsAssignableFrom(type);
                        // if service impl IServiceSession then mark it so we know we need and agent Start/Stop
                        // if it is not session , then all actions are stand alone

                        PluginServiceInfo pluginServiceInfo = new PluginServiceInfo()
                        {
                            ServiceId = gingerServiceAttribute.Id, IsSession = IsServiceSession
                        };
                        // expecting to get ExcelAction, FileAction, DatabaseAction...
                        MethodInfo[] methods    = type.GetMethods();
                        Type[]       interfaces = type.GetInterfaces();
                        foreach (MethodInfo MI in methods)
                        {
                            //Check if method have token [GingerAction] or is is of interface else we ignore
                            GingerActionAttribute gingerActionAttr = (GingerActionAttribute)Attribute.GetCustomAttribute(MI, typeof(GingerActionAttribute), false);

                            if (gingerActionAttr == null && !MI.IsVirtual)   // Virtual if it is interface
                            {
                                continue;
                            }

                            PluginServiceActionInfo action = new PluginServiceActionInfo();

                            if (gingerActionAttr != null)
                            {
                                action.ActionId    = gingerActionAttr.Id;
                                action.Description = gingerActionAttr.Description;
                            }
                            else
                            {
                                // Check if the method is from interface
                                if (MI.IsVirtual)
                                {
                                    foreach (Type serviceInterface in interfaces)
                                    {
                                        //check if marked with [GingerInterface]
                                        GingerInterfaceAttribute gingerInterfaceAttr = (GingerInterfaceAttribute)Attribute.GetCustomAttribute(serviceInterface, typeof(GingerInterfaceAttribute), false);

                                        if (gingerInterfaceAttr != null)
                                        {
                                            var mm = serviceInterface.GetMethod(MI.Name);
                                            if (mm != null)
                                            {
                                                // Get the action id and desc from the interface
                                                action.Interface = serviceInterface.FullName;

                                                GingerActionAttribute interfaceGAAttr = (GingerActionAttribute)Attribute.GetCustomAttribute(mm, typeof(GingerActionAttribute), false);
                                                if (interfaceGAAttr == null)
                                                {
                                                    // no GA attr then ignore
                                                    continue;
                                                }
                                                else
                                                {
                                                    action.ActionId    = interfaceGAAttr.Id;
                                                    action.Description = interfaceGAAttr.Description;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            if (string.IsNullOrEmpty(action.ActionId))
                            {
                                continue;
                            }

                            foreach (ParameterInfo PI in MI.GetParameters())
                            {
                                if (PI.ParameterType.Name != nameof(IGingerAction))
                                {
                                    action.InputValues.Add(new ActionInputValueInfo()
                                    {
                                        Param = PI.Name, ParamType = PI.ParameterType
                                    });
                                }
                            }
                            pluginServiceInfo.Actions.Add(action);
                        }
                        mServices.Add(pluginServiceInfo);
                    }
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Executes a node.
        /// </summary>
        /// <param name="Variables">Set of variables for the activity.</param>
        /// <returns>Next node of execution, if different from the default, otherwise null (for default).</returns>
        public override async Task <LinkedListNode <IActivityNode> > Execute(Variables Variables)
        {
            string     ActionValue = this.action.GetValue(Variables);
            object     Actor       = this.GetActorObject(this.actor, Variables);
            Type       T           = Actor.GetType();
            MethodInfo Method;

            int[] Positions;
            int   i, j, c;

            lock (this.synchObj)
            {
                if (T == this.lastType)
                {
                    Method    = this.lastMethod;
                    Positions = this.argumentPositions;
                }
                else
                {
                    c         = this.argumentNames.Length;
                    Positions = new int[c];
                    Method    = null;

                    IEnumerable <MethodInfo> Methods = T.GetRuntimeMethods();

                    foreach (MethodInfo MI in Methods)
                    {
                        if (MI.IsAbstract || !MI.IsPublic)
                        {
                            continue;
                        }

                        if (MI.Name != ActionValue)
                        {
                            continue;
                        }

                        ParameterInfo[] Parameters = MI.GetParameters();
                        if (Parameters.Length != c)
                        {
                            continue;
                        }

                        for (i = 0; i < c; i++)
                        {
                            ParameterInfo Parameter = Parameters[i];
                            j = Array.IndexOf <string>(this.argumentNames, Parameter.Name);
                            if (j < 0)
                            {
                                Parameters = null;
                                break;
                            }

                            Positions[i] = j;
                        }

                        if (Parameters is null)
                        {
                            continue;
                        }

                        Method = MI;
                        break;
                    }

                    if (Method is null)
                    {
                        throw new Exception("No method named " + this.action + " found on actor " + this.actor + " (of type " + T.FullName + "), with the givet set of arguments.");
                    }

                    this.lastMethod        = Method;
                    this.argumentPositions = Positions;
                    this.lastType          = T;
                }
            }

            c = Positions.Length;

            object[] Arguments = new object[c];

            for (i = 0; i < c; i++)
            {
                Arguments[i] = this.arguments[Positions[i]].Value.Evaluate(Variables);
            }

            object Result = Method.Invoke(Actor, Arguments);

            if (Result is Task Task)
            {
                await Task;
            }

            return(null);
        }