-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c38c421
commit 4f6b156
Showing
14 changed files
with
302 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 23 additions & 52 deletions
75
ManagedCode.Storage.Core/Extensions/StorageOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,23 @@ | ||
|
||
|
||
using System; | ||
using System.Reflection; | ||
|
||
namespace ManagedCode.Storage.Core.Extensions; | ||
|
||
public static class StorageOptionsExtensions | ||
{ | ||
public static T DeepCopy<T>(this T? source) where T : class, IStorageOptions | ||
{ | ||
if (source == null) | ||
return default; | ||
|
||
// Create new instance of the same type | ||
var instance = Activator.CreateInstance<T>(); | ||
|
||
// Get all properties of the type | ||
var properties = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); | ||
|
||
foreach (var property in properties) | ||
{ | ||
if (property.CanWrite && property.CanRead) | ||
{ | ||
var value = property.GetValue(source); | ||
if (value != null) | ||
{ | ||
// Handle value types and strings | ||
if (property.PropertyType.IsValueType || property.PropertyType == typeof(string)) | ||
{ | ||
property.SetValue(instance, value); | ||
} | ||
// Handle reference types by recursive deep copy | ||
else | ||
{ | ||
var deepCopyMethod = typeof(StorageOptionsExtensions) | ||
.GetMethod(nameof(DeepCopy)) | ||
?.MakeGenericMethod(property.PropertyType); | ||
|
||
if (deepCopyMethod != null) | ||
{ | ||
var copiedValue = deepCopyMethod.Invoke(null, [value]); | ||
property.SetValue(instance, copiedValue); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return instance; | ||
} | ||
} | ||
// using System; | ||
// using System.Reflection; | ||
// using System.Text.Json; | ||
// | ||
// namespace ManagedCode.Storage.Core.Extensions; | ||
// | ||
// public static class StorageOptionsExtensions | ||
// { | ||
// public static T DeepCopy<T>(this T? source) where T : class, IStorageOptions | ||
// { | ||
// if (source == null) | ||
// return default; | ||
// | ||
// var options = new JsonSerializerOptions | ||
// { | ||
// WriteIndented = false, | ||
// PropertyNameCaseInsensitive = true | ||
// }; | ||
// | ||
// var json = JsonSerializer.Serialize(source, source.GetType(), options); | ||
// return (T)JsonSerializer.Deserialize(json, source.GetType(), options)!; | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.