Exemplo n.º 1
0
        /// <summary>
        /// This method determines if the specified Type should be treated as a
        /// complex type by the framework.
        /// </summary>
        /// <param name="type">The type to check.</param>
        /// <returns>True if the type is a complex type, false otherwise.</returns>
        public static bool IsComplexType(Type type)
        {
#if !SERVERFX
            // Client side we can rely on derivaition from ComplexObject
            if (!typeof(ComplexObject).IsAssignableFrom(type))
            {
                return(false);
            }
#else
            if (!type.IsVisible || type.IsGenericType || type.IsAbstract)
            {
                return(false);
            }

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                return(false);
            }

            if (!type.IsClass)
            {
                return(false);
            }

            if (type.GetConstructor(Type.EmptyTypes) == null)
            {
                return(false);
            }

            if (TypeUtility.IsPredefinedType(type))
            {
                return(false);
            }

            // can't be a framework type
            if (IsSystemAssembly(type.Assembly))
            {
                return(false);
            }

            // server side only checks
            // can't be an entity
            if (TypeDescriptor.GetProperties(type).Cast <PropertyDescriptor>().Any(p => p.Attributes[typeof(KeyAttribute)] != null))
            {
                return(false);
            }
#endif
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns true if the specified property is a data member that should be serialized
        /// </summary>
        /// <param name="propertyDescriptor">The property to inspect</param>
        /// <returns>true if the specified property is a data member that should be serialized</returns>
        public static bool IsSerializableDataMember(PropertyDescriptor propertyDescriptor)
        {
            if (!(TypeUtility.IsPredefinedType(propertyDescriptor.PropertyType) || TypeUtility.IsSupportedComplexType(propertyDescriptor.PropertyType)))
            {
                return(false);
            }

            if (propertyDescriptor.Attributes[typeof(ExcludeAttribute)] != null)
            {
                // properties that are marked [Exclude] are not data members
                return(false);
            }

            if (propertyDescriptor.Attributes[typeof(AssociationAttribute)] != null)
            {
                // associations are not data members
                return(false);
            }

            AttributeCollection attrs = propertyDescriptor.ComponentType.Attributes();

            if (attrs[typeof(DataContractAttribute)] != null)
            {
                // [DataContract] on type, nothing on member
                if (propertyDescriptor.Attributes[typeof(DataMemberAttribute)] == null)
                {
                    return(false);
                }
            }
            else
            {
                // [IgnoreDataMember] on member
                if (propertyDescriptor.Attributes[typeof(IgnoreDataMemberAttribute)] != null)
                {
                    return(false);
                }
            }

            return(true);
        }