16 public static class CSharpExtensions {
21 public static bool NearlyEquals(
this float a,
float b,
float epsilon = Constants.EPSILON) {
22 float absA =
Math.Abs(a);
23 float absB =
Math.Abs(b);
24 float diff =
Math.Abs(a - b);
28 }
else if (a == 0 || b == 0 || diff <
float.MinValue) {
31 return diff < (epsilon *
float.MinValue);
33 return diff / (absA + absB) < epsilon;
41 public static bool HasMethod(
this object objectToCheck,
string methodName) {
42 var type = objectToCheck.GetType();
43 return type.GetMethod(methodName) !=
null;
50 public static int indexOf(
this Enum enumItem) {
51 return Array.IndexOf(Enum.GetValues(enumItem.GetType()), enumItem);
58 public static T itemFor<T>(
this int ordinal) {
59 T[] values = (T[])Enum.GetValues(typeof(T));
60 return values[ordinal];
67 public static void Dispatch<T>(
this EventHandler<T> handler,
68 object sender, T eventArgs) where T : EventArgs {
69 if (handler !=
null) handler(sender, eventArgs);
78 public static void DispatchOnContext<T>(
this EventHandler<T> handler,
object sender,
79 System.Threading.SynchronizationContext context,
80 T eventArgs) where T : EventArgs {
81 if (handler !=
null) {
82 if (context !=
null) {
83 System.Threading.SendOrPostCallback evt = (spc_args) => { handler(sender, spc_args as T); };
84 context.Post(evt, eventArgs);
86 handler(sender, eventArgs);