aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/ExtensionMethods
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/ExtensionMethods')
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/AssemblyExtensions.cs12
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/BooleanExtensions.cs14
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/ExceptionExtensions.cs17
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs15
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/IEnumerableExtensions.cs17
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/IParameterizedExtensions.cs18
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/ListExtensions.cs26
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs121
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/StringExtensions.cs42
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/TimeSpanExtensions.cs13
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs89
-rw-r--r--Software/Visual_Studio/Tango.Core/ExtensionMethods/ZipArchiveExtensions.cs39
12 files changed, 72 insertions, 351 deletions
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/AssemblyExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/AssemblyExtensions.cs
index bad90d47f..f75e76d3a 100644
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/AssemblyExtensions.cs
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/AssemblyExtensions.cs
@@ -33,17 +33,5 @@ public static class AssemblyExtensions
.AddDays(version.Revision);
}
}
-
- public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
- {
- try
- {
- return assembly.GetTypes();
- }
- catch (ReflectionTypeLoadException e)
- {
- return e.Types.Where(t => t != null);
- }
- }
}
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/BooleanExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/BooleanExtensions.cs
deleted file mode 100644
index 02464dc8e..000000000
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/BooleanExtensions.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-public static class BooleanExtensions
-{
- public static String ToStringYesNo(this Boolean value)
- {
- return value ? "Yes" : "No";
- }
-}
-
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ExceptionExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ExceptionExtensions.cs
index 7b87245b7..af4fc39b5 100644
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ExceptionExtensions.cs
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ExceptionExtensions.cs
@@ -31,23 +31,6 @@ public static class ExceptionExtensions
}
/// <summary>
- /// Gets the first exception if this is an aggregated exception.
- /// </summary>
- /// <param name="exception">The exception.</param>
- /// <returns></returns>
- public static Exception GetFirstIfAggregate(this Exception exception)
- {
- var ex = exception as AggregateException;
-
- if (ex != null && ex.InnerExceptions.Count > 0)
- {
- return ex.InnerExceptions.First();
- }
-
- return exception;
- }
-
- /// <summary>
/// Flattens the exception message in case it is an aggregated exception.
/// </summary>
/// <param name="exception">The exception.</param>
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs
index 426fbbd02..05e204f17 100644
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs
@@ -30,7 +30,7 @@ public static class FrameworkElementExtensions
bool mousePressed = false;
bool touchDown = false;
- MouseButtonEventHandler mouseDownHandler = (x, e) =>
+ MouseButtonEventHandler mouseDownHandler = (x, e) =>
{
if (!touchDown)
{
@@ -57,7 +57,7 @@ public static class FrameworkElementExtensions
//element.MouseDown += mouseDownHandler;
element.AddHandler(FrameworkElement.MouseDownEvent, mouseDownHandler);
- EventHandler<TouchEventArgs> touchDownHandler = (x, e) =>
+ EventHandler<TouchEventArgs> touchDownHandler = (x, e) =>
{
if (!mousePressed)
{
@@ -85,7 +85,7 @@ public static class FrameworkElementExtensions
//element.TouchDown += touchDownHandler;
element.AddHandler(FrameworkElement.TouchDownEvent, touchDownHandler);
- MouseButtonEventHandler mouseUpHandler = (_, __) =>
+ MouseButtonEventHandler mouseUpHandler = (_, __) =>
{
touchDown = false;
mousePressed = false;
@@ -236,7 +236,7 @@ public static class FrameworkElementExtensions
element.AddHandler(FrameworkElement.PreviewMouseDownEvent, previewMouseDownHandler);
//element.PreviewMouseDown += previewMouseDownHandler;
- EventHandler<TouchEventArgs> previewTouchDownHandler = (x, e) =>
+ EventHandler<TouchEventArgs> previewTouchDownHandler = (x, e) =>
{
if (!mousePressed)
{
@@ -264,7 +264,7 @@ public static class FrameworkElementExtensions
//element.PreviewTouchDown += previewTouchDownHandler;
element.AddHandler(FrameworkElement.PreviewTouchDownEvent, previewTouchDownHandler);
- MouseButtonEventHandler previewMouseUpHandler = (_, __) =>
+ MouseButtonEventHandler previewMouseUpHandler = (_, __) =>
{
touchDown = false;
mousePressed = false;
@@ -592,7 +592,7 @@ public static class FrameworkElementExtensions
{
bool isLoaded = false;
- element.AddHandler(FrameworkElement.LoadedEvent, new RoutedEventHandler((x, y) =>
+ element.AddHandler(FrameworkElement.LoadedEvent, new RoutedEventHandler((x, y) =>
{
if (!isLoaded)
{
@@ -766,11 +766,10 @@ public static class FrameworkElementExtensions
/// <param name="deceleration">The deceleration.</param>
/// <param name="onComplete">The on complete.</param>
/// <returns></returns>
- public static DoubleAnimation StartDoubleAnimation(this FrameworkElement element, DependencyProperty property, TimeSpan duration, double to, double? from = null, double? acceleration = null, double? deceleration = null, Action onComplete = null, bool autoReverse = false)
+ public static DoubleAnimation StartDoubleAnimation(this FrameworkElement element, DependencyProperty property, TimeSpan duration, double to, double? from = null, double? acceleration = null, double? deceleration = null, Action onComplete = null)
{
DoubleAnimation ani = new DoubleAnimation();
ani.Duration = duration;
- ani.AutoReverse = autoReverse;
if (acceleration.HasValue)
{
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/IEnumerableExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/IEnumerableExtensions.cs
index 68594d8ac..f4192a88b 100644
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/IEnumerableExtensions.cs
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/IEnumerableExtensions.cs
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
-using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Tango.Core;
@@ -105,21 +104,5 @@ public static class IEnumerableExtensions
{
return source.GroupBy(property).Select(g => g.First());
}
-
- /// <summary>
- /// Orders the collection by natural alphanumeric string.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="selector">The selector.</param>
- /// <returns></returns>
- public static IOrderedEnumerable<T> OrderByAlphaNumeric<T>(this IEnumerable<T> source, Func<T, string> selector)
- {
- int max = source
- .SelectMany(i => Regex.Matches(selector(i), @"\d+").Cast<Match>().Select(m => (int?)m.Value.Length))
- .Max() ?? 0;
-
- return source.OrderBy(i => Regex.Replace(selector(i), @"\d+", m => m.Value.PadLeft(max, '0')));
- }
}
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/IParameterizedExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/IParameterizedExtensions.cs
index c0490af8e..7107b9ee9 100644
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/IParameterizedExtensions.cs
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/IParameterizedExtensions.cs
@@ -9,7 +9,6 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.Core;
-using Tango.Core.CustomAttributes;
/// <summary>
/// Contains extension methods for <see cref="IParameterized"/>.
@@ -54,11 +53,10 @@ public static class IParameterizedExtensions
{
var paramAtt = prop.GetCustomAttributes(typeof(ParameterItemAttribute), false).Cast<ParameterItemAttribute>().FirstOrDefault();
var ignore = prop.GetCustomAttributes(typeof(ParameterIgnoreAttribute), false).Cast<ParameterIgnoreAttribute>().FirstOrDefault();
- var indexAttr = prop.GetCustomAttributes(typeof(PropertyIndexAttribute), false).Cast<PropertyIndexAttribute>().FirstOrDefault();
if (ignore == null && !properties.Exists(x => x.Name == prop.Name))
{
- var item = instance.CreateParameterItem(prop, paramAtt,indexAttr != null ? indexAttr.Index : index++, mode);
+ var item = instance.CreateParameterItem(prop, paramAtt, index++, mode);
ps.Add(item);
properties.Add(prop);
}
@@ -66,7 +64,7 @@ public static class IParameterizedExtensions
}
}
- return ps.OrderBy(x => x.Index).ToObservableCollection();
+ return ps;
}
/// <summary>
@@ -114,18 +112,6 @@ public static class IParameterizedExtensions
item.Minimum = rangeAtt.Minimum;
item.Maximum = rangeAtt.Maximum;
}
-
- StringFormatAttribute formatAtt = propertyInfo.GetCustomAttribute<StringFormatAttribute>();
- if (formatAtt != null)
- {
- item.StringFormat = formatAtt.Format;
- }
-
- PropertyIndexAttribute indexAtt = propertyInfo.GetCustomAttribute<PropertyIndexAttribute>();
- if (indexAtt != null)
- {
- item.Index = indexAtt.Index;
- }
}
if (mode == ParameterItemMode.Event)
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ListExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ListExtensions.cs
deleted file mode 100644
index f3e57b3ea..000000000
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ListExtensions.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-
-public static class ListExtensions
-{
- /// <summary>
- /// Splits the list to chunks.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="chunkSize">Size of the chunk.</param>
- /// <returns></returns>
- public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
- {
- return source
- .Select((x, i) => new { Index = i, Value = x })
- .GroupBy(x => x.Index / chunkSize)
- .Select(x => x.Select(v => v.Value).ToList())
- .ToList();
- }
-}
-
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs
index 3b344ce56..fe2c7ff9a 100644
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs
@@ -12,34 +12,6 @@ using Tango.Serialization;
namespace Tango.Core.ExtensionMethods
{
- public enum MappingFlags
- {
- /// <summary>
- /// All properties will be mapped.
- /// </summary>
- All = 1,
-
- /// <summary>
- /// Do not map string properties.
- /// </summary>
- NoStrings = 2,
-
- /// <summary>
- /// Do not map string properties with value of null.
- /// </summary>
- NoNullStrings = 4,
-
- /// <summary>
- /// Do not map reference types.
- /// </summary>
- NoReferenceTypes = 8,
-
- /// <summary>
- /// Map only value types.
- /// </summary>
- ValueTypesOnly,
- }
-
/// <summary>
/// Contains <see cref="Object"/> extension methods.
/// </summary>
@@ -59,7 +31,7 @@ namespace Tango.Core.ExtensionMethods
foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null))
{
- if (!prop.PropertyType.IsGenericTypeAndNotNullable())
+ if (!prop.PropertyType.IsGenericType)
{
prop.SetValue(cloned, prop.GetValue(obj));
}
@@ -78,7 +50,7 @@ namespace Tango.Core.ExtensionMethods
{
foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.SetMethod != null))
{
- if (!prop.PropertyType.IsGenericTypeAndNotNullable())
+ if (!prop.PropertyType.IsGenericType)
{
prop.SetValue(destination, prop.GetValue(source));
}
@@ -86,66 +58,85 @@ namespace Tango.Core.ExtensionMethods
}
/// <summary>
- /// Maps the object primitive properties values to the destination object.
+ /// Maps the object properties values to the destination object.
/// </summary>
- /// <param name="source">The source object.</param>
- /// <param name="destination">The destination object.</param>
- /// <param name="flags">The mapping flags.</param>
- /// <param name="condition">Optional condition.</param>
- public static void MapPropertiesTo(this object source, object destination, MappingFlags flags = MappingFlags.NoReferenceTypes, Func<PropertyInfo, bool> condition = null)
+ /// <param name="source">The source.</param>
+ /// <param name="destination">The destination.</param>
+ public static void MapPrimitivesTo(this object source, object destination)
{
- foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
+ foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive))
{
- var propType = prop.PropertyType;
- var value = prop.GetValue(source);
-
- if (condition != null)
- {
- if (!condition(prop)) continue;
- }
+ var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance);
- if (!propType.IsValueType && flags.HasFlag(MappingFlags.ValueTypesOnly))
+ if (desProp != null && desProp.PropertyType.IsPrimitive && desProp.SetMethod != null)
{
- continue;
+ desProp.SetValue(destination, prop.GetValue(source));
}
+ }
+ }
- if (propType == typeof(String) && flags.HasFlag(MappingFlags.NoStrings))
- {
- continue;
- }
+ /// <summary>
+ /// Maps the object properties values to the destination object including strings and without assigning null string values from the source.
+ /// </summary>
+ /// <param name="source">The source.</param>
+ /// <param name="destination">The destination.</param>
+ public static void MapPrimitivesWithStringsNoNullsTo(this object source, object destination)
+ {
+ foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String)))
+ {
+ var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance);
- if (propType.IsClass && propType != typeof(String) && flags.HasFlag(MappingFlags.NoReferenceTypes))
+ if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null)
{
- continue;
- }
+ var value = prop.GetValue(source);
- if (flags.HasFlag(MappingFlags.NoNullStrings) && propType == typeof(String) && String.IsNullOrEmpty(value as String))
- {
- continue;
+ if (desProp.PropertyType != typeof(String) || !String.IsNullOrEmpty(value as String))
+ {
+ desProp.SetValue(destination, value);
+ }
}
+ }
+ }
+ /// <summary>
+ /// Maps the object properties values to the destination object including strings and without assigning null string values from the source.
+ /// </summary>
+ /// <param name="source">The source.</param>
+ /// <param name="destination">The destination.</param>
+ public static void MapPrimitivesWithStrings(this object source, object destination)
+ {
+ foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String)))
+ {
var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance);
- if (desProp != null && desProp.SetMethod != null && desProp.PropertyType.IsEnum && prop.PropertyType.IsEnum)
- {
- desProp.SetValue(destination, Enum.Parse(desProp.PropertyType, value.ToString()));
- }
- else if (desProp != null && desProp.PropertyType == prop.PropertyType && desProp.SetMethod != null)
+ if (desProp != null && (desProp.PropertyType.IsPrimitive || desProp.PropertyType == typeof(String)) && desProp.SetMethod != null)
{
+ var value = prop.GetValue(source);
+
desProp.SetValue(destination, value);
}
}
}
-
/// <summary>
- /// Maps the object primitive properties values to the destination object.
+ /// Maps the object properties values to the destination object.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="destination">The destination.</param>
- public static void MapPrimitivesTo(this object source, object destination)
+ public static void MapPrimitivesTo(this object source, object destination, Func<PropertyInfo, bool> condition)
{
- source.MapPropertiesTo(destination, MappingFlags.ValueTypesOnly);
+ foreach (var prop in source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsPrimitive))
+ {
+ var desProp = destination.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance);
+
+ if (desProp != null && desProp.PropertyType.IsPrimitive && desProp.SetMethod != null)
+ {
+ if (condition(prop))
+ {
+ desProp.SetValue(destination, prop.GetValue(source));
+ }
+ }
+ }
}
/// <summary>
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/StringExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/StringExtensions.cs
index 5a12e16bb..78bb693f6 100644
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/StringExtensions.cs
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/StringExtensions.cs
@@ -224,28 +224,6 @@ public static class StringExtensions
);
}
- /// <summary>
- /// Removes any invalid file name characters from the string.
- /// </summary>
- /// <param name="str">The string.</param>
- /// <returns></returns>
- public static string ToValidFileName(this string str)
- {
- char[] _invalidChars = System.IO.Path.GetInvalidFileNameChars();
-
- String validFileName = str;
-
- if (validFileName != null)
- {
- foreach (var c in _invalidChars)
- {
- validFileName = validFileName.Replace(c.ToString(), "");
- }
- }
-
- return validFileName;
- }
-
public static String ToStringOrEmpty(this String str)
{
return str != null ? str : String.Empty;
@@ -256,16 +234,6 @@ public static class StringExtensions
return String.IsNullOrEmpty(str) ? null : str;
}
- public static bool IsNotNullOrEmpty(this String str)
- {
- return !String.IsNullOrWhiteSpace(str);
- }
-
- public static String ToOneLine(this String str)
- {
- return str.Replace(Environment.NewLine, " ");
- }
-
public static List<T> ToEnumValues<T>(this String str, char splitChar) where T : struct
{
if (!String.IsNullOrWhiteSpace(str))
@@ -277,14 +245,4 @@ public static class StringExtensions
return new List<T>();
}
}
-
- public static IEnumerable<int> AllIndexesOf(this string str, string searchstring)
- {
- int minIndex = str.IndexOf(searchstring);
- while (minIndex != -1)
- {
- yield return minIndex;
- minIndex = str.IndexOf(searchstring, minIndex + searchstring.Length);
- }
- }
}
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/TimeSpanExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/TimeSpanExtensions.cs
deleted file mode 100644
index 0aa70f5a8..000000000
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/TimeSpanExtensions.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-public static class TimeSpanExtensions
-{
- public static String ToStringUnlimitedHours(this TimeSpan span)
- {
- return $"{((int)span.TotalHours).ToString("D2")}:{span.Minutes.ToString("D2")}:{span.Seconds.ToString("D2")}";
- }
-}
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs
index 44297b277..c6fd3d811 100644
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs
+++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/TypeExtensions.cs
@@ -10,19 +10,6 @@ using System.Threading.Tasks;
/// </summary>
public static class TypeExtensions
{
- private class AssemblyExtensionMethods
- {
- public Assembly Assembly { get; set; }
- public List<MethodInfo> Methods { get; set; }
-
- public AssemblyExtensionMethods()
- {
- Methods = new List<MethodInfo>();
- }
- }
-
- private static List<AssemblyExtensionMethods> _extensionMethodsCache = new List<AssemblyExtensionMethods>();
-
/// <summary>
/// Gets all the extension methods registered in the specified assembly.
/// </summary>
@@ -31,61 +18,14 @@ public static class TypeExtensions
/// <returns></returns>
public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type, Assembly extensionsAssembly)
{
- if (type.Name == "CalculateRequest")
- {
-
- }
+ var query = from t in extensionsAssembly.GetTypes()
+ where !t.IsGenericType && !t.IsNested
+ from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
+ where m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false)
+ where m.GetParameters()[0].ParameterType == type
+ select m;
- var asmMethods = _extensionMethodsCache.FirstOrDefault(x => x.Assembly == extensionsAssembly);
-
- if (asmMethods == null)
- {
- var query = from t in extensionsAssembly.GetTypes()
- where !t.IsGenericType && !t.IsNested
- from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
- where m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false)
- select m;
-
- asmMethods = new AssemblyExtensionMethods();
- asmMethods.Assembly = extensionsAssembly;
- asmMethods.Methods = query.ToList();
- _extensionMethodsCache.Add(asmMethods);
- }
-
- List<MethodInfo> methods = new List<MethodInfo>();
-
- asmMethods.Methods.Where(x => x.GetParameters()[0].ParameterType.IsAssignableFrom(type));
-
- foreach (var method in asmMethods.Methods)
- {
- var parameter = method.GetParameters()[0];
-
- if (parameter.ParameterType.IsGenericParameter)
- {
- var constraints = parameter.ParameterType.GetGenericParameterConstraints().ToList();
-
- if (constraints.Count > 0)
- {
- if (constraints[0].IsAssignableFrom(type))
- {
- methods.Add(method);
- }
- else if (constraints[0].GetInterfaces().Any(x => x.IsAssignableFrom(type)))
- {
- methods.Add(method);
- }
- }
- }
- else
- {
- if (parameter.ParameterType.IsAssignableFrom(type))
- {
- methods.Add(method);
- }
- }
- }
-
- return methods;
+ return query;
}
/// <summary>
@@ -182,19 +122,4 @@ public static class TypeExtensions
{
return type.GetProperties().Where(x => x.GetCustomAttribute<T>() != null);
}
-
- public static bool IsGenericTypeAndNotNullable(this Type type)
- {
- return type.IsGenericType && Nullable.GetUnderlyingType(type) == null;
- }
-
- public static bool IsNullable(this Type type)
- {
- return Nullable.GetUnderlyingType(type) != null;
- }
-
- public static bool IsValueTypeOrString(this Type type)
- {
- return type.IsValueType || type == typeof(String);
- }
}
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ZipArchiveExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ZipArchiveExtensions.cs
deleted file mode 100644
index 15aba05bd..000000000
--- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ZipArchiveExtensions.cs
+++ /dev/null
@@ -1,39 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.IO.Compression;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-public static class ZipArchiveExtensions
-{
- public static void ExtractToDirectory(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
- {
- if (!overwrite)
- {
- archive.ExtractToDirectory(destinationDirectoryName);
- return;
- }
-
- DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
- string destinationDirectoryFullPath = di.FullName;
-
- foreach (ZipArchiveEntry file in archive.Entries)
- {
- string completeFileName = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, file.FullName));
-
- if (!completeFileName.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
- {
- throw new IOException("Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability");
- }
-
- if (file.Name == "")
- {
- Directory.CreateDirectory(Path.GetDirectoryName(completeFileName));
- continue;
- }
- file.ExtractToFile(completeFileName, true);
- }
- }
-}