Exemple #1
0
        public static dynamic Assign(object target, params object[] sources)
        {
            var oSrc = new ExpandoWrapper(target);

            foreach (var source in sources)
            {
                var dSrc = new ExpandoWrapper(source);
                foreach (var kv in dSrc.Properties)
                {
                    oSrc[kv.Key] = kv.Value;
                }
            }

            return(oSrc);
        }
Exemple #2
0
        public static T Assign <T>(object target, params object[] sources) where T : class
        {
            var instance = Activator.CreateInstance <T>();

            foreach (var source in sources.Prepend(target))
            {
                var dSrc = new ExpandoWrapper(source);

                var properties = typeof(T).GetProperties();
                foreach (var keyValuePair in dSrc.Properties)
                {
                    var matchingPropertyByName = properties.FirstOrDefault(x => x.Name.ToLowerInvariant() == keyValuePair.Key);
                    if (matchingPropertyByName != null)
                    {
                        matchingPropertyByName.SetValue(instance, keyValuePair.Value);
                    }
                }
            }

            return(instance);
        }