예제 #1
0
    private static TDestination MapObject <TDestination>(
        object source, IDictionary <string, PropertyData> propertyDataMap, object overrides = null,
        TDestination destination = null, IEnumerable <string> ignoreProperties = null, bool ignoreSourceNullValues = true)
        where TDestination : class, new()
    {
        if (source == null)
        {
            return(null);
        }

        destination = destination ?? new TDestination();

        foreach (var propertyDataKvp in propertyDataMap)
        {
            var propertyName = propertyDataKvp.Key;
            var propertyData = propertyDataKvp.Value;

            // property ignores
            if (ignoreProperties != null && ignoreProperties.Contains(propertyName))
            {
                continue;
            }

            // get from overrides
            object sourcePropertyValue;
            if (!LightObjectMapper.GetFromOverrides(overrides, propertyName, out sourcePropertyValue))
            {
                // not in overrides
                if (propertyData.sourcePropertyInfo == null) // not in source property either, ignore
                {
                    continue;
                }

                sourcePropertyValue = propertyData.sourcePropertyInfo.GetValue(source); // fetch from source property
                if (sourcePropertyValue == null && ignoreSourceNullValues)              // ignore if source null values are ignored
                {
                    continue;
                }
            }

            // apply type converter
            if (LightObjectMapper.typeConverters.Count > 0)
            {
                var sourceObjectType = sourcePropertyValue?.GetType() ?? propertyData.sourcePropertyInfo?.PropertyType;
                if (sourceObjectType != null)
                {
                    Func <object, object> typeConverter;
                    if (LightObjectMapper.typeConverters.TryGetValue(sourceObjectType, out typeConverter) && typeConverter != null)
                    {
                        sourcePropertyValue = typeConverter(sourcePropertyValue);
                    }
                }
            }

            // set in destination object
            propertyData.destinationPropertyInfo.SetValue(destination, sourcePropertyValue);
        }

        return(destination);
    }
예제 #2
0
    public static TDestination MapObject <TDestination>(
        object source, TDestination destination, object overrides = null, IEnumerable <string> ignoreProperties = null, bool ignoreSourceNullValues = false)
        where TDestination : class, new()
    {
        if (source == null)
        {
            return(null);
        }

        var propertyDataMap = LightObjectMapper.GetPropertyDataMap(source.GetType(), typeof(TDestination));

        return(LightObjectMapper.MapObject <TDestination>(source, propertyDataMap, overrides, destination, ignoreProperties, ignoreSourceNullValues));
    }
예제 #3
0
    public static ICollection <TDestination> MapObjects <TSource, TDestination>(
        IEnumerable <TSource> sourceEnumerable, Func <TSource, object> overridesGetter = null, IEnumerable <string> ignoreProperties = null, bool ignoreSourceNullValues = true)
        where TDestination : class, new()
    {
        if (sourceEnumerable == null)
        {
            return(null);
        }

        var destination = new List <TDestination>(sourceEnumerable.Count());

        var propertyDataMap = LightObjectMapper.GetPropertyDataMap(typeof(TSource), typeof(TDestination));

        foreach (var sourceElem in sourceEnumerable)
        {
            var overrides       = overridesGetter?.Invoke(sourceElem);
            var destinationElem = LightObjectMapper.MapObject <TDestination>(sourceElem, propertyDataMap, overrides, null, ignoreProperties, ignoreSourceNullValues);
            destination.Add(destinationElem);
        }

        return(destination);
    }
예제 #4
0
 public static TDestination MapObject <TDestination>(
     object source, object overrides = null, IEnumerable <string> ignoreProperties = null)
     where TDestination : class, new()
 {
     return(LightObjectMapper.MapObject <TDestination>(source, null, overrides, ignoreProperties, true));
 }