/// <summary> /// add a default constructor. /// </summary> /// <param name="instance">The instance.</param> /// <param name="moveToCorrectPosition">if set to <c>true</c> [move to correct position].</param> /// <returns>The constructor.</returns> public static CodeFunction AddDefaultConstructor( this CodeClass instance, bool moveToCorrectPosition) { TraceService.WriteLine("CodeClassExtensions::AddDefaultConstructor file=" + instance.Name); CodeFunction codeFunction = instance.AddFunction( instance.Name, vsCMFunction.vsCMFunctionConstructor, vsCMTypeRef.vsCMTypeRefVoid, 0, vsCMAccess.vsCMAccessPublic, null); codeFunction.GetEndPoint().CreateEditPoint().InsertNewLine(); string comment = "<doc><summary>\r\nInitializes a new instance of the " + instance.Name + " class.\r\n</summary></doc>\r\n"; codeFunction.DocComment = comment; if (moveToCorrectPosition) { TraceService.WriteLine("Move to correct position"); IEnumerable<CodeVariable> variables = instance.GetVariables(); CodeVariable lastVariable = variables.LastOrDefault(); if (lastVariable != null) { EditPoint startPoint = codeFunction.StartPoint.CreateEditPoint(); EditPoint endPoint = codeFunction.EndPoint.CreateEditPoint(); string text = startPoint.GetText(endPoint); //// remove current code Function text instance.RemoveMember(codeFunction); if (endPoint.GetText(1) == Environment.NewLine) { TraceService.WriteLine("Delete line"); endPoint.Delete(1); } EditPoint editPoint = lastVariable.EndPoint.CreateEditPoint(); editPoint.Insert(string.Format("{0}{1}{2}", Environment.NewLine, Environment.NewLine, text)); //// we need to re find the function as we have deleted this one! codeFunction = instance.GetFunction(instance.Name); codeFunction.DocComment = comment; } } return codeFunction; }
public static void AddFunctions(this DbModel model, Type functionsType) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (functionsType == null) { throw new ArgumentNullException(nameof(functionsType)); } functionsType .GetMethods(BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static) .Select(methodInfo => new { MethodInfo = methodInfo, FunctionAttribute = methodInfo.GetCustomAttribute<FunctionAttribute>() }) .Where(method => method.FunctionAttribute != null) .ForEach(method => model.AddFunction(method.MethodInfo, method.FunctionAttribute)); }
/// <summary> /// Registers built-in methods for expressions parser. /// </summary> /// <param name="parser">The parser instance.</param> public static void RegisterMethods(this Parser parser) { parser.AddFunction("Now", () => DateTime.Now); parser.AddFunction("Today", () => DateTime.Today); parser.AddFunction<int, int, int, DateTime>("Date", (year, month, day) => new DateTime(year, month, day)); parser.AddFunction<int, int, int, int, int, int, DateTime>("Date", (year, month, day, hour, minute, second) => new DateTime(year, month, day, hour, minute, second)); parser.AddFunction<int, int, int, int, TimeSpan>("TimeSpan", (days, hours, minutes, seconds) => new TimeSpan(days, hours, minutes, seconds)); parser.AddFunction<string, int>("Length", str => str != null ? str.Length : 0); parser.AddFunction<string, string>("Trim", str => str != null ? str.Trim() : null); parser.AddFunction<string, string, string>("Concat", (strA, strB) => string.Concat(strA, strB)); parser.AddFunction<string, string, string, string>("Concat", (strA, strB, strC) => string.Concat(strA, strB, strC)); parser.AddFunction<string, string, int>("CompareOrdinal", (strA, strB) => string.Compare(strA, strB, StringComparison.Ordinal) > 0 ? 1 : string.Compare(strA, strB, StringComparison.Ordinal) < 0 ? -1 : 0); parser.AddFunction<string, string, int>("CompareOrdinalIgnoreCase", (strA, strB) => string.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) > 0 ? 1 : string.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) < 0 ? -1 : 0); parser.AddFunction<string, string, bool>("StartsWith", (str, prefix) => str != null && prefix != null && str.StartsWith(prefix)); parser.AddFunction<string, string, bool>("StartsWithIgnoreCase", (str, prefix) => str != null && prefix != null && str.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)); parser.AddFunction<string, string, bool>("EndsWith", (str, suffix) => str != null && suffix != null && str.EndsWith(suffix)); parser.AddFunction<string, string, bool>("EndsWithIgnoreCase", (str, suffix) => str != null && suffix != null && str.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)); parser.AddFunction<string, string, bool>("Contains", (str, substr) => str != null && substr != null && str.Contains(substr)); parser.AddFunction<string, string, bool>("ContainsIgnoreCase", (str, substr) => str != null && substr != null && str.ToLower().Contains(substr.ToLower())); parser.AddFunction<string, bool>("IsNullOrWhiteSpace", str => string.IsNullOrWhiteSpace(str)); parser.AddFunction<string, bool>("IsDigitChain", str => str != null && Regex.IsMatch(str, @"^[0-9]+$", RegexOptions.Compiled)); parser.AddFunction<string, bool>("IsNumber", str => str != null && Regex.IsMatch(str, @"^[\+-]?[0-9]*\.?[0-9]+(?:[eE][\+-]?[0-9]+)?$", RegexOptions.Compiled)); parser.AddFunction<string, bool>("IsEmail", str => str != null && Regex.IsMatch(str, @"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", RegexOptions.Compiled)); // taken from HTML5 specification: http://www.w3.org/TR/html5/forms.html#e-mail-state-(type=email) parser.AddFunction<string, bool>("IsUrl", str => str != null && Regex.IsMatch(str, @"^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase)); // contributed by Diego Perini: https://gist.github.com/dperini/729294 (https://mathiasbynens.be/demo/url-regex) parser.AddFunction<string, string, bool>("IsRegexMatch", (str, regex) => str != null && regex != null && Regex.IsMatch(str, regex, RegexOptions.Compiled)); parser.AddFunction<string, Guid>("Guid", str => new Guid(str)); }
/// <summary> /// Implements the function. /// </summary> /// <param name="instance">The instance.</param> /// <param name="codeSnippet">The code snippet.</param> public static void ImplementFunction( this CodeClass instance, CodeSnippet codeSnippet) { TraceService.WriteLine("CodeClassExtensions::ImplementFunction file=" + instance.Name); CodeFunction codeFunction = instance.AddFunction( "temp", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessPublic, null); TextPoint startPoint = codeFunction.StartPoint; EditPoint editPoint = startPoint.CreateEditPoint(); instance.RemoveMember(codeFunction); editPoint.Insert(codeSnippet.Code); }