diff options
| author | Joshua M. Boniface <joshua@boniface.me> | 2019-04-17 22:11:53 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-17 22:11:53 -0400 |
| commit | 0539861dc038b53cb23f353e0f68885aa0133bab (patch) | |
| tree | fa67ec105746d9870b3b445f2e6b8c41d58015c2 | |
| parent | deedf2a36c4e1ddf320922cf7c057aa6cb68af2e (diff) | |
| parent | 2f33e99006fd479ac9134ede80cbb990948d1261 (diff) | |
Merge pull request #1182 from Bond-009/deepcopy
Speed up DeepCopy
| -rw-r--r-- | MediaBrowser.Controller/Entities/BaseItemExtensions.cs | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs index 9c955a724..815239be2 100644 --- a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs +++ b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs @@ -64,21 +64,31 @@ namespace MediaBrowser.Controller.Entities 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(); + var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList(); - foreach (var sourceProp in sourceProps) + 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> @@ -93,7 +103,5 @@ namespace MediaBrowser.Controller.Entities source.DeepCopy(dest); return dest; } - - } } |
