Skip to content

Commit 066901b

Browse files
committed
Refactor object cloning and simplify code structure
- Added a using directive for `ListFunctions.Extensions`. - Removed the `CopyObject` method and replaced it with `CloneIf()` for object cloning. - Simplified the retrieval of the `statement` variable by inlining the call to `_statementProp.Value.GetValue(info)`. - Updated handling of `variable.Value` to utilize the new `CloneIf()` method, improving clarity and efficiency.
1 parent bce17ad commit 066901b

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

src/engine/ListFunctions.Engine/Modern/Exceptions/ScriptBlockInvocationException.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ListFunctions.Internal;
1+
using ListFunctions.Extensions;
2+
using ListFunctions.Internal;
23
using System;
34
using System.Collections.Generic;
45
using System.Collections.ObjectModel;
@@ -64,22 +65,6 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
6465
}
6566

6667
#endif
67-
68-
[return: NotNullIfNotNull(nameof(obj))]
69-
protected static object? CopyObject(object? obj)
70-
{
71-
switch (obj)
72-
{
73-
case ICloneable cloneable:
74-
return cloneable.Clone();
75-
76-
case PSObject pso:
77-
return pso.Copy();
78-
79-
default:
80-
return obj;
81-
}
82-
}
8368
private static string FormatMessage(string message, Exception? inner, string? statement)
8469
{
8570
if (string.IsNullOrWhiteSpace(statement))
@@ -105,8 +90,7 @@ public static string GetScriptStatement(InvocationInfo info)
10590

10691
try
10792
{
108-
string? statement = _statementProp.Value.GetValue(info) as string;
109-
return statement ?? info.Line;
93+
return _statementProp.Value.GetValue(info) as string ?? info.Line;
11094
}
11195
catch (Exception e)
11296
{
@@ -135,7 +119,7 @@ private static void AddToDict(ref Dictionary<string, object?> dict, PSVariable?
135119
return;
136120
}
137121

138-
object? val = CopyObject(variable.Value);
122+
object? val = variable.Value.CloneIf();
139123

140124
#if NET5_0_OR_GREATER
141125
_ = dict.TryAdd(variable.Name, val);

0 commit comments

Comments
 (0)