Beispiel #1
0
        public object[,] Eval(params object[] inputs)
        {
            try
            {
                var convertedInputs = new List <object>();
                for (var i = 0; i < inputs.Length - _argOffset; i++)
                {
                    var paramName     = _methodInfo.GetParameters()[i].Name;
                    var inputAsMatrix = inputs[i + _argOffset] as object[, ];
                    if (inputAsMatrix[0, 0] is ExcelMissing && _defaultValues[i] == string.Empty)
                    {
                        throw new ArgumentException($"{paramName}: Is left blank but is not optional.");
                    }
                    convertedInputs.Add(ExcelTypeConverter.ConvertInput(_methodInfo.GetParameters()[i].ParameterType,
                                                                        inputAsMatrix,
                                                                        paramName, _defaultValues[i]));
                }

                var output     = _methodInfo.Invoke(null, convertedInputs.ToArray());
                var outputName = _argOffset == 0 ? null : GetOutputName(inputs[0]);
                return(ExcelTypeConverter.ConvertOuput(_methodInfo.ReturnType, output, outputName));
            }
            catch (TargetInvocationException e)
            {
                var result = new object[1, 1];
                if (e.InnerException != null)
                {
                    result[0, 0] = "ERROR: " + e.InnerException.Message;
                }
                else
                {
                    result[0, 0] = "ERROR: " + e.Message;
                }
                return(result);
            }
            catch (Exception e)
            {
                var result = new object[1, 1];
                result[0, 0] = "ERROR: " + e.Message;
                return(result);
            }
        }
Beispiel #2
0
        public static object[,] ViewObjectPropertyValue(
            [QuantSAExcelArgument(Description = "The object you wish to view.", Name = "Object")]
            object objectName,
            [QuantSAExcelArgument(Description = "The name of the property from the object that you wish to view.")]
            string propertyName,
            [QuantSAExcelArgument(Description = "If the property with propertyName is in turn an object then this" +
                                                "is the name of the property inside that which you wish to view.",
                                  Default = null)]
            string propertyNameL2)
        {
            if (propertyNameL2 != null)
            {
                var obj = GetObjectPropertyValue(objectName, propertyName);
                return(ViewObjectPropertyValue(obj, propertyNameL2, null));
            }

            var output = GetObjectPropertyValue(objectName, propertyName);

            if (output is ISerializableViaName objWithName)
            {
                return(ExcelTypeConverter.ConvertOuput(typeof(string), objWithName.GetName(), propertyName));
            }
            return(ExcelTypeConverter.ConvertOuput(output.GetType(), output, propertyName));
        }
Beispiel #3
0
        /// <summary>
        /// Get all the functions that appear in the assembly.  If the function name appears in
        /// <paramref name="funcsInUserFile"/> then use the value there to override the default
        /// visibility.
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="addInName"></param>
        /// <param name="funcsInUserFile"></param>
        /// <param name="delegates"></param>
        /// <param name="functionAttributes"></param>
        /// <param name="functionArgumentAttributes"></param>
        public static void GetDelegatesAndAttributes(Assembly assembly, string addInName,
                                                     Dictionary <string, bool> funcsInUserFile, ref List <Delegate> delegates,
                                                     ref List <object> functionAttributes, ref List <List <object> > functionArgumentAttributes)
        {
            var types = assembly.GetTypes();

            foreach (var type in types)
            {
                if (!type.IsPublic)
                {
                    continue;
                }
                foreach (var member in type.GetMembers())
                {
                    var excelFuncAttr    = member.GetCustomAttribute <QuantSAExcelFunctionAttribute>();
                    var dnaExcelFuncAttr = member.GetCustomAttribute <ExcelFunctionAttribute>();
                    if (dnaExcelFuncAttr != null && excelFuncAttr == null)
                    {
                        Log.Warn($"{dnaExcelFuncAttr.Name} is defined as an ExcelDNA function but not a QuantSA function so will be ignored");
                    }
                    if (excelFuncAttr == null)
                    {
                        continue;
                    }
                    if (funcsInUserFile.ContainsKey(excelFuncAttr.Name))
                    {
                        excelFuncAttr.IsHidden = !funcsInUserFile[excelFuncAttr.Name];
                    }

                    var parts = excelFuncAttr.Name.Split('.');
                    if (!(parts.Length == 2 && parts[0].Equals(addInName)))
                    {
                        throw new AddInException($"{excelFuncAttr.Name} does not following the naming " +
                                                 $"convention: {addInName}.FunctionName");
                    }
                    FunctionNames.Add(excelFuncAttr.Name);
                    var method   = member as MethodInfo;
                    var aAttr    = new List <object>();
                    var defaults = new List <string>();
                    if (method == null)
                    {
                        throw new AddInException($"{excelFuncAttr.Name} is marked with a " +
                                                 "QuantSAExcelFunctionAttribute but is not a method");
                    }
                    var putOnMap = ExcelTypeConverter.ShouldUseReference(method.ReturnType);
                    if (putOnMap)
                    {
                        aAttr.Add(new ExcelArgumentAttribute
                        {
                            Name        = "objectName",
                            Description = "The name that this object will be assigned on the map. " +
                                          "Should be unique."
                        });
                    }
                    foreach (var param in method.GetParameters())
                    {
                        var argAttrib = param.GetCustomAttribute <QuantSAExcelArgumentAttribute>();
                        if (argAttrib == null)
                        {
                            var dnaAttrib = param.GetCustomAttribute <ExcelArgumentAttribute>();
                            if (dnaAttrib != null)
                            {
                                if (dnaAttrib.Name == null)
                                {
                                    dnaAttrib.Name = param.Name;
                                }
                                dnaAttrib.Description = "(" + param.ParameterType.Name + ")" + dnaAttrib.Description;
                                aAttr.Add(dnaAttrib);
                            }
                            else
                            {
                                aAttr.Add(new ExcelArgumentAttribute
                                {
                                    Name        = param.Name,
                                    Description = param.ParameterType.Name
                                });
                            }

                            defaults.Add(string.Empty);
                        }

                        else
                        {
                            if (argAttrib.Name == null)
                            {
                                argAttrib.Name = param.Name;
                            }
                            argAttrib.Description = "(" + param.ParameterType.Name + ")" + argAttrib.Description;
                            aAttr.Add(argAttrib);
                            if (argAttrib.Default != string.Empty)
                            {
                                argAttrib.Description = "*" + argAttrib.Description + $"(Default value = {argAttrib.Default})";
                            }
                            defaults.Add(argAttrib.Default);
                        }
                    }

                    var dnaFuncAttr = excelFuncAttr;
                    if (dnaFuncAttr.Name == null)
                    {
                        dnaFuncAttr.Name = method.Name;
                    }
                    var excelFunction = new ExcelFunction(method, defaults, putOnMap);
                    delegates.Add(excelFunction.GetDelegate());
                    functionAttributes.Add(dnaFuncAttr);
                    functionArgumentAttributes.Add(aAttr);
                }
            }
        }