Ejemplo n.º 1
0
        internal static MemberCache[] GetMembers(Type type)
        {
            var pl = type.GetProperties(__ReflectionFlags);
            var fl = type.GetFields(__ReflectionFlags);
            var c  = new List <MemberCache> (pl.Length + fl.Length);

            foreach (var m in pl)
            {
                if (m.GetIndexParameters().Length > 0 ||               // Property is an indexer
                    m.PropertyType.IsUnsafe())
                {
                    continue;
                }
                c.Add(new MemberCache(m));
            }
            foreach (var m in fl)
            {
                if (m.IsLiteral ||              // Skip const field
                    m.FieldType.IsUnsafe())
                {
                    continue;
                }
                c.Add(new MemberCache(m));
            }
            c = c.FindAll(m => {
                return(AttributeHelper.HasAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute> (m.MemberInfo, true) == false);
            });
            var r = new MemberCache[c.Count];

            c.CopyTo(r, 0);
            return(r);
        }
Ejemplo n.º 2
0
        public override bool IsMemberDeserializable(MemberInfo member, IMemberInfo info)
        {
            var ro = AttributeHelper.GetAttribute <System.ComponentModel.ReadOnlyAttribute> (member, true);

            if (ro != null)
            {
                return(ro.IsReadOnly == false);
            }
            var s = AttributeHelper.GetAttribute <JsonSerializableAttribute> (member, true);

            if (s != null)
            {
                return(true);
            }
#if NET_40_OR_GREATER
            if (UseDataContractAttributes && _ContractualTypes.Contains(member.DeclaringType))
            {
                return(AttributeHelper.HasAttribute <DataMemberAttribute> (member, true));
            }
            if (UseDataContractAttributes && AttributeHelper.HasAttribute <IgnoreDataMemberAttribute> (member, true))
            {
                return(false);
            }
#endif
            if (UseXmlSerializationAttributes && AttributeHelper.HasAttribute <XmlIgnoreAttribute> (member, false))
            {
                return(false);
            }
            return(base.IsMemberDeserializable(member, info));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns the <see cref="IJsonInterceptor"/> for given type. If no interceptor, null should be returned. The interceptor can be set via <see cref="JsonInterceptorAttribute"/>.
        /// </summary>
        /// <param name="type">The type to be checked.</param>
        /// <returns>The interceptor.</returns>
        public override IJsonInterceptor GetInterceptor(Type type)
        {
#if NET_40_OR_GREATER
            if (UseDataContractAttributes && AttributeHelper.HasAttribute <DataContractAttribute> (type, false))
            {
                _ContractualTypes.Add(type);
            }
#endif
            var ia = AttributeHelper.GetAttribute <JsonInterceptorAttribute> (type, true);
            if (ia != null)
            {
                return(ia.Interceptor);
            }
            return(null);
        }
Ejemplo n.º 4
0
        public override bool?IsMemberSerializable(MemberInfo member, IMemberInfo info)
        {
            var ic = AttributeHelper.GetAttribute <JsonIncludeAttribute> (member, true);

            if (ic != null)
            {
                return(ic.Include ? true : false);
            }
            if (AttributeHelper.HasAttribute <JsonSerializableAttribute> (member, true))
            {
                return(true);
            }
#if NET_40_OR_GREATER
            if (UseDataContractAttributes && _ContractualTypes.Contains(member.DeclaringType))
            {
                return(AttributeHelper.HasAttribute <DataMemberAttribute> (member, true));
            }
            if (UseDataContractAttributes && AttributeHelper.HasAttribute <IgnoreDataMemberAttribute> (member, true))
            {
                return(false);
            }
#endif
            if (UseXmlSerializationAttributes && AttributeHelper.HasAttribute <XmlIgnoreAttribute> (member, false))
            {
                return(false);
            }
            if (IgnoreAttributes != null && IgnoreAttributes.Count > 0)
            {
                foreach (var item in IgnoreAttributes)
                {
                    if (member.IsDefined(item, false))
                    {
                        return(false);
                    }
                }
            }
            return(base.IsMemberSerializable(member, info));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets whether the type is always deserializable. The value can be set via <see cref="JsonSerializableAttribute"/>.
 /// </summary>
 /// <param name="type">The type to be deserialized.</param>
 /// <returns>Whether the type can be deserialized even if it is a non-public type.</returns>
 public override bool IsAlwaysDeserializable(Type type)
 {
     return(AttributeHelper.HasAttribute <JsonSerializableAttribute> (type, false));
 }
Ejemplo n.º 6
0
        internal ReflectionCache(Type type)
        {
            Type                 = type;
            TypeName             = type.FullName;
            AssemblyName         = type.AssemblyQualifiedName;
            CircularReferencable = type.IsValueType == false || type != typeof(string);
            IsAbstract           = type.IsUndetermined();
            IsAnonymous          = type.IsAnonymous();

            JsonDataType      = Reflection.GetJsonDataType(type);
            SerializeMethod   = JsonSerializer.GetWriteJsonMethod(type);
            DeserializeMethod = JsonDeserializer.GetReadJsonMethod(type);

            if (JsonDataType == JsonDataType.Enum)
            {
                IsFlaggedEnum = AttributeHelper.HasAttribute <FlagsAttribute> (type, false);
                return;
            }

            if (type.IsPointer)
            {
                throw new JsonSerializationException("Can not serialize pointer type: " + type.AssemblyQualifiedName);
            }
            if (type.IsArray)
            {
                ArgumentTypes = new Type[] { type.GetElementType() };
                CommonType    = type.GetArrayRank() == 1 ? ComplexType.Array : ComplexType.MultiDimensionalArray;
            }
            else
            {
                var t = type;
                if (t.IsGenericType == false)
                {
                    while ((t = t.BaseType) != null)
                    {
                        if (t.IsGenericType)
                        {
                            break;
                        }
                    }
                }
                if (t != null)
                {
                    ArgumentTypes = t.GetGenericArguments();
                    var gt = t.GetGenericTypeDefinition();
                    if (gt.Equals(typeof(Dictionary <,>)))
                    {
                        CommonType = ComplexType.Dictionary;
                    }
                    else if (gt.Equals(typeof(List <>)))
                    {
                        CommonType = ComplexType.List;
                    }
                    else if (gt.Equals(typeof(Nullable <>)))
                    {
                        CommonType      = ComplexType.Nullable;
                        SerializeMethod = JsonSerializer.GetWriteJsonMethod(ArgumentTypes[0]);
                    }
                }
            }
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                if (typeof(Array).IsAssignableFrom(type) == false)
                {
                    AppendItem = Reflection.CreateWrapperMethod <AddCollectionItem> (Reflection.FindMethod(type, "Add", new Type[] { null }));
                }
                if (ArgumentTypes != null && ArgumentTypes.Length == 1)
                {
                    ItemSerializer   = JsonSerializer.GetWriteJsonMethod(ArgumentTypes[0]);
                    ItemDeserializer = JsonDeserializer.GetReadJsonMethod(ArgumentTypes[0]);
                }
            }
            if (ArgumentTypes != null)
            {
                ArgumentReflections = new ReflectionCache[ArgumentTypes.Length];
            }
            if (CommonType != ComplexType.Array &&
                CommonType != ComplexType.MultiDimensionalArray &&
                CommonType != ComplexType.Nullable)
            {
                if (type.IsPubliclyAccessible() == false)
                {
                    ConstructorInfo |= ConstructorTypes.NonPublic;
                }
                if (type.IsClass || type.IsValueType)
                {
                    try {
                        Constructor = Reflection.CreateConstructorMethod(type, type.IsVisible == false || typeof(DatasetSchema).Equals(type));
                    }
                    catch (Exception ex) {
                        throw new JsonSerializationException("Error occurred when creating constructor method for type " + type.AssemblyQualifiedName, ex);
                    }
                    if (Constructor != null && Constructor.Method.IsPublic == false)
                    {
                        ConstructorInfo |= ConstructorTypes.NonPublic;
                    }
                    if (Constructor == null)
                    {
                        var c = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        if (c != null && c.Length > 0)
                        {
                            ConstructorInfo |= ConstructorTypes.Parametric;
                        }
                    }

                    Members = Reflection.GetMembers(type);
                }
            }
            //if (typeof (IEnumerable).IsAssignableFrom (type)) {
            //	return;
            //}
            //if (JsonDataType != JsonDataType.Undefined) {
            //	return;
            //}
        }