public ExcelFunction LoadAndGetExcelFunction(string functionName) { var delegates = new List <Delegate>(); var functionAttributes = new List <object>(); var functionArgumentAttributes = new List <List <object> >(); var assembly = Assembly.GetAssembly(typeof(XLEquities)); StaticData.Load(); ExcelTypeConverter.AddConvertersFrom(Assembly.GetAssembly(typeof(AddIn))); FunctionRegistration.GetDelegatesAndAttributes(assembly, "QSA", new Dictionary <string, bool>(), ref delegates, ref functionAttributes, ref functionArgumentAttributes); ExcelFunction func = null; foreach (var d in delegates) { func = (ExcelFunction)d.Target; if (func.GetName() == functionName) { return(func); } } throw new InvalidOperationException($"{functionName} is not an available Excel function."); }
private static void RegisterMultiMagicFunction(FunctionRegistration method) { var first = method.MultiMagicAttributes.First(); string defaultName = first.Name; var deleg = method.Method.CreateDelegate(typeof(Func <ListNode, Scope, List <IValue> >)); MagicFunction magicFunction = new MultiMagicFunction((Func <ListNode, Scope, List <IValue> >)deleg, defaultName, first.Line, GetFileName(first)); foreach (var globalname in method.MultiMagicAttributes) { Scope[globalname.Name] = magicFunction; } }
//TODO replace with a generic method private static void RegisterNetFunction(FunctionRegistration method) { var first = method.NetAttributes.First(); string defaultName = first.Name; var firstParameterType = method.Method.GetParameters()[0].ParameterType; NetFunction netFunction; if (typeof(IValue).IsAssignableFrom(firstParameterType)) { var deleg = method.Method.CreateDelegate <Func <IValue, Scope, IValue> >(); netFunction = new NetFunction(deleg, defaultName, first.Line, GetFileName(first)); } else { var deleg = method.Method.CreateDelegate <Func <List <IValue>, Scope, IValue> >(); netFunction = new NetFunction(deleg, defaultName, first.Line, GetFileName(first)); } foreach (var globalname in method.NetAttributes) { Scope[globalname.Name] = netFunction; } }
// Called when addin opens public void AutoOpen() { try { QuantSAState.SetLogger(new ExcelFileLogFactory()); ExcelDna.IntelliSense.IntelliSenseServer.Install(); _log = QuantSAState.LogFactory.Get(MethodBase.GetCurrentMethod().DeclaringType); var pathString = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "QuantSA"); if (!Directory.Exists(pathString)) { Directory.CreateDirectory(pathString); } //TODO: Check if newer version of addin exists. _log.Info("Loading static data"); StaticData.Load(); _log.Info("Check custom function visibility"); // Get the functions that are set for the user. var funcsInUserFile = GetFunctionVisibility(FunctionsFilenameUser); //Check in the installation folder for any dlls that include a class of type IQuantSAPlugin AssemblyImageResources = new Dictionary <string, Bitmap>(); _log.Info("Check for plugins"); GetPlugins(); var assemblies = new[] { Assembly.GetAssembly(typeof(XLEquities)), Assembly.GetAssembly(typeof(AddIn)) }; _log.Info("Add converters"); foreach (var tuple in Plugins) { ExcelTypeConverter.AddConvertersFrom(tuple.Item2); } foreach (var assembly in assemblies) { ExcelTypeConverter.AddConvertersFrom(assembly); } _log.Info("Register user functions"); foreach (var tuple in Plugins) { FunctionRegistration.RegisterFrom(tuple.Item2, tuple.Item1.GetShortName(), funcsInUserFile); } foreach (var assembly in assemblies) { FunctionRegistration.RegisterFrom(assembly, "QSA", funcsInUserFile); } UpdateUserFunctionFile(funcsInUserFile, FunctionRegistration.FunctionNames); } catch (Exception e) { _log.Error(e); var pathString = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "QuantSA"); var fileName = Path.Combine(pathString, "QuantSAError.txt"); File.WriteAllText(fileName, e.ToString()); throw new AddInException("An error occurred while opening the QuantSA addin.\n" + "Check the error log file for details.\n\n" + $"{fileName}"); } }