Allow ReplacePlaceholders to handle null objects safely
Updated ReplacePlaceholders to accept nullable objects and skip nulls during placeholder resolution, preventing NullReferenceExceptions when nulls are passed in the objects array.
This commit is contained in:
@@ -18,7 +18,7 @@ public static partial class PlaceholderExtensions
|
|||||||
/// <exception cref="PlaceholderResolutionException">
|
/// <exception cref="PlaceholderResolutionException">
|
||||||
/// Thrown when a placeholder's column name cannot be resolved from any of the provided objects.
|
/// Thrown when a placeholder's column name cannot be resolved from any of the provided objects.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
public static string ReplacePlaceholders(this string str, params object[] objects)
|
public static string ReplacePlaceholders(this string str, params object?[] objects)
|
||||||
{
|
{
|
||||||
return PlaceholderRegex().Replace(str, match =>
|
return PlaceholderRegex().Replace(str, match =>
|
||||||
{
|
{
|
||||||
@@ -29,6 +29,9 @@ public static partial class PlaceholderExtensions
|
|||||||
|
|
||||||
foreach (var obj in objects)
|
foreach (var obj in objects)
|
||||||
{
|
{
|
||||||
|
if (obj is null)
|
||||||
|
continue;
|
||||||
|
|
||||||
var value = obj.GetValueByColumnName(columnName);
|
var value = obj.GetValueByColumnName(columnName);
|
||||||
if (value is not null)
|
if (value is not null)
|
||||||
return ToSqlLiteral(value);
|
return ToSqlLiteral(value);
|
||||||
|
|||||||
Reference in New Issue
Block a user