Skip to content

Commit

Permalink
Update license, fix compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Wilkinson committed Jan 10, 2024
1 parent 228b983 commit 5f8bf13
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class DiscriminatedUnionJsonConverter : JsonConverter<DiscriminatedUnio
{
private const int MaxDepth = 50;

private static readonly ConcurrentDictionary<Type, Dictionary<string, Func<object, object>>> Getters = new ConcurrentDictionary<Type, Dictionary<string, Func<object, object>>>();
private static readonly ConcurrentDictionary<Type, Dictionary<string, Func<object, object?>>> Getters = new();

public override bool CanConvert(Type typeToConvert)
=> typeof(DiscriminatedUnion).IsAssignableFrom(typeToConvert);
Expand All @@ -26,7 +26,7 @@ public override DiscriminatedUnion Read(ref Utf8JsonReader reader, Type typeToCo
public override void Write(Utf8JsonWriter writer, DiscriminatedUnion value, JsonSerializerOptions options)
=> Write(writer, value.Value);

private static void Write(Utf8JsonWriter writer, object value, int depth = 0)
private static void Write(Utf8JsonWriter writer, object? value, int depth = 0)
{
if (++depth >= MaxDepth)
{
Expand Down Expand Up @@ -107,7 +107,7 @@ private static void Write(Utf8JsonWriter writer, object value, int depth = 0)
var type = value.GetType();
if (!Getters.TryGetValue(type, out var gettersByName))
{
gettersByName = type.GetProperties().ToDictionary(x => x.Name, x => x.GetGetter(x.DeclaringType));
gettersByName = type.GetProperties().ToDictionary(x => x.Name, x => x.GetGetter(x.DeclaringType!));
Getters.TryAdd(type, gettersByName);
}

Expand Down
4 changes: 2 additions & 2 deletions DataTables.Blazor/DataTables.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>3.6.0</Version>
<Version>3.6.1</Version>
<AssemblyName>DataTables.Blazor</AssemblyName>
<PackageId>DataTables.Blazor</PackageId>
<Description>A basic port for jquery DataTables into Blazor.</Description>
Expand All @@ -19,7 +19,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<IsPackable>true</IsPackable>
<Copyright>Justin Wilkinson (2023)</Copyright>
<Copyright>Justin Wilkinson (2024)</Copyright>
<Authors>Justin Wilkinson, Tim Larson, Jake Soenneker</Authors>
<Company />
<PackageTags>blazor datatables interops</PackageTags>
Expand Down
13 changes: 7 additions & 6 deletions DataTables.Blazor/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ internal static class ReflectionExtensions
/// <param name="property">Property to obtain setter of.</param>
/// <param name="type">Type of instance.</param>
/// <returns>A delegate that invokes the property setter.</returns>
public static Func<object, object> GetGetter(this PropertyInfo property, Type type) => CreateGetDelegate(property.GetMethod, type);
public static Func<object, object?> GetGetter(this PropertyInfo property, Type type)
=> CreateGetDelegate(property.GetMethod!, type);

#region Helpers
private static readonly MethodInfo GenericGetHelper = typeof(ReflectionExtensions).GetMethod(nameof(GetDelegateHelper), BindingFlags.Static | BindingFlags.NonPublic);
private static readonly MethodInfo GenericGetHelper = typeof(ReflectionExtensions).GetMethod(nameof(GetDelegateHelper), BindingFlags.Static | BindingFlags.NonPublic)!;

/// <summary>
/// Helper method that returns a getter for a property using the object class.
/// </summary>
/// <param name="method">Method Info to pass</param>
/// <param name="type">Type of instance</param>
/// <returns></returns>
private static Func<object, object> CreateGetDelegate(MethodInfo method, Type type)
private static Func<object, object?> CreateGetDelegate(MethodInfo method, Type type)
{
// Supply the type arguments to the generic helper.
var genericMethod = GenericGetHelper.MakeGenericMethod(type, method.ReturnType);

// Cast the result to the right kind of delegate and return it. The null argument is because it's a static method
return (Func<object, object>)genericMethod.Invoke(null, new[] { method });
return (Func<object, object?>)genericMethod.Invoke(null, new[] { method })!;
}

/// <summary>
Expand All @@ -41,13 +42,13 @@ private static Func<object, object> CreateGetDelegate(MethodInfo method, Type ty
/// <typeparam name="TParam">Parameter type</typeparam>
/// <param name="method">Get Method</param>
/// <returns>A weakly typed delegate that calls a strongly type one.</returns>
private static Func<object, object> GetDelegateHelper<TTarget, TParam>(MethodInfo method) where TTarget : class
private static Func<object, object?> GetDelegateHelper<TTarget, TParam>(MethodInfo method) where TTarget : class
{
// Convert the slow MethodInfo into a fast, strongly typed, open delegate
var func = CreateTypedFunction<TTarget, TParam>(method);

// Now create a more weakly typed delegate which will call the strongly typed one
return target => (TParam)Convert.ChangeType(func((TTarget)Convert.ChangeType(target, typeof(TTarget))), typeof(TParam));
return target => (TParam?)Convert.ChangeType(func((TTarget)Convert.ChangeType(target, typeof(TTarget))), typeof(TParam));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Justin Wilkinson
Copyright (c) 2024 Justin Wilkinson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit 5f8bf13

Please sign in to comment.