Skip to content

Commit 66ae612

Browse files
Added more type
1 parent 0a3520b commit 66ae612

File tree

8 files changed

+210
-24
lines changed

8 files changed

+210
-24
lines changed

src/Reflector/Is.cs

+26-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,30 @@ public static bool Equal(this DateTimeOffset actual, DateTimeOffset expected, Ti
1313
(actual - expected).Duration() < tolerance;
1414
public static bool Equal(this TimeOnly actual, TimeOnly expected, TimeSpan tolerance) => (actual - expected).Duration() < tolerance;
1515
public static bool Equal(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance) => (actual - expected).Duration() < tolerance;
16-
16+
public static bool IsCloseTo(this TimeOnly subject, TimeOnly other, TimeSpan precision)
17+
{
18+
long startTicks = other.Add(-precision).Ticks;
19+
long endTicks = other.Add(precision).Ticks;
20+
long ticks = subject.Ticks;
21+
22+
return startTicks <= endTicks
23+
? startTicks <= ticks && endTicks >= ticks
24+
: startTicks <= ticks || endTicks >= ticks;
25+
}
26+
public static DateTimeOffset ToDateTimeOffset(this DateTime dateTime)
27+
{
28+
return dateTime.ToDateTimeOffset(TimeSpan.Zero);
29+
}
30+
public static DateTimeOffset ToDateTimeOffset(this DateTime dateTime, TimeSpan offset)
31+
{
32+
return new DateTimeOffset(DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified), offset);
33+
}
34+
public static long ToUnixTimeMilliseconds(this DateTime dateTime)
35+
{
36+
return dateTime.ToDateTimeOffset(TimeSpan.Zero).ToUnixTimeMilliseconds();
37+
}
38+
public static long ToUnixTimeSeconds(this DateTime dateTime)
39+
{
40+
return dateTime.ToDateTimeOffset(TimeSpan.Zero).ToUnixTimeSeconds();
41+
}
1742
}

src/Reflector/IsEvent.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace VReflector;
55

66
public static class IsEvent
77
{
8-
public static string GetEventAccessModifier(this EventInfo @event)
8+
public static AccessModifier? GetEventAccessModifier(this EventInfo @event)
99
{
1010
var method = @event?.GetAddMethod() ?? @event?.GetRemoveMethod(true);
11-
return method?.GetMethodAccessModifier() ?? string.Empty;
11+
return method?.GetMethodAccessModifier();
1212
}
1313
public static string GetEventModifiers(this EventInfo @event)
1414
{

src/Reflector/IsField.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ public static bool IsConstant(this FieldInfo field)
1515
{
1616
return field != null && field.IsLiteral;
1717
}
18-
public static string GetFieldAccessModifier(this FieldInfo field)
18+
public static AccessModifier GetFieldAccessModifier(this FieldInfo field)
1919
{
2020
return field switch
2121
{
22-
_ when field.IsPrivate => "private",
23-
_ when field.IsPublic => "public",
24-
_ when field.IsFamilyOrAssembly => "protected internal",
25-
_ when field.IsFamily => "protected",
26-
_ when field.IsAssembly => "internal",
27-
_ => string.Empty
22+
_ when field.IsPrivate => AccessModifier.Private,
23+
_ when field.IsPublic => AccessModifier.Public,
24+
_ when field.IsFamilyOrAssembly => AccessModifier.ProtectedInternal,
25+
_ when field.IsFamily => AccessModifier.Protected,
26+
_ when field.IsAssembly => AccessModifier.Internal,
27+
_ => AccessModifier.Internal
2828
};
2929
}
3030
public static string GetFieldModifiers(this FieldInfo field)

src/Reflector/IsMethod.cs

+34-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
1-
using System.Reflection;
1+
using System.Linq.Expressions;
2+
using System.Reflection;
3+
using System.Runtime.CompilerServices;
24

35
namespace VReflector;
46

57
public static class IsMethod
68
{
7-
public static string GetMethodAccessModifier(this MethodInfo method)
9+
public static AccessModifier GetMethodAccessModifier(this MethodBase method)
810
{
911
return method switch
1012
{
11-
_ when method.IsPrivate => "private",
12-
_ when method.IsPublic => "public",
13-
_ when method.IsFamilyOrAssembly => "protected internal",
14-
_ when method.IsFamily => "protected",
15-
_ when method.IsAssembly => "internal",
16-
_ => string.Empty
13+
_ when method.IsPrivate => AccessModifier.Private,
14+
_ when method.IsPublic => AccessModifier.Public,
15+
_ when method.IsFamilyOrAssembly => AccessModifier.ProtectedInternal,
16+
_ when method.IsFamily => AccessModifier.Protected,
17+
_ when method.IsAssembly => AccessModifier.Internal,
18+
_ => AccessModifier.Internal
19+
};
20+
}
21+
public static AccessModifier GetMethodAccessModifier(this MethodInfo method)
22+
{
23+
return method switch
24+
{
25+
_ when method.IsPrivate => AccessModifier.Private,
26+
_ when method.IsPublic => AccessModifier.Public,
27+
_ when method.IsFamilyOrAssembly => AccessModifier.ProtectedInternal,
28+
_ when method.IsFamily => AccessModifier.Protected,
29+
_ when method.IsAssembly => AccessModifier.Internal,
30+
_ => AccessModifier.Internal
1731
};
1832
}
1933
public static string GetMethodModifiers(this MethodInfo method)
@@ -27,4 +41,16 @@ public static string GetMethodModifiers(this MethodInfo method)
2741
_ => string.Empty
2842
};
2943
}
44+
public static bool IsNonVirtual(this MethodInfo method)
45+
{
46+
return !method.IsVirtual || method.IsFinal;
47+
}
48+
public static bool IsVirtualOrAbstract(this MethodInfo method)
49+
{
50+
return method.IsVirtual && !method.IsFinal || method.IsAbstract;
51+
}
52+
public static bool IsAsync(this MethodInfo methodInfo)
53+
{
54+
return methodInfo.IsDecoratedWith<AsyncStateMachineAttribute>();
55+
}
3056
}

src/Reflector/IsProperty.cs

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Reflection;
23

34
namespace VReflector;
45

@@ -13,7 +14,7 @@ public static bool IsIndexer(this PropertyInfo member)
1314
return member.GetIndexParameters().Length != 0;
1415
}
1516

16-
public static bool SetIsAllowed(this PropertyInfo pInfo, bool checkNonPublicSetter = false, bool checkInitSetter = false)
17+
public static bool SetIsAllowed([DisallowNull]this PropertyInfo pInfo, bool checkNonPublicSetter = false, bool checkInitSetter = false)
1718
{
1819
var setMethod = pInfo.GetSetMethod(nonPublic: checkNonPublicSetter);
1920
if (setMethod == null)
@@ -37,12 +38,29 @@ public static bool SetIsAllowed(this PropertyInfo pInfo, bool checkNonPublicSett
3738

3839
}
3940

40-
public static string GetPropertyAccessModifier(this PropertyInfo property)
41+
public static AccessModifier? GetPropertyAccessModifier([DisallowNull] this PropertyInfo property)
4142
{
4243
var method = property?.GetMethod ?? property?.SetMethod;
43-
return method?.GetMethodAccessModifier() ?? string.Empty;
44+
return method?.GetMethodAccessModifier();
4445
}
45-
public static string GetPropertyModifiers(this PropertyInfo property)
46+
public static bool IsVirtual([DisallowNull] this PropertyInfo property)
47+
{
48+
MethodInfo? methodInfo = property?.GetGetMethod(nonPublic: true) ?? property?.GetSetMethod(nonPublic: true);
49+
return !methodInfo?.IsNonVirtual() ?? false;
50+
}
51+
52+
public static bool IsStatic([DisallowNull] this PropertyInfo property)
53+
{
54+
MethodInfo? methodInfo = property?.GetGetMethod(nonPublic: true) ?? property?.GetSetMethod(nonPublic: true);
55+
return methodInfo?.IsStatic ?? false;
56+
}
57+
58+
public static bool IsAbstract([DisallowNull] this PropertyInfo property)
59+
{
60+
MethodInfo? methodInfo = property?.GetGetMethod(nonPublic: true) ?? property?.GetSetMethod(nonPublic: true);
61+
return methodInfo?.IsAbstract ?? false;
62+
}
63+
public static string GetPropertyModifiers([DisallowNull] this PropertyInfo property)
4664
{
4765
var method = property?.GetMethod ?? property?.SetMethod;
4866
return method?.GetMethodModifiers() ?? string.Empty;

src/Reflector/IsSequence.cs

+77
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,83 @@ public static bool ContainsAllKeys<TKey, TValue>([DisallowNull] this IDictionary
299299

300300
return keys.All(key => dictionary.ContainsKey(key));
301301
}
302+
public static IEnumerable<TKey> GetKeys<TCollection, TKey, TValue>(this TCollection collection)
303+
where TCollection : IEnumerable<KeyValuePair<TKey, TValue>>
304+
{
305+
return collection switch
306+
{
307+
IDictionary<TKey, TValue> dictionary => dictionary.Keys,
308+
IReadOnlyDictionary<TKey, TValue> readOnlyDictionary => readOnlyDictionary.Keys,
309+
_ => collection.Select(kvp => kvp.Key).ToList(),
310+
};
311+
}
312+
public static IEnumerable<TValue> GetValues<TCollection, TKey, TValue>(this TCollection collection)
313+
where TCollection : IEnumerable<KeyValuePair<TKey, TValue>>
314+
{
315+
return collection switch
316+
{
317+
IDictionary<TKey, TValue> dictionary => dictionary.Values,
318+
IReadOnlyDictionary<TKey, TValue> readOnlyDictionary => readOnlyDictionary.Values,
319+
_ => collection.Select(kvp => kvp.Value).ToList(),
320+
};
321+
}
322+
public static bool ContainsKey<TCollection, TKey, TValue>(this TCollection collection, TKey key)
323+
where TCollection : IEnumerable<KeyValuePair<TKey, TValue>>
324+
{
325+
return collection switch
326+
{
327+
IDictionary<TKey, TValue> dictionary => dictionary.ContainsKey(key),
328+
IReadOnlyDictionary<TKey, TValue> readOnlyDictionary => readOnlyDictionary.ContainsKey(key),
329+
_ => ContainsKey(collection, key),
330+
};
302331

332+
static bool ContainsKey(TCollection collection, TKey key)
333+
{
334+
var comparer = Comparer<TKey>.Default;
335+
return collection.Any(kvp => comparer.Compare(kvp.Key, key)==0);
336+
}
337+
}
338+
public static bool TryGetValue<TCollection, TKey, TValue>(this TCollection collection, TKey key, out TValue value)
339+
where TCollection : IEnumerable<KeyValuePair<TKey, TValue>>
340+
{
341+
return collection switch
342+
{
343+
IDictionary<TKey, TValue> dictionary => dictionary.TryGetValue(key, out value),
344+
IReadOnlyDictionary<TKey, TValue> readOnlyDictionary => readOnlyDictionary.TryGetValue(key, out value),
345+
_ => TryGetValue(collection, key, out value),
346+
};
347+
348+
static bool TryGetValue(TCollection collection, TKey key, out TValue value)
349+
{
350+
var comparer = Comparer<TKey>.Default;
303351

352+
foreach (var kvp in collection)
353+
{
354+
if (comparer.Compare(kvp.Key, key) == 0)
355+
{
356+
value = kvp.Value;
357+
return true;
358+
}
359+
}
360+
361+
value = default;
362+
return false;
363+
}
364+
}
365+
public static TValue GetValue<TCollection, TKey, TValue>(this TCollection collection, TKey key)
366+
where TCollection : IEnumerable<KeyValuePair<TKey, TValue>>
367+
{
368+
return collection switch
369+
{
370+
IDictionary<TKey, TValue> dictionary => dictionary[key],
371+
IReadOnlyDictionary<TKey, TValue> readOnlyDictionary => readOnlyDictionary[key],
372+
_ => GetValue(collection, key),
373+
};
374+
375+
static TValue GetValue(TCollection collection, TKey key)
376+
{
377+
var comparer = Comparer<TKey>.Default;
378+
return collection.First(kvp => comparer.Compare(kvp.Key, key) == 0).Value;
379+
}
380+
}
304381
}

src/Reflector/IsString.cs

+40
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,44 @@ public static bool StringEqualIgnoreCase(string? actual, string? expected)
152152
{
153153
return StringComparer.OrdinalIgnoreCase.Equals(actual, expected);
154154
}
155+
public static int IndexOfFirstMismatch(this string value, string expected, IEqualityComparer<string> comparer)
156+
{
157+
for (int index = 0; index < value.Length; index++)
158+
{
159+
if (index >= expected.Length || !comparer.Equals(value[index..(index + 1)], expected[index..(index + 1)]))
160+
{
161+
return index;
162+
}
163+
}
164+
165+
return -1;
166+
}
167+
public static string? Capitalize(this string? @this)
168+
{
169+
if (@this ==null || @this.Length == 0)
170+
{
171+
return @this;
172+
}
173+
174+
char[] charArray = @this.ToCharArray();
175+
charArray[0] = char.ToUpperInvariant(charArray[0]);
176+
return new string(charArray);
177+
}
178+
public static int CountSubstring(this string str, string substring, IEqualityComparer<string> comparer)
179+
{
180+
string actual = str ?? string.Empty;
181+
string search = substring ?? string.Empty;
182+
183+
int count = 0;
184+
int maxIndex = actual.Length - search.Length;
185+
for (int index = 0; index <= maxIndex; index++)
186+
{
187+
if (comparer.Equals(actual[index..(index + search.Length)], search))
188+
{
189+
count++;
190+
}
191+
}
192+
193+
return count;
194+
}
155195
}

src/Reflector/VReflector.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<VersionPrefix>2.1.1.1</VersionPrefix>
7+
<VersionPrefix>2.1.1.2</VersionPrefix>
88
<SignAssembly>True</SignAssembly>
99
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1010
<Title>VReflector</Title>

0 commit comments

Comments
 (0)