-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Service factory improvements
- Loading branch information
Showing
5 changed files
with
135 additions
and
28 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Forge.OpenAI.Infrastructure | ||
{ | ||
|
||
/// <summary>Make a copy about the source instance into the destination one</summary> | ||
public static class MappingHelper | ||
{ | ||
|
||
public static void Map<TSource, TDestination>(TSource source, TDestination destination, bool throwErrorOnMissingTargetProperty = false) | ||
{ | ||
if (source == null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
|
||
if (destination == null) | ||
{ | ||
throw new ArgumentNullException(nameof(destination)); | ||
} | ||
|
||
Type sourceType = source.GetType(); | ||
Type destinationType = destination.GetType(); | ||
|
||
foreach (PropertyInfo sourceProperty in sourceType.GetProperties()) | ||
{ | ||
PropertyInfo destinationProperty = destinationType.GetProperty(sourceProperty.Name); | ||
|
||
if (destinationProperty != null) | ||
{ | ||
destinationProperty.SetValue(destination, sourceProperty.GetValue(source)); | ||
} | ||
else if (throwErrorOnMissingTargetProperty) | ||
{ | ||
throw new InvalidOperationException($"Property {sourceProperty.Name} not found in destination object"); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
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