Пример #1
0
 /// <summary>
 /// Overrides reflection result with <see cref="TypeOverride"/> for the <typeparamref name="T"/> type.
 /// </summary>
 /// <typeparam name="T">The type to be overridden.</typeparam>
 /// <param name="overrideInfo">The override info of the type.</param>
 /// <param name="purgeExisting">If this value is true, the reflection engine will reflect the type again and apply the <paramref name="overrideInfo"/>, otherwise, <paramref name="overrideInfo"/> is merged into the existing reflection cache.</param>
 /// <seealso cref="Override(Type,TypeOverride,bool)"/>
 public void Override <T>(TypeOverride overrideInfo, bool purgeExisting)
 {
     Override(typeof(T), overrideInfo, purgeExisting);
 }
Пример #2
0
        /// <summary>
        /// Overrides reflection result with <see cref="TypeOverride"/> for the specific type and optionally purge existing overrides.
        /// </summary>
        /// <param name="type">The type to be overridden.</param>
        /// <param name="overrideInfo">The override info of the type.</param>
        /// <param name="purgeExisting">If this value is true, the reflection engine will reflect the type again and apply the <paramref name="overrideInfo"/>, otherwise, <paramref name="overrideInfo"/> is merged into the existing reflection cache.</param>
        /// <remarks>
        /// <para>At this moment, the override only affects the registered type.</para>
        /// <para>If a class has its subclasses, the override will not be applied to its subclasses.</para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The parameter <paramref name="type"/> or <paramref name="overrideInfo"/> is null.</exception>
        /// <exception cref="MissingMemberException">No member is found for a <see cref="MemberOverride"/> in <paramref name="overrideInfo"/>.</exception>
        public void Override(Type type, TypeOverride overrideInfo, bool purgeExisting)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (overrideInfo == null)
            {
                throw new ArgumentNullException("overrideInfo");
            }
            var c = GetReflectionCache(type);

            if (purgeExisting)
            {
                ControlTypeSerializationSettings(type, c);
            }
            if (overrideInfo.OverrideInterceptor)
            {
                c.Interceptor = overrideInfo.Interceptor;
            }
            if (overrideInfo.OverrideConverter)
            {
                c.Converter = overrideInfo.Converter;
            }
            if (overrideInfo.OverrideContainerName)
            {
                c.CollectionName    = overrideInfo.CollectionContainer;
                c.DeserializeMethod = overrideInfo.CollectionContainer == null
                                        ? JsonDeserializer.GetReadJsonMethod(type)
                                        : new CompoundDeserializer(c.CollectionName, c.DeserializeMethod).Deserialize;
            }
            if (overrideInfo.Deserializable.HasValue)
            {
                c.AlwaysDeserializable = overrideInfo.Deserializable == true;
            }
            if (overrideInfo._MemberOverrides == null || overrideInfo._MemberOverrides.Count == 0)
            {
                return;
            }
            var s = c.Setters;

            // add properties ignored by _controller in GetProperties method
            foreach (var ov in overrideInfo._MemberOverrides)
            {
                if (ov.Deserializable != true)
                {
                    continue;
                }
                var p = c.FindProperties(ov.MemberName);
                if (p.Count == 0)
                {
                    var m = c.FindMemberCache(ov.MemberName);
                    if (m == null)
                    {
                        throw new MissingMemberException(c.TypeName, ov.MemberName);
                    }
                    var pi = new JsonMemberSetter(m);
                    // TODO: load serialization control settings
                    var ds = LoadMemberDeserializationSettings(pi, _controller);
                    if (ds != null)
                    {
                        foreach (var item in ds)
                        {
                            item.Value.Member.MemberTypeReflection = GetReflectionCache(item.Value.Member.MemberType);
                            AddPropertyInfo(c.Setters, item.Key, item.Value);
                        }
                    }
                }
            }
            foreach (var ov in overrideInfo._MemberOverrides)
            {
                var g = c.FindGetters(ov.MemberName);
                if (g == null)
                {
                    throw new MissingMemberException(type.FullName, ov.MemberName);
                }
                OverrideGetters(g, ov);
                OverridePropInfo(type, s, ov, g);
            }
        }
Пример #3
0
 /// <summary>
 /// Overrides reflection result with <see cref="TypeOverride"/> for the <typeparamref name="T"/> type. If the type is already overridden, either automatically or manually, the <paramref name="overrideInfo"/> will merged into the existing reflected info.
 /// </summary>
 /// <typeparam name="T">The type to be overridden.</typeparam>
 /// <param name="overrideInfo">The override info of the type.</param>
 /// <seealso cref="Override(Type,TypeOverride,bool)"/>
 public void Override <T>(TypeOverride overrideInfo)
 {
     Override(typeof(T), overrideInfo, false);
 }