aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Entities/BaseItemExtensions.cs
diff options
context:
space:
mode:
authorWWWesten <4700006+WWWesten@users.noreply.github.com>2021-11-01 23:43:29 +0500
committerGitHub <noreply@github.com>2021-11-01 23:43:29 +0500
commit0a14279e2a21bcb9654a06a2d49e1e4f0cc5329c (patch)
treee1b1bd603b011ca98e5793e356326bf4a35a7050 /MediaBrowser.Controller/Entities/BaseItemExtensions.cs
parentf2817fef743eeb75a00782ceea363b2d3e7dc9f2 (diff)
parent76eeb8f655424d295e73ced8349c6fefee6ddb12 (diff)
Merge branch 'jellyfin:master' into master
Diffstat (limited to 'MediaBrowser.Controller/Entities/BaseItemExtensions.cs')
-rw-r--r--MediaBrowser.Controller/Entities/BaseItemExtensions.cs61
1 files changed, 44 insertions, 17 deletions
diff --git a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs
index 9c955a724..33870e2fb 100644
--- a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs
+++ b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs
@@ -1,3 +1,6 @@
+#pragma warning disable CS1591
+
+using System;
using System.Linq;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
@@ -41,9 +44,10 @@ namespace MediaBrowser.Controller.Entities
/// <param name="file">The file.</param>
public static void SetImagePath(this BaseItem item, ImageType imageType, string file)
{
- if (file.StartsWith("http", System.StringComparison.OrdinalIgnoreCase))
+ if (file.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
- item.SetImage(new ItemImageInfo
+ item.SetImage(
+ new ItemImageInfo
{
Path = file,
Type = imageType
@@ -60,40 +64,63 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
/// <param name="source">The source object.</param>
/// <param name="dest">The destination object.</param>
+ /// <typeparam name="T">Source type.</typeparam>
+ /// <typeparam name="TU">Destination type.</typeparam>
public static void DeepCopy<T, TU>(this T source, TU dest)
- where T : BaseItem
- where TU : BaseItem
+ 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();
+ if (source == null)
+ {
+ throw new ArgumentNullException(nameof(source));
+ }
+
+ if (dest == null)
+ {
+ throw new ArgumentNullException(nameof(dest));
+ }
- foreach (var sourceProp in sourceProps)
+ var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList();
+
+ foreach (var sourceProp in typeof(T).GetProperties())
{
- if (destProps.Any(x => x.Name == sourceProp.Name))
+ // We should be able to write to the property
+ // for both the source and destination type
+ // This is only false when the derived type hides the base member
+ // (which we shouldn't copy anyway)
+ if (!sourceProp.CanRead || !sourceProp.CanWrite)
{
- var p = destProps.First(x => x.Name == sourceProp.Name);
- p.SetValue(dest, sourceProp.GetValue(source, null), null);
+ continue;
}
- }
+ var v = sourceProp.GetValue(source);
+ if (v == null)
+ {
+ continue;
+ }
+ var p = destProps.Find(x => x.Name == sourceProp.Name);
+ if (p != null)
+ {
+ p.SetValue(dest, v);
+ }
+ }
}
/// <summary>
/// Copies all properties on newly created object. Skips properties that do not exist.
/// </summary>
/// <param name="source">The source object.</param>
+ /// <typeparam name="T">Source type.</typeparam>
+ /// <typeparam name="TU">Destination type.</typeparam>
+ /// <returns>Destination object.</returns>
public static TU DeepCopy<T, TU>(this T source)
- where T : BaseItem
- where TU : BaseItem, new()
+ where T : BaseItem
+ where TU : BaseItem, new()
{
var dest = new TU();
source.DeepCopy(dest);
return dest;
}
-
-
}
}