MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5using System.Linq;6using System.Reflection;7using System.Runtime.CompilerServices;8 9namespace System.Management.Automation10{11 internal static class ExtensionMethods12 {13 public static void SafeInvoke(this EventHandler eventHandler, object sender, EventArgs eventArgs)14 {15 eventHandler?.Invoke(sender, eventArgs);16 }17 18 public static void SafeInvoke<T>(this EventHandler<T> eventHandler, object sender, T eventArgs) where T : EventArgs19 {20 eventHandler?.Invoke(sender, eventArgs);21 }22 }23 24 internal static class EnumerableExtensions25 {26 internal static IEnumerable<T> Prepend<T>(this IEnumerable<T> collection, T element)27 {28 yield return element;29 foreach (T t in collection)30 yield return t;31 }32 33 internal static int SequenceGetHashCode<T>(this IEnumerable<T> xs)34 {35 // algorithm based on https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode36 if (xs == null)37 {38 return 82460653; // random number39 }40 41 unchecked42 {43 int hash = 41; // 41 is a random prime number44 foreach (T x in xs)45 {46 hash *= 59; // 59 is a random prime number47 if (x != null)48 {49 hash += x.GetHashCode();50 }51 }52 53 return hash;54 }55 }56 }57 58 /// <summary>59 /// The type extension methods within this partial class are used/shared by both FullCLR and CoreCLR powershell.60 ///61 /// * If you want to add an extension method that will be used by both FullCLR and CoreCLR powershell, please add it here.62 /// * If you want to add an extension method that will be used only by CoreCLR powershell, please add it to the partial63 /// 'PSTypeExtensions' class in 'CorePsExtensions.cs'.64 /// </summary>65 internal static class PSTypeExtensions66 {67 /// <summary>68 /// Check does the type have an instance default constructor with visibility that allows calling it from subclass.69 /// </summary>70 /// <param name="type">Type.</param>71 /// <returns>True when type has a default ctor.</returns>72 internal static bool HasDefaultCtor(this Type type)73 {74 var ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);75 if (ctor != null)76 {77 if (ctor.IsPublic || ctor.IsFamily || ctor.IsFamilyOrAssembly)78 {79 return true;80 }81 }82 83 return false;84 }85 86 internal static bool IsNumeric(this Type type)87 {88 return LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(type));89 }90 91 internal static bool IsNumericOrPrimitive(this Type type)92 {93 return type.IsPrimitive || LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(type));94 }95 96 internal static bool IsSafePrimitive(this Type type)97 {98 return type.IsPrimitive && (type != typeof(IntPtr)) && (type != typeof(UIntPtr));99 }100 101 internal static bool IsFloating(this Type type)102 {103 return LanguagePrimitives.IsFloating(LanguagePrimitives.GetTypeCode(type));104 }105 106 internal static bool IsInteger(this Type type)107 {108 return LanguagePrimitives.IsInteger(LanguagePrimitives.GetTypeCode(type));109 }110 111 [MethodImpl(MethodImplOptions.AggressiveInlining)]112 internal static TypeCode GetTypeCode(this Type type)113 {114 return Type.GetTypeCode(type);115 }116 117 internal static IEnumerable<T> GetCustomAttributes<T>(this Type type, bool inherit)118 where T : Attribute119 {120 return from attr in type.GetCustomAttributes(typeof(T), inherit)121 where attr is T122 select (T)attr;123 }124 }125 126 internal static class WeakReferenceExtensions127 {128 internal static bool TryGetTarget<T>(this WeakReference weakReference, out T target) where T : class129 {130 var t = weakReference.Target;131 target = t as T;132 return (target != null);133 }134 }135}136 