public void TestDefaultParameterValueAttribute() { DefaultParameterValueAttribute dpva = new DefaultParameterValueAttribute(this); dpva = new DefaultParameterValueAttribute(null); Assert(dpva.GetType().IsSubclassOf(typeof(System.Attribute))); }
public static string BuildMethodDisplayName(MethodInfo m) { StringBuilder sb = new StringBuilder(); sb.Append(m.Name); sb.Append("("); int cnt = 0; bool optional = false; string defValue = ""; ParameterInfo[] prms = m.GetParameters(); while (cnt < prms.Length) { if (cnt > 0) { sb.Append(", "); } ParameterInfo pd = prms[cnt]; foreach (object c in pd.GetCustomAttributes(true)) { if (c is OutAttribute) { sb.Append("out "); } else if (c is InAttribute) { sb.Append("in "); } else if (c is OptionalAttribute) { optional = true; } else if (c is DefaultParameterValueAttribute) { DefaultParameterValueAttribute d = (DefaultParameterValueAttribute)c; String s = d.Value.ToString(); Object o = d.Value; switch (s) { case "System.SByte": defValue = ((SByte)o).ToString(); break; case "System.Int16": defValue = ((Int16)o).ToString(); break; case "System.Int32": defValue = ((Int32)o).ToString(); break; case "System.Int64": defValue = ((Int64)o).ToString(); break; case "System.Byte": defValue = ((Byte)o).ToString(); break; case "System.UInt16": defValue = ((UInt16)o).ToString(); break; case "System.UInt32": defValue = ((UInt32)o).ToString(); break; case "System.UInt64": defValue = ((UInt64)o).ToString(); break; case "System.Decimal": defValue = ((Decimal)o).ToString(); break; case "System.Double": defValue = ((Double)o).ToString(); break; case "System.Single": defValue = ((Single)o).ToString(); break; case "System.String": defValue = "\"" + ((String)o).ToString() + "\""; break; case "System.Char": defValue = "'" + ((Char)o).ToString() + "'"; break; default: //if (c.ConstructorArguments[0].Type.Resolve().IsEnum) //{ //} defValue = "Default Value was of a custom type. Unable to serialize."; break; } } } sb.Append(pd.ParameterType.Name); sb.Append(" "); sb.Append(pd.Name); if (optional) { sb.Append(" = "); sb.Append(defValue); optional = false; } cnt++; } sb.Append(")"); //throw new Exception(); return(sb.ToString()); }
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); }