Refactored ExtensionMethod search code from DynamicNodeList (searches for extension methods e.g. .Random()) to tidy it up

Moved Extension Method searching code to seperate static class
Moved all the defined extension methods for DynamicNode into the same ExtensionMethods.cs file
Changed the way that .ContainsAny within a where is evaluated to use the ExtensionMethodFinder class - should allow extra search methods to be defined
This commit is contained in:
agrath@gmail.com
2011-02-26 16:15:45 -13:00
parent 1f05d43047
commit cbe94ae6aa
7 changed files with 261 additions and 240 deletions

View File

@@ -22,5 +22,75 @@ namespace umbraco.MacroEngines
}
return flattenedList;
}
public static DynamicNodeList Random(this DynamicNodeList all, int Min, int Max)
{
//get a random number generator
Random r = new Random();
//choose the number of elements to be returned between Min and Max
int Number = r.Next(Min, Max);
//Call the other method
return Random(all, Number);
}
public static DynamicNodeList Random(this DynamicNodeList all, int Max)
{
//Randomly order the items in the set by a Guid, Take the correct number, and return this wrapped in a new DynamicNodeList
return new DynamicNodeList(all.Items.OrderBy(x => Guid.NewGuid()).Take(Max));
}
public static DynamicNode Random(this DynamicNodeList all)
{
return all.Items.OrderBy(x => Guid.NewGuid()).First();
}
public static bool ContainsAny(this string haystack, List<string> needles)
{
if (!string.IsNullOrEmpty(haystack) || needles.Count > 0)
{
foreach (string value in needles)
{
if (haystack.Contains(value))
return true;
}
}
return false;
}
public static bool ContainsAny(this string haystack, params string[] needles)
{
if (!string.IsNullOrEmpty(haystack) || needles.Length > 0)
{
foreach (string value in needles)
{
if (haystack.Contains(value))
return true;
}
}
return false;
}
public static bool ContainsAny(this string haystack, StringComparison comparison, List<string> needles)
{
if (!string.IsNullOrEmpty(haystack) || needles.Count > 0)
{
foreach (string value in needles)
{
if (haystack.IndexOf(value, comparison) >= 0)
return true;
}
}
return false;
}
public static bool ContainsAny(this string haystack, StringComparison comparison, params string[] needles)
{
if (!string.IsNullOrEmpty(haystack) || needles.Length > 0)
{
foreach (string value in needles)
{
if (haystack.IndexOf(value, comparison) >= 0)
return true;
}
}
return false;
}
}
}