Пример #1
0
        public T CopyBySerialization <T>(T obj, SerializationBackend serializationBackend)
        {
            var result = default(T);

            if (serializationBackend == SerializationBackend.Binary)
            {
                var memoryStream = new MemoryStream();
                var writer       = new BinarySerializationWriter(memoryStream);
                writer.Write(obj);
                writer.Flush();

                memoryStream.Seek(0, SeekOrigin.Begin);
                var reader = new BinarySerializationReader(memoryStream);
                reader.Serialize(ref result, ArchiveMode.Deserialize);
            }
            //else if (serializationBackend == SerializationBackend.Xml)
            //{
            //    var xmlDoc = new XmlDocument();
            //    var xmlElement = xmlDoc.CreateElement("object");

            //    var writer = new XmlSerializationWriter(xmlElement);
            //    writer.Write(obj);
            //    writer.Flush();

            //    var reader = new XmlSerializationReader(xmlElement);
            //    reader.Serialize(ref result, ArchiveMode.Deserialize);
            //}

            return(result);
        }
Пример #2
0
 public void TestSerializationStructType(SerializationBackend serializationBackend)
 {
     var source = new SerializeTypeTest { A = new S { A = 32 } };
     var copy = CopyBySerialization(source, serializationBackend);
     Assert.That(copy.A, Is.InstanceOf(typeof(S)));
     Assert.That(((S)source.A).A, Is.EqualTo(((S)copy.A).A));
 }
Пример #3
0
 public void TestSerializationComplexTypeClass(SerializationBackend serializationBackend)
 {
     var source = new SerializeClassTest { A = 32, B = 123 };
     var copy = CopyBySerialization(source, serializationBackend);
     Assert.That(copy.A, Is.EqualTo(source.A));
     Assert.That(copy.B, Is.EqualTo(source.B));
 }
Пример #4
0
        public void TestSerializationList(SerializationBackend serializationBackend)
        {
            var source = new List<int> { 32, 12, 15 };
            var copy = CopyBySerialization(source, serializationBackend);

            Assert.That(copy.Count, Is.EqualTo(source.Count));
            for (int i = 0; i < source.Count; ++i)
                Assert.That(copy[i], Is.EqualTo(source[i]));
        }
Пример #5
0
        public void TestSerializationType(SerializationBackend serializationBackend)
        {
            var source = new SerializeTypeTest {
                A = new B()
            };
            var copy = CopyBySerialization(source, serializationBackend);

            Assert.True(copy.A is B);
        }
Пример #6
0
        protected override void Initialize()
        {
            this.backend = this.Property.ValueEntry.SerializationBackend;

            if (!ChildInfos.ContainsKey(backend))
            {
                ChildInfos[backend] = InspectorPropertyInfoUtility.GetDefaultPropertiesForType(this.Property, typeof(EditableKeyValuePair <TKey, TValue>), false);
            }
        }
Пример #7
0
        public void TestSerializationType(SerializationBackend serializationBackend)
        {
            var source = new SerializeTypeTest {
                A = new B()
            };
            var copy = CopyBySerialization(source, serializationBackend);

            Assert.That(copy.A, Is.InstanceOf(typeof(B)));
        }
Пример #8
0
        public void TestSerializationStructType(SerializationBackend serializationBackend)
        {
            var source = new SerializeTypeTest {
                A = new S {
                    A = 32
                }
            };
            var copy = CopyBySerialization(source, serializationBackend);

            Assert.True(copy.A is S);
            Assert.Equal(((S)source.A).A, ((S)copy.A).A);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="InspectorValuePropertyInfo{TOwner, TValue}"/> class.
        /// </summary>
        /// <param name="fieldInfo">The field to represent.</param>
        /// <param name="serializationBackend">The serialization backend.</param>
        /// <param name="allowEditable">Whether the property can be editable.</param>
        public InspectorValuePropertyInfo(FieldInfo fieldInfo, SerializationBackend serializationBackend, bool allowEditable)
            : base(fieldInfo, fieldInfo.FieldType.IsValueType ? PropertyType.ValueType : PropertyType.ReferenceType, serializationBackend, allowEditable)
        {
            MemberAliasFieldInfo aliasFieldInfo = fieldInfo as MemberAliasFieldInfo;

            if (aliasFieldInfo != null)
            {
                fieldInfo = aliasFieldInfo.AliasedField;
            }

            this.getter = EmitUtilities.CreateInstanceFieldGetter <TOwner, TValue>(fieldInfo);
            this.setter = EmitUtilities.CreateInstanceFieldSetter <TOwner, TValue>(fieldInfo);
        }
Пример #10
0
        public void TestSerializationBaseTypes(SerializationBackend serializationBackend)
        {
            var source = new SerializeBaseTypeTest
            {
                Nullable1 = 546,
                Bool      = true,
                B         = 12,
                SB        = 14,
                C         = 'a',
                F         = 12.0f,
                D         = 13.0,
                Enum      = SerializeEnum.Enum2,
                Enum2     = SerializeEnum.Enum3,
                I16       = -8,
                I32       = -12,
                I64       = -16,
                U16       = 8,
                U32       = 12,
                U64       = 16,
                //Decimal = 12300,
                String = "Tat\"asd\\a",
                Data   = new byte[] { 12, 31, 11 },
                Guid   = Guid.NewGuid(),
            };
            var copy = CopyBySerialization(source, serializationBackend);

            Assert.Equal(source.Nullable1, copy.Nullable1);
            Assert.Equal(source.Nullable2, copy.Nullable2);
            Assert.Equal(source.Bool, copy.Bool);
            Assert.Equal(source.C, copy.C);
            Assert.Equal(source.B, copy.B);
            Assert.Equal(source.SB, copy.SB);
            Assert.Equal(source.F, copy.F);
            Assert.Equal(source.D, copy.D);
            Assert.Equal(source.U16, copy.U16);
            Assert.Equal(source.I16, copy.I16);
            Assert.Equal(source.U32, copy.U32);
            Assert.Equal(source.I32, copy.I32);
            Assert.Equal(source.U64, copy.U64);
            Assert.Equal(source.I64, copy.I64);
            Assert.Equal(source.Enum, copy.Enum);
            Assert.Equal(source.Enum2, copy.Enum2);
            Assert.Equal(source.EnumNull, copy.EnumNull);
            Assert.Equal(source.String, copy.String);
            Assert.Equal(source.StringNull, copy.StringNull);
            Assert.Equal(source.Data, copy.Data);
            Assert.Equal(source.DataNull, copy.DataNull);
            Assert.Equal(source.Guid, copy.Guid);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="InspectorValuePropertyInfo{TOwner, TValue}"/> class.
        /// </summary>
        /// <param name="propertyInfo">The property to represent.</param>
        /// <param name="serializationBackend">The serialization backend.</param>
        /// <param name="allowEditable">Whether the property can be editable.</param>
        public InspectorValuePropertyInfo(PropertyInfo propertyInfo, SerializationBackend serializationBackend, bool allowEditable)
            : base(propertyInfo, propertyInfo.PropertyType.IsValueType ? PropertyType.ValueType : PropertyType.ReferenceType, serializationBackend, allowEditable)

        {
            MemberAliasPropertyInfo aliasPropertyInfo = propertyInfo as MemberAliasPropertyInfo;

            if (aliasPropertyInfo != null)
            {
                propertyInfo = aliasPropertyInfo.AliasedProperty;
            }

            this.getter = EmitUtilities.CreateInstancePropertyGetter <TOwner, TValue>(propertyInfo);

            if (propertyInfo.CanWrite)
            {
                this.setter = EmitUtilities.CreateInstancePropertySetter <TOwner, TValue>(propertyInfo);
            }
        }
Пример #12
0
        public static InspectorPropertyInfo CreateValue(string name, int order, SerializationBackend serializationBackend, IValueGetterSetter getterSetter, IEnumerable <Attribute> attributes)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (getterSetter == null)
            {
                throw new ArgumentNullException("getterSetter");
            }

            if (name.Contains('.'))
            {
                throw new ArgumentException("Property names may not contain '.'; was given the name '" + name + "'.");
            }

            var result = new InspectorPropertyInfo();

            result.memberInfos = new MemberInfo[0];

            result.typeOfOwner = getterSetter.OwnerType;
            result.typeOfValue = getterSetter.ValueType;

            if (attributes == null)
            {
                result.attributes = new List <Attribute>();
            }
            else
            {
                result.attributes = attributes.Where(attr => attr != null).ToList();
            }

            result.PropertyName         = name;
            result.PropertyType         = PropertyType.Value;
            result.SerializationBackend = serializationBackend;
            result.IsEditable           = !getterSetter.IsReadonly;

            result.getterSetter = getterSetter;

            return(result);
        }
Пример #13
0
        public void TestSerializationCollectionTypes(SerializationBackend serializationBackend)
        {
            var source = new SerializeCollectionTest
            {
                List = new List <int> {
                    3112, 123
                },
                ListInterface = new List <int> {
                    5112, 623
                },
                ListClass = new List <SerializeClassTest> {
                    new SerializeClassTest {
                        A = 1, B = 2
                    }, new SerializeClassTest {
                        A = 3, B = 4
                    }
                },
                ReadOnlyList = { 345, 567 },
                Dictionary   = new Dictionary <string, string> {
                    { "a", "b" }, { "c", "d" }
                },
            };
            var copy = CopyBySerialization(source, serializationBackend);

            Assert.Equal(source.List, copy.List);
            Assert.Null(copy.ListNull);
            Assert.Equal(source.ListInterface, copy.ListInterface);
            Assert.Null(copy.ListInterfaceNull);
            Assert.Equal(source.ListClass.Count, copy.ListClass.Count);
            Assert.Equal(source.ReadOnlyList.Count, copy.ReadOnlyList.Count);
            for (int i = 0; i < source.ListClass.Count; ++i)
            {
                Assert.Equal(source.ListClass[i].A, copy.ListClass[i].A);
                Assert.Equal(source.ListClass[i].B, copy.ListClass[i].B);
            }

            Assert.Equal(source.Dictionary, copy.Dictionary);
            Assert.Null(copy.DictionaryNull);
        }
Пример #14
0
        public static void AddMember(this IList <InspectorPropertyInfo> infos, string name, bool allowEditable = true, SerializationBackend backend = SerializationBackend.None, params Attribute[] attributes)
        {
            var members = ProcessingOwnerType.GetMember(name, MemberTypes.Field | MemberTypes.Method | MemberTypes.Property, Flags.AllMembers);

            if (members.Length == 0 || members.Length > 1)
            {
                throw new ArgumentException("Could not find precisely 1 member on type '" + ProcessingOwnerType.GetNiceName() + "' with name '" + name + "'; found " + members.Length + " members.");
            }

            AddMember(infos, members[0], true, SerializationBackend.None, attributes ?? new Attribute[0]);
        }
Пример #15
0
 public static InspectorPropertyInfo CreateForMember(MemberInfo member, bool allowEditable, SerializationBackend serializationBackend, IEnumerable <Attribute> attributes)
 {
     return(CreateForMember(member, allowEditable, serializationBackend, attributes.ToList()));
 }
Пример #16
0
        public static void AddProcessedMember(this IList <InspectorPropertyInfo> infos, InspectorProperty parentProperty, MemberInfo member, bool allowEditable = true, SerializationBackend backend = SerializationBackend.None, params Attribute[] attributes)
        {
            var list = new List <Attribute>();

            if (attributes != null)
            {
                list.AddRange(attributes);
            }
            InspectorPropertyInfoUtility.ProcessAttributes(parentProperty, member, list);
            infos.Add(InspectorPropertyInfo.CreateForMember(member, allowEditable, backend, list));
        }
Пример #17
0
 public static void AddMember(this IList <InspectorPropertyInfo> infos, MemberInfo member, bool allowEditable = true, SerializationBackend backend = SerializationBackend.None, params Attribute[] attributes)
 {
     infos.Add(InspectorPropertyInfo.CreateForMember(member, allowEditable, backend, attributes ?? new Attribute[0]));
 }
Пример #18
0
 public static InspectorPropertyInfo CreateValue(string name, int order, SerializationBackend serializationBackend, IValueGetterSetter getterSetter, params Attribute[] attributes)
 {
     return(CreateValue(name, order, serializationBackend, getterSetter, (IEnumerable <Attribute>)attributes));
 }
Пример #19
0
        public static void AddProcessedMember(this IList <InspectorPropertyInfo> infos, InspectorProperty parentProperty, string name, bool allowEditable = true, SerializationBackend backend = SerializationBackend.None, params Attribute[] attributes)
        {
            var type = ProcessingOwnerType;

            type = (parentProperty.ValueEntry != null) ? parentProperty.ValueEntry.TypeOfValue : ProcessingOwnerType;

            var members = type.GetMember(name, MemberTypes.Field | MemberTypes.Method | MemberTypes.Property, Flags.AllMembers);

            if (members.Length == 0 || members.Length > 1)
            {
                throw new ArgumentException("Could not find precisely 1 member on type '" + type.GetNiceName() + "' with name '" + name + "'; found " + members.Length + " members.");
            }

            var list = new List <Attribute>();

            if (attributes != null)
            {
                list.AddRange(attributes);
            }
            InspectorPropertyInfoUtility.ProcessAttributes(parentProperty, members[0], list);
            infos.Add(InspectorPropertyInfo.CreateForMember(members[0], allowEditable, backend, list));
        }
Пример #20
0
        public static InspectorPropertyInfo CreateForMember(MemberInfo member, bool allowEditable, SerializationBackend serializationBackend, params Attribute[] attributes)
        {
            var list = new List <Attribute>(attributes.Length);

            for (int i = 0; i < attributes.Length; i++)
            {
                list.Add(attributes[i]);
            }

            return(CreateForMember(member, allowEditable, serializationBackend, list));
        }
Пример #21
0
 public static void AddValue <TValue>(this IList <InspectorPropertyInfo> infos, string name, Func <TValue> getter, Action <TValue> setter, int order = 0, SerializationBackend backend = SerializationBackend.None)
 {
     AddValue <TValue>(infos, name, getter, setter, 0, SerializationBackend.None, null);
 }
Пример #22
0
        public static InspectorPropertyInfo CreateForMember(MemberInfo member, bool allowEditable, SerializationBackend serializationBackend, List <Attribute> attributes)
        {
            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            if (!(member is FieldInfo || member is PropertyInfo || member is MethodInfo))
            {
                throw new ArgumentException("Can only create inspector properties for field, property and method members.");
            }

            if (member is MethodInfo && serializationBackend != SerializationBackend.None)
            {
                throw new ArgumentException("Serialization backend can only be None for method members.");
            }

            if (member is MethodInfo && allowEditable)
            {
                //throw new ArgumentException("allowEditable can only be false for method members.");
                allowEditable = false;
            }

            if (allowEditable && member is FieldInfo && (member as FieldInfo).IsLiteral)
            {
                allowEditable = false;
            }

            string name = null;

            if (member is MethodInfo)
            {
                var mi         = member as MethodInfo;
                var parameters = mi.GetParameters();
                if (parameters.Length > 0)
                {
                    name = mi.GetNiceName();
                }
            }

            if (name == null)
            {
                name = member.Name;
            }

            if (name.Contains("."))
            {
                var index = name.LastIndexOf(".") + 1;

                if (index < name.Length)
                {
                    name = name.Substring(index);
                }
                else
                {
                    throw new ArgumentException("A member name somehow had a '.' as the last character. This shouldn't be possible, but the '" + member.Name + "' has messed things up for everyone now. Good job!");
                }
            }

            var result = new InspectorPropertyInfo();

            if (member.IsDefined(typeof(OmitFromPrefabModificationPathsAttribute), true))
            {
                name = "#" + name;
            }

            result.memberInfos          = new MemberInfo[] { member };
            result.PropertyName         = name;
            result.PropertyType         = member is MethodInfo ? PropertyType.Method : PropertyType.Value;
            result.SerializationBackend = serializationBackend;

            if (attributes == null)
            {
                result.attributes = new List <Attribute>();
            }
            else
            {
                result.attributes = attributes;

                for (int i = attributes.Count - 1; i >= 0; i--)
                {
                    var attr = attributes[i];

                    if (attr == null)
                    {
                        attributes.RemoveAt(i);
                        continue;
                    }

                    var orderAttr = attr as PropertyOrderAttribute;
                    if (orderAttr != null)
                    {
                        result.Order = orderAttr.Order;
                    }
                }
            }

            result.typeOfOwner = member.DeclaringType;

            if (member is FieldInfo || member is PropertyInfo)
            {
                var valueType = member.GetReturnType();

                result.typeOfValue = valueType;

                result.getterSetter = GetEmittedGetterSetterCreator(member.DeclaringType, valueType)(member, !allowEditable);

                //if (member is FieldInfo)
                //{
                //    //TwoArgsObjectArray[0] = member;
                //    //TwoArgsObjectArray[1] = !allowEditable;

                //    //var con = getterSetterType.GetConstructor(GetterSetterFieldConstructorSignature);
                //    //result.getterSetter = (IValueGetterSetter)con.Invoke(TwoArgsObjectArray);

                //    //result.getterSetter = (IValueGetterSetter)Activator.CreateInstance(typeof(GetterSetter<,>).MakeGenericType(member.DeclaringType, valueType), member, !allowEditable);

                //    result.getterSetter = GetEmittedFieldGetterSetterCreator(member.DeclaringType, valueType)(member as FieldInfo, !allowEditable);
                //}
                //else
                //{
                //    //OneArgObjectArray[0] = member;

                //    //var con = getterSetterType.GetConstructor(GetterSetterPropertyConstructorSignature);
                //    //result.getterSetter = (IValueGetterSetter)con.Invoke(OneArgObjectArray);

                //    //result.getterSetter = (IValueGetterSetter)Activator.CreateInstance(typeof(GetterSetter<,>).MakeGenericType(member.DeclaringType, valueType), member);

                //}

                result.IsEditable = allowEditable && !attributes.HasAttribute <ReadOnlyAttribute>() && !result.getterSetter.IsReadonly;
            }

            return(result);
        }
Пример #23
0
 public static void AddValue <TValue>(this IList <InspectorPropertyInfo> infos, string name, Func <TValue> getter, Action <TValue> setter, int order = 0, SerializationBackend backend = SerializationBackend.None, params Attribute[] attributes)
 {
     infos.Add(InspectorPropertyInfo.CreateValue(name, order, backend, new GetterSetter <object, TValue>(getter, setter), attributes));
 }