Example #1
0
        internal static TTarget Convert <TTarget>(object source, ModelConvertOptions options)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return((TTarget)Convert(source, typeof(TTarget), options ?? Target <TTarget>().Options));
        }
Example #2
0
 internal PropertyConvertContext(object source, ModelPropertyInfo[] sourcePropertyChain, object target, ModelPropertyInfo targetProperty, object value, ModelConvertOptions options)
 {
     _sourceProperties = sourcePropertyChain;
     TargetProperty    = targetProperty;
     Value             = value;
     Source            = source;
     Target            = target;
     Options           = options;
 }
Example #3
0
        public static SourceOptions <T> Source <T>(T value)
        {
            ModelConvertOptions options = new ModelConvertOptions();

            var source = options.Source <T>();

            source.Value = value;

            return(source);
        }
Example #4
0
        internal static void WalkConvertiblProperties(object target, ModelInfo targetInfo, object source, ModelInfo sourceInfo,
                                                      ModelConvertOptions options, Action <ModelPropertyInfo, PropertyConvertContext, object> callback, object state)
        {
            foreach (var mp1 in targetInfo.Properties)
            {
                var mp0 = MapProperty(mp1, sourceInfo, options);

                if (mp0.Length > 0 && options.ShouldSetValue(mp1))
                {
                    PropertyConvertContext context = new PropertyConvertContext(source, mp0, target, mp1, GetValue(mp0, source), options);

                    callback(mp1, context, state);
                }
            }
        }
Example #5
0
        //public static object Convert(object source, Type type)
        //{
        //    if (source == null)
        //    {
        //        throw new ArgumentNullException(nameof(source));
        //    }

        //    if (type == null)
        //    {
        //        throw new ArgumentNullException(nameof(type));
        //    }

        //    return Convert(source, type, null);
        //}

        public static object Convert(object source, Type type, ModelConvertOptions options)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (options == null)
            {
                options = new ModelConvertOptions();
            }

            return(Convert(source, options.MatchSourceType(source.GetType()), type, options));
        }
Example #6
0
        //public static void Populate(object target, object source, Action<ModelConvertOptions> initOptions)
        //{
        //    ModelConvertOptions options;

        //    if (initOptions != null)
        //    {
        //        options = new ModelConvertOptions();
        //        initOptions(options);
        //    }
        //    else
        //        options = null;

        //    Populate(target, source, options);
        //}

        //public static void Populate(object target, object source)
        //{
        //    if (target == null)
        //    {
        //        throw new ArgumentNullException(nameof(target));
        //    }

        //    if (source == null)
        //    {
        //        throw new ArgumentNullException(nameof(source));
        //    }

        //    Populate(target, source, (ModelConvertOptions)null);
        //}

        //public static void Populate(object target, object source, ModelConvertOptions options)
        //{
        //    if (target == null)
        //    {
        //        throw new ArgumentNullException(nameof(target));
        //    }

        //    if (source == null)
        //    {
        //        throw new ArgumentNullException(nameof(source));
        //    }

        //    Populate(target, target.GetType(), source, source.GetType(), options);
        //}

        public static void Populate(object target, Type targetType, object source, Type sourceType, ModelConvertOptions options)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (options == null)
            {
                options = new ModelConvertOptions();
            }

            WalkConvertiblProperties(
                target, ModelInfo.GetModelInfo(options.MatchTargetType(targetType)),
                source, ModelInfo.GetModelInfo(options.MatchSourceType(sourceType)),
                options, SetPropertyValue, null);
        }
Example #7
0
 public SourceOptions(ModelConvertOptions model)
     : base(model, typeof(T))
 {
 }
Example #8
0
        private static object Convert(object source, Type sourceType, Type targetType, ModelConvertOptions options)
        {
            ModelInfo info0 = ModelInfo.GetModelInfo(sourceType)
            , info1         = ModelInfo.GetModelInfo(targetType);

            object target = info1.CreateInstance();

            WalkConvertiblProperties(target, info1, source, info0, options, SetPropertyValue, null);

            return(target);
        }
Example #9
0
        private static ModelPropertyInfo[] MapProperty(ModelPropertyInfo targetProperty, ModelInfo sourceModel, ModelConvertOptions options)
        {
            List <ModelPropertyInfo> chain = new List <ModelPropertyInfo>(3);
            string name = options.MapProperty(targetProperty) ?? targetProperty.GetPropertyMap(sourceModel.ModelType);

            if (name != null)
            {
                FillPropertyChain(sourceModel, name, chain);
            }
            else
            {
                var p0 = sourceModel.GetProperty(name = targetProperty.Name);

                if (p0 == null)
                {
                    Stack <ModelPropertyInfo> stack = null;

                    if (FindNestedProperty(name, sourceModel, ref stack))
                    {
                        chain.AddRange(stack);
                    }
                }
                else
                {
                    chain.Add(p0);
                }
            }

            return(chain.ToArray());
        }
Example #10
0
        public static TargetOptions <T> Target <T>()
        {
            ModelConvertOptions options = new ModelConvertOptions();

            return(options.Target <T>());
        }
Example #11
0
        //public static void Populate<TTarget, TSource>(TTarget target, TSource source)
        //{
        //    var options = new ModelConvertOptions();

        //    options
        //        .Source<TSource>()
        //        .Target<TTarget>();

        //    Populate(target, source, options);
        //}

        internal static void Populate <TTarget, TSource>(TTarget target, TSource source, ModelConvertOptions options)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            Populate(target, typeof(TTarget), source, typeof(TSource), options);
        }
Example #12
0
 public TargetOptions(ModelConvertOptions model, Type modelType)
     : base(model, modelType)
 {
 }