Inheritance: System.Attribute
        /// <summary>
        /// Creates a new ExcelFunctionRegistration from a LambdaExpression.
        /// Uses the Name and Parameter Names to fill in the default attributes.
        /// </summary>
        /// <param name="functionLambda"></param>
        public ExcelFunctionRegistration(LambdaExpression functionLambda)
        {
            if (functionLambda == null) throw new ArgumentNullException("functionLambda");

            FunctionLambda = functionLambda;
            FunctionAttribute = new ExcelFunctionAttribute { Name = functionLambda.Name };
            ParameterRegistrations = functionLambda.Parameters
                                     .Select( p => new ExcelParameterRegistration(new ExcelArgumentAttribute { Name = p.Name }))
                                     .ToList();

            CustomAttributes = new List<object>();
            ReturnRegistration = new ExcelReturnRegistration();
        }
Exemplo n.º 2
0
 // Fix up the ExplicitRegistration, since we _are_ now explicitly registering
 static void ClearExplicitRegistration(List <object> methodAttributes)
 {
     foreach (object attrib in methodAttributes)
     {
         ExcelFunctionAttribute funcAttrib = attrib as ExcelFunctionAttribute;
         if (funcAttrib != null)
         {
             funcAttrib.ExplicitRegistration = false;
             continue;
         }
         ExcelCommandAttribute cmdAttrib = attrib as ExcelCommandAttribute;
         if (cmdAttrib != null)
         {
             cmdAttrib.ExplicitRegistration = false;
         }
     }
 }
        /// <summary>
        /// Creates a new ExcelFunctionRegistration with the given LambdaExpression.
        /// Uses the passes in attributes for registration.
        /// 
        /// The number of ExcelParameterRegistrations passed in must match the number of parameters in the LambdaExpression.
        /// </summary>
        /// <param name="functionLambda"></param>
        /// <param name="functionAttribute"></param>
        /// <param name="parameterRegistrations"></param>
        public ExcelFunctionRegistration(LambdaExpression functionLambda, ExcelFunctionAttribute functionAttribute, IEnumerable<ExcelParameterRegistration> parameterRegistrations = null)
        {
            if (functionLambda == null) throw new ArgumentNullException("functionLambda");
            if (functionAttribute == null) throw new ArgumentNullException("functionAttribute");

            FunctionLambda = functionLambda;
            FunctionAttribute = functionAttribute;
            if (parameterRegistrations == null)
            {
                if (functionLambda.Parameters.Count != 0) throw new ArgumentOutOfRangeException("parameterRegistrations", "No parameter registrations provided, but function has parameters.");
                ParameterRegistrations = new List<ExcelParameterRegistration>();
            }
            else
            {
                ParameterRegistrations = new List<ExcelParameterRegistration>(parameterRegistrations);
                if (functionLambda.Parameters.Count != ParameterRegistrations.Count) throw new ArgumentOutOfRangeException("parameterRegistrations", "Mismatched number of ParameterRegistrations provided.");
            }

            // Create the lists - hope the rest is filled in right...?
            CustomAttributes = new List<object>();
            ReturnRegistration = new ExcelReturnRegistration();
        }
Exemplo n.º 4
0
        // NOTE: 16 parameter max for Expression.GetDelegateType
        public RegistrationEntry(MethodInfo methodInfo)
        {
            MethodInfo = methodInfo;

            var paramExprs = methodInfo.GetParameters()
                             .Select(pi => Expression.Parameter(pi.ParameterType, pi.Name))
                             .ToArray();
            FunctionLambda = Expression.Lambda(Expression.Call(methodInfo, paramExprs), methodInfo.Name, paramExprs);

            // Need to make sure we have explicit
            FunctionAttribute = methodInfo.GetCustomAttribute<ExcelFunctionAttribute>();
            if (FunctionAttribute == null)
                FunctionAttribute = new ExcelFunctionAttribute { Name = methodInfo.Name };
            else if (string.IsNullOrEmpty(FunctionAttribute.Name))
                FunctionAttribute.Name = methodInfo.Name;

            ArgumentAttributes = new List<ExcelArgumentAttribute>();
            foreach (var pi in methodInfo.GetParameters())
            {
                var argAtt = pi.GetCustomAttribute<ExcelArgumentAttribute>();
                if (argAtt == null)
                    argAtt = new ExcelArgumentAttribute { Name = pi.Name };
                else if (string.IsNullOrEmpty(argAtt.Name))
                    argAtt.Name = pi.Name;

                ArgumentAttributes.Add(argAtt);
            }

            // Special check for final Params argument - transform to an ExcelParamsArgumentAttribute
            // NOTE: This won't work with a custom derived attribute...
            var lastParam = methodInfo.GetParameters().LastOrDefault();
            if (lastParam != null && lastParam.GetCustomAttribute<ParamArrayAttribute>() != null)
            {
                var excelParamsAtt = new ExcelParamsArgumentAttribute(ArgumentAttributes.Last());
                ArgumentAttributes[ArgumentAttributes.Count - 1] = excelParamsAtt;
            }
        }
        // NOTE: 16 parameter max for Expression.GetDelegateType
        // Copies all the (non Excel...) attributes from the method into the CustomAttribute lists.
        // TODO: What about native async function, which returns 'Void'?
        /// <summary>
        /// Creates a new ExcelFunctionRegistration from a MethodInfo, with a LambdaExpression that represents a call to the method.
        /// Uses the Name and Parameter Names from the MethodInfo to fill in the default attributes.
        /// All CustomAttributes on the method and parameters are copies to the respective collections in the ExcelFunctionRegistration.
        /// </summary>
        /// <param name="methodInfo"></param>
        public ExcelFunctionRegistration(MethodInfo methodInfo)
        {
            CustomAttributes = new List<object>();

            var paramExprs = methodInfo.GetParameters()
                             .Select(pi => Expression.Parameter(pi.ParameterType, pi.Name))
                             .ToList();
            FunctionLambda = Expression.Lambda(Expression.Call(methodInfo, paramExprs), methodInfo.Name, paramExprs);

            var allMethodAttributes = methodInfo.GetCustomAttributes(true);
            foreach (var att in allMethodAttributes)
            {
                var funcAtt = att as ExcelFunctionAttribute;
                if (funcAtt != null)
                {
                    FunctionAttribute = funcAtt;
                    // At least ensure that name is set - from the method if need be.
                    if (string.IsNullOrEmpty(FunctionAttribute.Name))
                        FunctionAttribute.Name = methodInfo.Name;
                }
                else
                {
                    CustomAttributes.Add(att);
                }
            }
            // Check that ExcelFunctionAttribute has been set
            if (FunctionAttribute == null)
            {
                FunctionAttribute = new ExcelFunctionAttribute { Name = methodInfo.Name };
            }

            ParameterRegistrations = methodInfo.GetParameters().Select(pi => new ExcelParameterRegistration(pi)).ToList();
            ReturnRegistration = new ExcelReturnRegistration();
            ReturnRegistration.CustomAttributes.AddRange(methodInfo.ReturnParameter.GetCustomAttributes(true));

            // Check that we haven't made a mistake
            Debug.Assert(IsValid());
        }