Created
March 19, 2015 09:58
-
-
Save 121jigowatts/1b6dc75141b2daacab10 to your computer and use it in GitHub Desktop.
マルチキャストデリゲートでValidation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Delegate.History | |
{ | |
public class ValidationByDeletege | |
{ | |
public static Predicate<string> IsNotEmpty() | |
{ | |
return value => value.Trim().Length > 0; | |
} | |
public static Predicate<string> IsLength(int len) | |
{ | |
return value => value.Length <= len; | |
} | |
public static Predicate<string> IsNumber() | |
{ | |
return value => | |
{ | |
int res; | |
if (int.TryParse(value, out res)) { return true; } | |
return false; | |
}; | |
} | |
public static bool Validate<T>(T value, Predicate<T> userCallbacks) | |
{ | |
foreach (Predicate<T> p in userCallbacks.GetInvocationList()) | |
{ | |
bool result = p(value); | |
if (!result) { return result; } | |
} | |
return true; | |
} | |
public static bool User(string value) | |
{ | |
Predicate<string> v1 = IsNotEmpty(); | |
Predicate<string> v2 = IsLength(10); | |
Predicate<string> v3 = IsNumber(); | |
//マルチキャストデリゲート | |
var userCallbacks = v1 + v2 + v3; | |
return Validate<string>(value, userCallbacks); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment