mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 02:37:08 -07:00
added resource mapping validation tests
This commit is contained in:
parent
1d49435675
commit
c3214a2e88
17 changed files with 297 additions and 76 deletions
41
NzbDrone.Api/Mapping/ValueInjectorExtensions.cs
Normal file
41
NzbDrone.Api/Mapping/ValueInjectorExtensions.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Omu.ValueInjecter;
|
||||
|
||||
namespace NzbDrone.Api.Mapping
|
||||
{
|
||||
public static class ValueInjectorExtensions
|
||||
{
|
||||
public static TTarget InjectTo<TTarget>(this object source) where TTarget : new()
|
||||
{
|
||||
var targetType = typeof(TTarget);
|
||||
|
||||
if (targetType.IsGenericType &&
|
||||
targetType.GetGenericTypeDefinition() != null &&
|
||||
targetType.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)) &&
|
||||
source.GetType().IsGenericType &&
|
||||
source.GetType().GetGenericTypeDefinition() != null &&
|
||||
source.GetType().GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
|
||||
{
|
||||
|
||||
var result = new TTarget();
|
||||
|
||||
var listSubType = targetType.GetGenericArguments()[0];
|
||||
var listType = typeof(List<>).MakeGenericType(listSubType);
|
||||
var addMethod = listType.GetMethod("Add");
|
||||
|
||||
foreach (var sourceItem in (IEnumerable)source)
|
||||
{
|
||||
var e = Activator.CreateInstance(listSubType).InjectFrom(sourceItem);
|
||||
addMethod.Invoke(result, new[] { e });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return (TTarget)new TTarget().InjectFrom(source);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue