/// <summary> /// Decorate a native C# function with QsFunction /// </summary> /// <param name="methodInfo"></param> /// <param name="funcAttribute"></param> /// <returns></returns> public QsFunction GetQsFunctionFromTypeMethod(MethodInfo methodInfo, QsFunctionAttribute funcAttribute) { string qsNamespace = this._NamespaceType.Name; string qsFuncName = funcAttribute == null ? methodInfo.Name : funcAttribute.FunctionName; var miParameters = methodInfo.GetParameters(); int qsFuncParamCount = miParameters.Length; QsFunction QsModFunc = new QsFunction("[" + qsNamespace + "." + qsFuncName + "]", true); QsModFunc.FunctionNamespace = qsNamespace; QsModFunc.FunctionName = qsFuncName; List <QsParamInfo> prms = new List <QsParamInfo>(miParameters.Length); foreach (var miParam in miParameters) { QsParamInfo prm = new QsParamInfo(); //parameter name prm.Name = miParam.Name; var pis = miParam.GetCustomAttributes(typeof(QsParamInfoAttribute), true); if (pis.Length > 0) { prm.Type = ((QsParamInfoAttribute)pis[0]).ParameterType; } else { prm.Type = QsParamType.Value; } prms.Add(prm); } QsModFunc.Parameters = prms.ToArray(); QsModFunc.FunctionDeclaration += "("; StringBuilder sb = new StringBuilder(); foreach (var p in prms) { sb.Append(", " + p.Name); } if (sb.Length > 0) { QsModFunc.FunctionDeclaration += sb.ToString().TrimStart(',', ' '); } QsModFunc.FunctionDeclaration += ")"; QsModFunc.InternalFunctionDelegate = FormNativeFunctionDelegate(methodInfo); return(QsModFunc); }
/// <summary> /// Gets the members of the QsNamespace or (static class ) that hold the hard coded functions and properties. /// </summary> /// <param name="namespaceType"></param> /// <returns></returns> private Dictionary <string, object> GetQsNamespaceMethods() { // The namespace is a static class with public visibility to its static members. var methods = _NamespaceType.GetMethods( BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public /*| BindingFlags.InvokeMethod */ ); Dictionary <string, object> CodedMembers = new Dictionary <string, object>(System.StringComparer.OrdinalIgnoreCase); /* * var FilteredMethods = from m in methods * where * m.IsSpecialName == false * * && m.ReturnType.IsArray == false && * (m.ReturnType.BaseType == typeof(QsValue) || m.ReturnType == typeof(QsValue) || * m.ReturnType == typeof(string) || m.ReturnType.IsValueType == true) * * select m; */ var FilteredMethods = from m in methods where m.IsSpecialName == false select m; foreach (var member in FilteredMethods) { var method = ((MethodInfo)member); if (!method.IsSpecialName) //because properies getters are methods also :S { ParameterInfo[] mps = method.GetParameters(); int paramCount = mps.Length; var fAttributes = method.GetCustomAttributes(typeof(QsFunctionAttribute), false); QsFunctionAttribute funcAttribute = null; string method_name = string.Empty; string methodTrueName = string.Empty; if (fAttributes.Length > 0) { funcAttribute = (QsFunctionAttribute)fAttributes[0]; // if the attribute were found treat the function as namedargument function that is not default function. method_name = funcAttribute.FunctionName; if (funcAttribute.DefaultScopeFunction == false) { // alter the name so it includes the parameters also string[] ptext = (from p in mps select p.Name).ToArray(); methodTrueName = QsFunction.FormFunctionSymbolicName(method_name, ptext); } else { // don't use special parameter symbolic naming. methodTrueName = QsFunction.FormFunctionSymbolicName(method_name, paramCount); } CodedMembers.Add(methodTrueName, GetQsFunctionFromTypeMethod(method, funcAttribute)); } else { method_name = method.Name; methodTrueName = QsFunction.FormFunctionSymbolicName(method_name, paramCount); CodedMembers.Add(methodTrueName, GetQsFunctionFromTypeMethod(method, null)); } } } return(CodedMembers); }