Created
February 1, 2017 00:58
-
-
Save jnm2/03e597d81027d64b033aef9620847f4c to your computer and use it in GitHub Desktop.
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; | |
public static class MatchExtensions | |
{ | |
/// <summary> | |
/// Performs the first action with a matching type. | |
/// </summary> | |
/// <param name="value">The value to cast.</param> | |
/// <param name="match1">The action to perform if the value is TMatch1.</param> | |
public static void Match<T, TMatch1>(this T value, Action<TMatch1> match1) | |
where TMatch1 : T | |
{ | |
if (value is TMatch1) | |
match1.Invoke((TMatch1)value); | |
} | |
/// <summary> | |
/// Performs the first action with a matching type. | |
/// </summary> | |
/// <param name="value">The value to cast.</param> | |
/// <param name="match1">The action to perform if the value is TMatch1.</param> | |
/// <param name="match2">The action to perform if the value is TMatch2 and no prior match was found.</param> | |
public static void Match<T, TMatch1, TMatch2>(this T value, Action<TMatch1> match1, Action<TMatch2> match2) | |
where TMatch1 : T | |
where TMatch2 : T | |
{ | |
if (value is TMatch1) | |
match1.Invoke((TMatch1)value); | |
else if (value is TMatch2) | |
match2.Invoke((TMatch2)value); | |
} | |
/// <summary> | |
/// Performs the first action with a matching type. | |
/// </summary> | |
/// <param name="value">The value to cast.</param> | |
/// <param name="match1">The action to perform if the value is TMatch1.</param> | |
/// <param name="match2">The action to perform if the value is TMatch2 and no prior match was found.</param> | |
/// <param name="match3">The action to perform if the value is TMatch3 and no prior match was found.</param> | |
public static void Match<T, TMatch1, TMatch2, TMatch3>(this T value, Action<TMatch1> match1, Action<TMatch2> match2, Action<TMatch3> match3) | |
where TMatch1 : T | |
where TMatch2 : T | |
where TMatch3 : T | |
{ | |
if (value is TMatch1) | |
match1.Invoke((TMatch1)value); | |
else if (value is TMatch2) | |
match2.Invoke((TMatch2)value); | |
else if (value is TMatch3) | |
match3.Invoke((TMatch3)value); | |
} | |
/// <summary> | |
/// Performs the first action with a matching type. | |
/// </summary> | |
/// <param name="value">The value to cast.</param> | |
/// <param name="match1">The action to perform if the value is TMatch1.</param> | |
/// <param name="match2">The action to perform if the value is TMatch2 and no prior match was found.</param> | |
/// <param name="match3">The action to perform if the value is TMatch3 and no prior match was found.</param> | |
/// <param name="match4">The action to perform if the value is TMatch4 and no prior match was found.</param> | |
public static void Match<T, TMatch1, TMatch2, TMatch3, TMatch4>(this T value, Action<TMatch1> match1, Action<TMatch2> match2, Action<TMatch3> match3, Action<TMatch4> match4) | |
where TMatch1 : T | |
where TMatch2 : T | |
where TMatch3 : T | |
where TMatch4 : T | |
{ | |
if (value is TMatch1) | |
match1.Invoke((TMatch1)value); | |
else if (value is TMatch2) | |
match2.Invoke((TMatch2)value); | |
else if (value is TMatch3) | |
match3.Invoke((TMatch3)value); | |
else if (value is TMatch4) | |
match4.Invoke((TMatch4)value); | |
} | |
/// <summary> | |
/// Performs the first action with a matching type. | |
/// </summary> | |
/// <param name="value">The value to cast.</param> | |
/// <param name="match1">The action to perform if the value is TMatch1.</param> | |
/// <param name="match2">The action to perform if the value is TMatch2 and no prior match was found.</param> | |
/// <param name="match3">The action to perform if the value is TMatch3 and no prior match was found.</param> | |
/// <param name="match4">The action to perform if the value is TMatch4 and no prior match was found.</param> | |
/// <param name="match5">The action to perform if the value is TMatch5 and no prior match was found.</param> | |
public static void Match<T, TMatch1, TMatch2, TMatch3, TMatch4, TMatch5>(this T value, Action<TMatch1> match1, Action<TMatch2> match2, Action<TMatch3> match3, Action<TMatch4> match4, Action<TMatch5> match5) | |
where TMatch1 : T | |
where TMatch2 : T | |
where TMatch3 : T | |
where TMatch4 : T | |
where TMatch5 : T | |
{ | |
if (value is TMatch1) | |
match1.Invoke((TMatch1)value); | |
else if (value is TMatch2) | |
match2.Invoke((TMatch2)value); | |
else if (value is TMatch3) | |
match3.Invoke((TMatch3)value); | |
else if (value is TMatch4) | |
match4.Invoke((TMatch4)value); | |
else if (value is TMatch5) | |
match5.Invoke((TMatch5)value); | |
} | |
/// <summary> | |
/// Performs the first action with a matching type. | |
/// </summary> | |
/// <param name="value">The value to cast.</param> | |
/// <param name="match1">The action to perform if the value is TMatch1.</param> | |
/// <param name="match2">The action to perform if the value is TMatch2 and no prior match was found.</param> | |
/// <param name="match3">The action to perform if the value is TMatch3 and no prior match was found.</param> | |
/// <param name="match4">The action to perform if the value is TMatch4 and no prior match was found.</param> | |
/// <param name="match5">The action to perform if the value is TMatch5 and no prior match was found.</param> | |
/// <param name="match6">The action to perform if the value is TMatch6 and no prior match was found.</param> | |
public static void Match<T, TMatch1, TMatch2, TMatch3, TMatch4, TMatch5, TMatch6>(this T value, Action<TMatch1> match1, Action<TMatch2> match2, Action<TMatch3> match3, Action<TMatch4> match4, Action<TMatch5> match5, Action<TMatch6> match6) | |
where TMatch1 : T | |
where TMatch2 : T | |
where TMatch3 : T | |
where TMatch4 : T | |
where TMatch5 : T | |
where TMatch6 : T | |
{ | |
if (value is TMatch1) | |
match1.Invoke((TMatch1)value); | |
else if (value is TMatch2) | |
match2.Invoke((TMatch2)value); | |
else if (value is TMatch3) | |
match3.Invoke((TMatch3)value); | |
else if (value is TMatch4) | |
match4.Invoke((TMatch4)value); | |
else if (value is TMatch5) | |
match5.Invoke((TMatch5)value); | |
else if (value is TMatch6) | |
match6.Invoke((TMatch6)value); | |
} | |
/// <summary> | |
/// Performs the first action with a matching type. | |
/// </summary> | |
/// <param name="value">The value to cast.</param> | |
/// <param name="match1">The action to perform if the value is TMatch1.</param> | |
/// <param name="match2">The action to perform if the value is TMatch2 and no prior match was found.</param> | |
/// <param name="match3">The action to perform if the value is TMatch3 and no prior match was found.</param> | |
/// <param name="match4">The action to perform if the value is TMatch4 and no prior match was found.</param> | |
/// <param name="match5">The action to perform if the value is TMatch5 and no prior match was found.</param> | |
/// <param name="match6">The action to perform if the value is TMatch6 and no prior match was found.</param> | |
/// <param name="match7">The action to perform if the value is TMatch7 and no prior match was found.</param> | |
public static void Match<T, TMatch1, TMatch2, TMatch3, TMatch4, TMatch5, TMatch6, TMatch7>(this T value, Action<TMatch1> match1, Action<TMatch2> match2, Action<TMatch3> match3, Action<TMatch4> match4, Action<TMatch5> match5, Action<TMatch6> match6, Action<TMatch7> match7) | |
where TMatch1 : T | |
where TMatch2 : T | |
where TMatch3 : T | |
where TMatch4 : T | |
where TMatch5 : T | |
where TMatch6 : T | |
where TMatch7 : T | |
{ | |
if (value is TMatch1) | |
match1.Invoke((TMatch1)value); | |
else if (value is TMatch2) | |
match2.Invoke((TMatch2)value); | |
else if (value is TMatch3) | |
match3.Invoke((TMatch3)value); | |
else if (value is TMatch4) | |
match4.Invoke((TMatch4)value); | |
else if (value is TMatch5) | |
match5.Invoke((TMatch5)value); | |
else if (value is TMatch6) | |
match6.Invoke((TMatch6)value); | |
else if (value is TMatch7) | |
match7.Invoke((TMatch7)value); | |
} | |
/// <summary> | |
/// Performs the first action with a matching type. | |
/// </summary> | |
/// <param name="value">The value to cast.</param> | |
/// <param name="match1">The action to perform if the value is TMatch1.</param> | |
/// <param name="match2">The action to perform if the value is TMatch2 and no prior match was found.</param> | |
/// <param name="match3">The action to perform if the value is TMatch3 and no prior match was found.</param> | |
/// <param name="match4">The action to perform if the value is TMatch4 and no prior match was found.</param> | |
/// <param name="match5">The action to perform if the value is TMatch5 and no prior match was found.</param> | |
/// <param name="match6">The action to perform if the value is TMatch6 and no prior match was found.</param> | |
/// <param name="match7">The action to perform if the value is TMatch7 and no prior match was found.</param> | |
/// <param name="match8">The action to perform if the value is TMatch8 and no prior match was found.</param> | |
public static void Match<T, TMatch1, TMatch2, TMatch3, TMatch4, TMatch5, TMatch6, TMatch7, TMatch8>(this T value, Action<TMatch1> match1, Action<TMatch2> match2, Action<TMatch3> match3, Action<TMatch4> match4, Action<TMatch5> match5, Action<TMatch6> match6, Action<TMatch7> match7, Action<TMatch8> match8) | |
where TMatch1 : T | |
where TMatch2 : T | |
where TMatch3 : T | |
where TMatch4 : T | |
where TMatch5 : T | |
where TMatch6 : T | |
where TMatch7 : T | |
where TMatch8 : T | |
{ | |
if (value is TMatch1) | |
match1.Invoke((TMatch1)value); | |
else if (value is TMatch2) | |
match2.Invoke((TMatch2)value); | |
else if (value is TMatch3) | |
match3.Invoke((TMatch3)value); | |
else if (value is TMatch4) | |
match4.Invoke((TMatch4)value); | |
else if (value is TMatch5) | |
match5.Invoke((TMatch5)value); | |
else if (value is TMatch6) | |
match6.Invoke((TMatch6)value); | |
else if (value is TMatch7) | |
match7.Invoke((TMatch7)value); | |
else if (value is TMatch8) | |
match8.Invoke((TMatch8)value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment