Sfoglia il codice sorgente

Added extra extension method that create a new instance of the destination object.

Erwin de Haan 6 anni fa
parent
commit
9c1c29325d

+ 3 - 6
Emby.Server.Implementations/Library/LibraryManager.cs

@@ -730,10 +730,8 @@ namespace Emby.Server.Implementations.Library
 
 
             _fileSystem.CreateDirectory(rootFolderPath);
             _fileSystem.CreateDirectory(rootFolderPath);
 
 
-            var tmpAFolder = new AggregateFolder();
-            ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(rootFolderPath))).DeepCopy<Folder,AggregateFolder>(tmpAFolder);            
-            var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? tmpAFolder;
-            
+            var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(rootFolderPath))).DeepCopy<Folder,AggregateFolder>();
+
             // In case program data folder was moved
             // In case program data folder was moved
             if (!string.Equals(rootFolder.Path, rootFolderPath, StringComparison.Ordinal))
             if (!string.Equals(rootFolder.Path, rootFolderPath, StringComparison.Ordinal))
             {
             {
@@ -801,8 +799,7 @@ namespace Emby.Server.Implementations.Library
 
 
                         if (tmpItem == null)
                         if (tmpItem == null)
                         {
                         {
-                            tmpItem = new UserRootFolder();                            
-                            ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath))).DeepCopy<Folder,UserRootFolder>(tmpItem);
+                            tmpItem = ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath))).DeepCopy<Folder,UserRootFolder>();
                         }
                         }
 
 
                         // In case program data folder was moved
                         // In case program data folder was moved

+ 16 - 1
MediaBrowser.Controller/Entities/BaseItemExtensions.cs

@@ -68,7 +68,9 @@ namespace MediaBrowser.Controller.Entities
         /// </summary>
         /// </summary>
         /// <param name="source">The source object.</param>
         /// <param name="source">The source object.</param>
         /// <param name="dest">The destination object.</param>
         /// <param name="dest">The destination object.</param>
-        public static void DeepCopy<T, TU>(this T source, TU dest)
+        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 sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList();
             var destProps = typeof(TU).GetProperties()
             var destProps = typeof(TU).GetProperties()
@@ -87,6 +89,19 @@ namespace MediaBrowser.Controller.Entities
 
 
         }
         }
 
 
+        /// <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;
+        }
+
 
 
     }
     }
 }
 }