diff options
| author | Erwin de Haan <EraYaN@users.noreply.github.com> | 2018-12-30 01:12:33 +0100 |
|---|---|---|
| committer | Erwin de Haan <EraYaN@users.noreply.github.com> | 2018-12-30 01:12:33 +0100 |
| commit | de7fcaadb3d8fa77802ddcfb9bbd5b9306c49224 (patch) | |
| tree | cbe066dc2b0cbd284d4670e8ed771361e211bb4e /MediaBrowser.Controller/Entities/BaseItemExtensions.cs | |
| parent | 1f0b83c66a9946e12dba48b6457a1424a34ca87f (diff) | |
Fixed #320 by adding an extension method to BaseItem to make a deep copy of an object.
Diffstat (limited to 'MediaBrowser.Controller/Entities/BaseItemExtensions.cs')
| -rw-r--r-- | MediaBrowser.Controller/Entities/BaseItemExtensions.cs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs index c56a370a8..97e56d1fd 100644 --- a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs +++ b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -61,5 +62,31 @@ namespace MediaBrowser.Controller.Entities item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file)); } } + + /// <summary> + /// Copies all properties on object. Skips properties that do not exist. + /// </summary> + /// <param name="source">The source object.</param> + /// <param name="dest">The destination object.</param> + public static void DeepCopy<T, TU>(this T source, TU dest) + { + 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) + { + if (destProps.Any(x => x.Name == sourceProp.Name)) + { + var p = destProps.First(x => x.Name == sourceProp.Name); + p.SetValue(dest, sourceProp.GetValue(source, null), null); + } + + } + + } + + } } |
