Beispiel #1
0
        public static void Copy <T, U>(T fromT, U toU, Func <PropertyAndFieldAccessor, bool> predicate = null, bool ignoreCase = true, bool copyNullMembers = false) where T : class where U : class
        {
            var fromProps = RicochetUtil.GetPropsAndFields <T>();
            var toProps   = RicochetUtil.GetPropsAndFields <U>();

            if (predicate != null)
            {
                fromProps = fromProps.Where(predicate);
                toProps   = toProps.Where(predicate);
            }

            foreach (var toProp in toProps)
            {
                var fromProp = fromProps.FirstOrDefault(x => string.Compare(x.Name, toProp.Name, ignoreCase) == 0);
                if (fromProp == null)
                {
                    continue;
                }

                var fromValue = fromProp.GetVal(fromT);
                if (!copyNullMembers && object.Equals(fromValue, null))
                {
                    continue;
                }

                toProp.SetVal(toU, fromValue);
            }
        }
Beispiel #2
0
        public static IEnumerable <T> ShallowCopyRange <T>(IEnumerable <T> originalItems) where T : class, new()
        {
            var ret   = new List <T>(originalItems.Count());
            var props = RicochetUtil.GetPropsAndFields <T>();

            foreach (var item in originalItems)
            {
                ret.Add(props.ShallowCopyItem(item));
            }
            return(ret);
        }
Beispiel #3
0
            public NestedMemberSelector <U> Where <U>(Func <PropertyAndFieldAccessor, bool> predicate = null)
            {
                predicate = predicate ?? (x => true);
                var props = RicochetUtil.GetPropsAndFields(typeof(T)).Where(x => predicate(x) && x.Type == typeof(U));

                if (!props.Any())
                {
                    throw new ArgumentException($"{typeof(T).Name} does not have a member of type {typeof(U).Name} matching the predicate.");
                }
                if (props.Count() > 1)
                {
                    throw new ArgumentException($"{typeof(T).Name} has more than one member of type {typeof(U).Name} matching the predicate.");
                }
                var value = props.First().GetVal(Result);

                return(new NestedMemberSelector <U> {
                    Result = (U)value,
                });
            }