Exemplo n.º 1
0
 public void TestAreEqual()
 {
     Assert.IsFalse((object)0 == (object)0); // !!
     Assert.IsTrue(TypeUtils.AreEqual((object)0, (object)0));
     Assert.IsTrue(TypeUtils.AreEqual(null, null));
     Assert.IsFalse(TypeUtils.AreEqual(null, (object)0));
     Assert.IsFalse(TypeUtils.AreEqual((object)0, null));
     Assert.IsFalse(TypeUtils.AreEqual((object)0, (object)0.0)); // !!
 }
Exemplo n.º 2
0
        private void RegisterMember(Type type, System.Reflection.MemberInfo m, Type mType, Func <object, object> get, Action <object, object> set)
        {
            // struct that holds access method for property/field
            MemberInfo accessor = new MemberInfo();

            accessor.Type = mType;
            accessor.Get  = get;
            accessor.Set  = set;

            if (set != null) // writeable ?
            {
                accessor.SerializeMethod = YamlSerializeMethod.Assign;
            }
            else
            {
                accessor.SerializeMethod = YamlSerializeMethod.Never;
                if (mType.IsClass)
                {
                    accessor.SerializeMethod = YamlSerializeMethod.Content;
                }
            }
            var attr1 = m.GetAttribute <YamlSerializeAttribute>();

            if (attr1 != null)     // specified
            {
                if (set == null)   // read only member
                {
                    if (attr1.SerializeMethod == YamlSerializeMethod.Assign ||
                        (mType.IsValueType && accessor.SerializeMethod == YamlSerializeMethod.Content))
                    {
                        throw new ArgumentException("{0} {1} is not writeable by {2}."
                                                    .DoFormat(mType.FullName, m.Name, attr1.SerializeMethod.ToString()));
                    }
                }
                accessor.SerializeMethod = attr1.SerializeMethod;
            }
            if (accessor.SerializeMethod == YamlSerializeMethod.Never)
            {
                return; // no need to register
            }
            if (accessor.SerializeMethod == YamlSerializeMethod.Binary)
            {
                if (!mType.IsArray)
                {
                    throw new InvalidOperationException("{0} {1} of {2} is not an array. Can not be serialized as binary."
                                                        .DoFormat(mType.FullName, m.Name, type.FullName));
                }
                if (!TypeUtils.IsPureValueType(mType.GetElementType()))
                {
                    throw new InvalidOperationException(
                              "{0} is not a pure ValueType. {1} {2} of {3} can not serialize as binary."
                              .DoFormat(mType.GetElementType(), mType.FullName, m.Name, type.FullName));
                }
            }

            // ShouldSerialize
            //      YamlSerializeAttribute(Never) => false
            //      ShouldSerializeSomeProperty => call it
            //      DefaultValueAttribute(default) => compare to it
            //      otherwise => true
            var shouldSerialize = type.GetMethod("ShouldSerialize" + m.Name,
                                                 BindingFlags.Instance | BindingFlags.NonPublic,
                                                 null, Type.EmptyTypes, new ParameterModifier[0]);

            if (shouldSerialize != null && shouldSerialize.ReturnType == typeof(bool) && accessor.ShouldSeriealize == null)
            {
                accessor.ShouldSeriealize = obj => (bool)shouldSerialize.Invoke(obj, EmptyObjectArray);
            }
            var attr2 = m.GetAttribute <DefaultValueAttribute>();

            if (attr2 != null && accessor.ShouldSeriealize == null)
            {
                var defaultValue = attr2.Value;
                if (TypeUtils.IsNumeric(defaultValue) && defaultValue.GetType() != mType)
                {
                    defaultValue = TypeUtils.CastToNumericType(defaultValue, mType);
                }
                accessor.ShouldSeriealize = obj => !TypeUtils.AreEqual(defaultValue, accessor.Get(obj));
            }
            if (accessor.ShouldSeriealize == null)
            {
                accessor.ShouldSeriealize = obj => true;
            }

            Accessors.Add(m.Name, accessor);
        }