Пример #1
0
        /// <summary>
        /// for Polymorphism creation of classes and structures. UnStable
        /// </summary>
        /// <param name="anonymousObject"></param>
        /// <param name="anonymouseObjectToBind"></param>
        /// <param name="BindFieldType"></param>
        /// <param name="IgnoreCase"></param>
        /// <returns></returns>
        /// TODO: fix the Polymorphism of Classes->Classes->Classes etc
        internal static object CopyFields(this object anonymousObject, object anonymouseObjectToBind, Type BindFieldType, bool IgnoreCase = true)
        {
            if (anonymousObject == null)
            {
                throw new ArgumentNullException();
            }
            if (BindFieldType == null)
            {
                return(anonymousObject);
            }

            if (anonymouseObjectToBind == null)
            {
                anonymouseObjectToBind = GenericType.Create(BindFieldType);
            }

            Type tsObjectType = anonymousObject.GetType();
            Type tbObjectType = anonymouseObjectToBind.GetType();

            FieldInfo[] fiSrcObject = tsObjectType.GetFields();
            FieldInfo[] fiDstObject = tbObjectType.GetFields();

            //if (fiSrcObject == null) throw new NullReferenceException();
            //if (fiDstObject == null) throw new NullReferenceException();
            //if (fiSrcObject.Length == 0) throw new MissingMemberException(String.Format("Object {0} is doesnt not have any fields.", tsObjectType.ToString()));
            //if (fiDstObject.Length == 0) throw new MissingMemberException(String.Format("Object {0} is doesnt not have any fields.", tbObjectType.ToString()));

            fiDstObject.TryCatch(
                pd =>
            {
                pd.ForEach(
                    d =>
                {
                    var s = fiSrcObject.Where(
                        p =>
                        (IgnoreCase == true ? p.Name.Equals(d.Name, StringComparison.OrdinalIgnoreCase) : p.Name.Equals(d.Name)) == true &&
                        p.IsPrivate == false &&
                        p.MemberType == MemberTypes.Field
                        ).FirstOrDefault();

                    if (s != null)
                    {
                        //So it does not break out of the for each loop
                        d.TryCatch(
                            p => p.SetValue(anonymouseObjectToBind, Convert.ChangeType(s.GetValue(anonymousObject), d.FieldType)),
                            p => p.SetValue(anonymouseObjectToBind, CopyFields(s.GetValue(anonymousObject), d.GetValue(anonymouseObjectToBind), d.FieldType))
                            );
                    }
                });
            });

            return(anonymouseObjectToBind);
        }
Пример #2
0
        internal static object CopyProperties(this object anonymousObject, object anonymouseObjectToBind, Type BindObjectCastType, bool IgnoreCase = true)
        {
            if (anonymousObject == null)
            {
                throw new ArgumentNullException("TSource object is null");
            }
            if (BindObjectCastType == null)
            {
                return(anonymousObject);
            }

            if (anonymouseObjectToBind == null)
            {
                anonymouseObjectToBind = GenericType.Create(BindObjectCastType);
            }

            Type tsObjectType = anonymousObject.GetType();
            Type tbObjectType = anonymouseObjectToBind.GetType();

            PropertyInfo[] pfSrcObject = tsObjectType.GetProperties();
            PropertyInfo[] pfDstObject = tbObjectType.GetProperties();

            //if (pfSrcObject == null) throw new NullReferenceException();
            //if (pfDstObject == null) throw new NullReferenceException();
            //if (pfSrcObject.Length == 0) throw new MissingMemberException(String.Format("Object {0} is doesnt not have any properties.", tsObjectType.ToString()));
            //if (pfDstObject.Length == 0) throw new MissingMemberException(String.Format("Object {0} is doesnt not have any properties.", tbObjectType.ToString()));

            pfDstObject.TryCatch(
                pd =>
            {
                pd.ForEach(
                    d =>
                {
                    var s = pfSrcObject.Where(
                        p =>
                        (IgnoreCase == true ? p.Name.Equals(d.Name, StringComparison.OrdinalIgnoreCase) : p.Name.Equals(d.Name)) == true &&
                        p.CanWrite == true &&
                        p.MemberType == MemberTypes.Property
                        ).FirstOrDefault();

                    if (s != null)
                    {
                        d.TryCatch(
                            p => p.SetValue(anonymouseObjectToBind, Convert.ChangeType(s.GetValue(anonymousObject, null), p.PropertyType)),
                            p => p.SetValue(anonymouseObjectToBind, CopyProperties(s.GetValue(anonymousObject, null), p.GetValue(anonymouseObjectToBind, null), p.PropertyType))
                            );
                    }
                });
            });

            return(anonymouseObjectToBind);
        }