Skip to content

Commit 1a48f44

Browse files
committed
Added more functions
1 parent d081c5e commit 1a48f44

File tree

7 files changed

+117
-3
lines changed

7 files changed

+117
-3
lines changed

Net.Bluewalk.DotNetUtils/Extensions/DateTimeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public static DateTime Random(this DateTime value, TimeSpan timeSpan)
7777
}
7878

7979
/// <summary>
80-
/// Get the closet date for target DayOfWeek
80+
/// Get the closest date for target DayOfWeek
8181
/// </summary>
8282
/// <param name="date">The original date</param>
8383
/// <param name="target">The target of DayOfWeek</param>
8484
/// <returns>The closet date which is target day of week</returns>
85-
public static DateTime GetClosetDate(this DateTime date, DayOfWeek target)
85+
public static DateTime GetClosestDate(this DateTime date, DayOfWeek target)
8686
{
8787
if (date.DayOfWeek == target)
8888
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Specialized;
2+
using System.Linq;
3+
using System.Web;
4+
5+
namespace Net.Bluewalk.DotNetUtils.Extensions
6+
{
7+
public static class NameValueCollectionExtensions
8+
{
9+
public static string ToQueryString(this NameValueCollection value) => string.Join("&",
10+
value.AllKeys.Select(a => a + "=" + HttpUtility.UrlEncode(value[a])));
11+
}
12+
}

Net.Bluewalk.DotNetUtils/Extensions/StreamExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Threading.Tasks;
23

34
namespace Net.Bluewalk.DotNetUtils.Extensions
45
{
@@ -39,5 +40,18 @@ public static string ReadToString(this Stream value)
3940
using var reader = new StreamReader(value);
4041
return reader.ReadToEnd();
4142
}
43+
44+
/// <summary>
45+
/// Reads to string.
46+
/// </summary>
47+
/// <param name="value"></param>
48+
/// <returns></returns>
49+
public static async Task<string> ReadToStringAsync(this Stream value)
50+
{
51+
if (value == null)
52+
return null;
53+
using var streamReader = new StreamReader(value);
54+
return await streamReader.ReadToEndAsync();
55+
}
4256
}
4357
}

Net.Bluewalk.DotNetUtils/Extensions/StringExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,34 @@ public static int ToInt(this string value)
209209
return ToInt(value, 0);
210210
}
211211

212+
/// <summary>
213+
/// Convert string to Decimal.
214+
/// </summary>
215+
/// <param name="value">The value.</param>
216+
/// <param name="defaultValue">The default value.</param>
217+
/// <returns>System.Double.</returns>
218+
public static decimal ToDecimcal(this string value, decimal defaultValue)
219+
{
220+
if (string.IsNullOrEmpty(value)) return defaultValue;
221+
222+
if (!decimal.TryParse(value.Replace(',', '.'), NumberStyles.Any, CultureInfo.InvariantCulture,
223+
out var result))
224+
if (!decimal.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
225+
result = defaultValue;
226+
227+
return result;
228+
}
229+
230+
/// <summary>
231+
/// Convert string to Double.
232+
/// </summary>
233+
/// <param name="value">The value.</param>
234+
/// <returns>System.Double.</returns>
235+
public static decimal ToDecimcal(this string value)
236+
{
237+
return ToDecimcal(value, 0);
238+
}
239+
212240
/// <summary>
213241
/// String to NameValueCollection.
214242
/// </summary>

Net.Bluewalk.DotNetUtils/Extensions/TimeSpanExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,20 @@ public static TimeSpan StripMilliseconds(this TimeSpan time)
5252
{
5353
return new(time.Days, time.Hours, time.Minutes, time.Seconds);
5454
}
55+
56+
/// <summary>
57+
/// Rounds the specific TimeSpan to nearest minute
58+
/// </summary>
59+
/// <param name="input"></param>
60+
/// <param name="minutes"></param>
61+
/// <returns></returns>
62+
public static TimeSpan RoundToNearestMinutes(this TimeSpan input, int minutes)
63+
{
64+
var halfRange = new TimeSpan(0, minutes / 2, 0);
65+
if (input.Ticks < 0)
66+
halfRange = halfRange.Negate();
67+
var totalMinutes = (int)(input + halfRange).TotalMinutes;
68+
return new TimeSpan(0, totalMinutes - totalMinutes % minutes, 0);
69+
}
5570
}
5671
}

Net.Bluewalk.DotNetUtils/Extensions/TypeExtensions.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,50 @@ public static IEnumerable<PropertyInfo> GetPublicProperties(this Type type)
4444
.Concat(type.GetInterfaces())
4545
.SelectMany(i => i.GetProperties());
4646
}
47+
48+
/// <summary>
49+
/// Get the parent types
50+
/// </summary>
51+
/// <param name="type"></param>
52+
/// <returns></returns>
53+
public static IEnumerable<Type> GetParentTypes(this Type type)
54+
{
55+
// is there any base type?
56+
if (type == null)
57+
{
58+
yield break;
59+
}
60+
61+
// return all implemented or inherited interfaces
62+
foreach (var i in type.GetInterfaces())
63+
{
64+
yield return i;
65+
}
66+
67+
// return all inherited types
68+
var currentBaseType = type.BaseType;
69+
while (currentBaseType != null)
70+
{
71+
yield return currentBaseType;
72+
currentBaseType = currentBaseType.BaseType;
73+
}
74+
}
75+
76+
/// <summary>
77+
/// Checks if type implements given interface
78+
/// </summary>
79+
/// <typeparam name="TInterface"></typeparam>
80+
/// <param name="type"></param>
81+
/// <returns></returns>
82+
/// <exception cref="InvalidOperationException"></exception>
83+
public static bool Implements<TInterface>(this Type type) where TInterface : class
84+
{
85+
var interfaceType = typeof(TInterface);
86+
87+
if (!interfaceType.IsInterface)
88+
throw new InvalidOperationException("Only interfaces can be implemented.");
89+
90+
return (interfaceType.IsAssignableFrom(type));
91+
}
4792
}
4893
}

Net.Bluewalk.DotNetUtils/Net.Bluewalk.DotNetUtils.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1515
<RepositoryType>Git</RepositoryType>
1616
<PackageReleaseNotes></PackageReleaseNotes>
17-
<Version>2021.4.22.1</Version>
17+
<Version>2021.12.29.1</Version>
1818
<AssemblyVersion>2021.4.22.1</AssemblyVersion>
1919
<FileVersion>2021.4.22.1</FileVersion>
2020
</PropertyGroup>

0 commit comments

Comments
 (0)