diff options
| author | LogicalPhallacy <44458166+LogicalPhallacy@users.noreply.github.com> | 2019-01-01 11:47:57 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-01 11:47:57 -0800 |
| commit | 443218e3f109cdfbffa8c8c5d30e09e3ba5c2285 (patch) | |
| tree | 6de8dee799eebba514bac0b6eac457b4c0137978 /MediaBrowser.Controller/Entities/BaseItemExtensions.cs | |
| parent | 0c52f448a0c6d5cd3c5cce597fcf3ad9582c90f1 (diff) | |
| parent | cff0ece07329bbfa05fd22fbde444d09aaeb9a5c (diff) | |
Merge pull request #4 from jellyfin/dev
Dev
Diffstat (limited to 'MediaBrowser.Controller/Entities/BaseItemExtensions.cs')
| -rw-r--r-- | MediaBrowser.Controller/Entities/BaseItemExtensions.cs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs index c56a370a8..8ab1788b5 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,46 @@ 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) + where T : BaseItem + where TU : BaseItem + { + 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); + } + + } + + } + + /// <summary> + /// Copies all properties on newly created object. Skips properties that do not exist. + /// </summary> + /// <param name="source">The source object.</param> + public static TU DeepCopy<T, TU>(this T source) + where T : BaseItem + where TU : BaseItem, new() + { + var dest = new TU(); + source.DeepCopy(dest); + return dest; + } + + } } |
