Beispiel #1
0
        private static BinaryBuilder Deconstruction(object obj)
        {
            if (obj == null)
            {
                return(nullObj);
            }

            if (obj.GetType().IsPrimitive || obj is string)
            {
                return(BinaryBuilder.PrimitiveToByte(obj));
            }

            if (!obj.GetType().IsSerializable) //PROTECTION
            {
                return(new byte[0]);           //don't touch the field, CONSIDER: throwing an error
            }
            else if (obj is ISerializable)
            {
                return(SeriObjDeconstructor((ISerializable)obj));
            }

            if (obj is IList) //string is also an enum but will never reach here thanks to the primitive check
            {
                return(ListObjDeconstructor((IList)obj));
            }

            var           fields   = obj.GetType().GetFields(bindingFlags);
            BinaryBuilder objGraph = new BinaryBuilder();

            #region SpecialCases

            /*Special cases so far:
             * 1. Object is null (Done)
             * 2. Object points to itself in an infnite loop (Done)
             * 3. backingField (Done)
             * 4. Object is an enum/list/array (Done)
             */
            #endregion
            foreach (var field in fields)
            {
                //the field is a field class, the fieldValue is the value of this field (the actual object)
                //for examle field is "int num = 5" and the field value is the 5
                if (field.IsNotSerialized) //PROTECTION
                {
                    continue;              //Don't touch the object, was meant to not be serialized
                }
                object fieldValue = field.GetValue(obj);

                if (fieldValue == null) //No sending nulls
                {
                    continue;           //Null object, ignore. spares the text in the writing
                }
                if (fieldValue == obj)
                {
                    throw new StackOverflowException("An object points to itself"); //Will cause an infinite loop, so just throw it
                }
                //BACKING FIELDS ARE IGNORED BECAUSE THE PROPERTIES LINE SAVES THEM INSTEAD
                //one of the few compiler generated attributes is backing fields
                //backing field is a proprty with a get and set only, which has a hidden field behind it
                //instead we save this field in the properties
                if (Attribute.GetCustomAttribute(field, typeof(CompilerGeneratedAttribute)) == null) //this line checks for backing field
                {
                    objGraph.Append(startClass + field.Name + equals + Deconstruction(fieldValue) + endClass);
                }
                else
                {
                    objGraph.Append(startClass + GetNameOfBackingField(field.Name) + equals + Deconstruction(fieldValue) + endClass);
                }
            }
            return(objGraph);
        }