-
Notifications
You must be signed in to change notification settings - Fork 79
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
Showing
63 changed files
with
3,407 additions
and
877 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Damselfly.Core.Utils | ||
{ | ||
/// <summary> | ||
/// https://stackoverflow.com/questions/3445784/copy-the-property-values-to-another-object-with-c-sharp | ||
/// </summary> | ||
public static class ObjectUtils | ||
{ | ||
public static bool CopyPropertiesTo<T, TU>(this T source, TU dest) | ||
{ | ||
bool changed = false; | ||
var sourceProps = typeof(T).GetProperties().Where(x => x.CanRead).ToList(); | ||
var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList(); | ||
|
||
foreach (var sourceProp in sourceProps) | ||
{ | ||
var destProp = destProps.FirstOrDefault(x => x.Name == sourceProp.Name); | ||
|
||
if (destProp != null ) | ||
{ | ||
var newVal = sourceProp.GetValue(source, null); | ||
var prevVal = destProp.GetValue(dest, null); | ||
|
||
if (newVal == null && prevVal == null) | ||
continue; | ||
|
||
if( newVal == null ) | ||
{ | ||
destProp.SetValue(dest, null); | ||
Logging.LogVerbose($"Setting property {destProp.Name} to NULL"); | ||
changed = true; | ||
continue; | ||
} | ||
|
||
if( ! newVal.Equals( prevVal ) ) | ||
{ | ||
Logging.LogVerbose($"Setting property {destProp.Name} to {newVal}"); | ||
// check if the property can be set or no. | ||
destProp.SetValue(dest, newVal); | ||
changed = true; | ||
} | ||
} | ||
} | ||
|
||
return changed; | ||
} | ||
} | ||
} | ||
|
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.