/// <summary> /// Register property as excel function /// </summary> /// <param name="property"></param> public void AddProperty(PropertyInfo property) { var getLambda = GetProperty(property); var attribute = (IExcelFunctionAttribute)property.GetCustomAttributes(typeof(IExcelFunctionAttribute)).Single(); var handleParameter = new ExcelParameterRegistration(new ExcelArgumentAttribute() { Name = "Handle" }); // we do not register twice the base properties if (property.ReflectedType != property.DeclaringType) { return; } var className = property.ReflectedType.Name.Replace("Public", ""); var getRegistration = new ExcelFunctionRegistration(getLambda, //attribute.ToExcelFunctionAttribute(property.DeclaringType.BaseType.GenericTypeArguments.First().Name + "." + "Get" +property.Name), attribute.ToExcelFunctionAttribute(className + "." + "Get" + property.Name), new List <ExcelParameterRegistration>() { handleParameter }); _registrations.Add(getRegistration); var bindLambda = BindProperty(property); var bindRegistration = new ExcelFunctionRegistration(bindLambda, attribute.ToExcelFunctionAttribute(className + "." + "Bind" + property.Name), new List <ExcelParameterRegistration>() { handleParameter }); _registrations.Add(bindRegistration); }
private static ExcelFunctionRegistration ToExcelFunctionRegistration(IScript script, string FunctionName) { var paramExprs = script .Parameters .Select(x => Expression.Parameter(GetExcelRegistrationTypeFor(x), x.Name)) .ToList(); var methodInfo = typeof(ExcelScriptAddin).GetMethod(nameof(InternalRun), BindingFlags.Static | BindingFlags.NonPublic); var paramsToObj = paramExprs.Select(x => Expression.Convert(x, typeof(object))); // cast parameter to object, otherwise won't match function signature of ExcelScriptAddin.Run var paramsArray = Expression.NewArrayInit(typeof(object), paramsToObj.ToArray()); var methodArgs = new Expression[] { Expression.Constant(script, typeof(IScript)), paramsArray }; LambdaExpression lambdaExpression = Expression.Lambda(Expression.Call(methodInfo, methodArgs), FunctionName, paramExprs); ExcelFunctionAttribute excelFunctionAttribute = new ExcelFunctionAttribute() { Name = FunctionName }; if (!String.IsNullOrEmpty(script.Description)) { excelFunctionAttribute.Description = script.Description; } IEnumerable <ExcelParameterRegistration> paramRegistrations = script .Parameters .Select((IParameter x) => { var argumentAttribute = new ExcelArgumentAttribute() { Name = x.Name }; var parameterRegistration = new ExcelParameterRegistration(argumentAttribute); if (x.Type == typeof(Excel.Range) || x.Type == typeof(ExcelReference)) { argumentAttribute.AllowReference = true; } if (x.IsOptional) { var optionalAttribute = new OptionalAttribute(); parameterRegistration.CustomAttributes.Add(optionalAttribute); var defaultValueAttribute = new DefaultParameterValueAttribute(x.DefaultValue); parameterRegistration.CustomAttributes.Add(defaultValueAttribute); } if (!String.IsNullOrEmpty(x.Description)) { argumentAttribute.Description = x.Description; } return(parameterRegistration); }); var reg = new ExcelFunctionRegistration(lambdaExpression, excelFunctionAttribute, paramRegistrations); return(reg); }
public static Expression <Func <object, Excel.Range> > ParameterConversion(Type paramType, ExcelParameterRegistration paramRegistration) { if (paramType == typeof(Excel.Range)) { return((Expression <Func <object, Excel.Range> >)((object input) => ReferenceToRange(input))); } else { return(null); } }