示例#1
0
        protected override object SetValue(object sourcePropertyValue)
        {
            if (this.SourcePropType.IsSubclassOf(typeof(EntityBase)) && TargetPropType.IsSubclassOf(typeof(ModelBase)) ||
                this.SourcePropType.IsSubclassOf(typeof(ModelBase)) && TargetPropType.IsSubclassOf(typeof(EntityBase)))
            {
                if (sourcePropertyValue != null)
                {
                    var target = Activator.CreateInstance(TargetPropType);

                    target.InjectFrom<DomainToModelValueInjection>(sourcePropertyValue);

                    return target;
                }

                return null;
            }

            if (EnableCollectionInjection)
            {
                if (TargetPropType.IsGenericType &&
                   TargetPropType.GetGenericTypeDefinition() != null &&
                   TargetPropType.GetGenericTypeDefinition().GetInterfaces()
                                 .Contains(typeof(IEnumerable)) &&
                   SourcePropType.IsGenericType &&
                   SourcePropType.GetGenericTypeDefinition() != null &&
                   SourcePropType.GetGenericTypeDefinition().GetInterfaces()
                                 .Contains(typeof(IEnumerable)))
                {
                    var t = TargetPropType.GetGenericArguments()[0];
                    var tlist = typeof(List<>).MakeGenericType(t);
                    var addMethod = tlist.GetMethod("Add");

                    var sourceItems = sourcePropertyValue as IEnumerable;

                    var sourceT = SourcePropType.GetGenericArguments()[0];
                    if (sourceItems != null && typeof(ISortableEntity).IsAssignableFrom(sourceT))
                    {
                        sourceItems = sourceItems.Cast<ISortableEntity>().OrderBy(f => f.SortOrder);
                    }

                    if (sourceItems != null)
                    {
                        var target = Activator.CreateInstance(tlist);

                        foreach (var sourceItem in sourceItems)
                        {
                            var e = Activator.CreateInstance(t);

                            e.InjectFrom<DomainToModelValueInjection>(sourceItem);

                            addMethod.Invoke(target, new[] { e });
                        }

                        return target;
                    }
                }
            }

            return base.SetValue(sourcePropertyValue);
        }
示例#2
0
            protected override object SetValue(object sv)
            {
                if (IsGenericEnumerable(SourcePropType))
                {
                    var genArgs = SourcePropType.GetGenericArguments();


                    return(sv);
                }
                if (SourcePropType.IsValueType || SourcePropType == typeof(string))
                {
                    return(sv);
                }

                return(Activator.CreateInstance(SourcePropType).InjectFrom <CloneInjection>(sv));
            }