Exemple #1
0
            private static bool DoesTypesHaveTheSameNumberOfFields(Type type1,
                                                                   Type type2)
            {
                var fieldsType2 = InternalSerializationStuff.GetFields(type2);
                var fieldsType1 = InternalSerializationStuff.GetFields(type1);

                return(fieldsType1.Length == fieldsType2.Length);
            }
 private void ComputeSerializationHashCode()
 {
     if (Type != null)
     {
         var fields = InternalSerializationStuff.GetFields(Type)
                      .OrderBy(f => f.Name,
                               StringComparer.Ordinal)
                      .Select(x => x.Name + x.FieldType.AssemblyQualifiedName);
         string stringToHash = String.Format("{0}{1}",
                                             Type.AssemblyQualifiedName,
                                             String.Join(",",
                                                         fields));
         unchecked
         {
             HashCode = 23;
             foreach (char c in stringToHash)
             {
                 HashCode = (HashCode * 31) + c;
             }
         }
     }
 }
        /// <summary>
        /// Throws an exception if the type is not supported for serialization or cloning
        /// </summary>
        /// <param name="type">Type to analyze</param>
        public static void ValidateSupportedTypes(Type type)
        {
            if (typeof(Expression).IsAssignableFrom(type))
            {
                throw new NotSupportedException(type.ToString());
            }

            if (typeof(Delegate).IsAssignableFrom(type))
            {
                throw new NotSupportedException(type.ToString());
            }

            if (type.IsPointer)
            {
                throw new NotSupportedException($"Pointer types such as {type} are not suported");
            }

            if (InternalSerializationStuff.GetFields(type).Any(x => x.FieldType.IsPointer))
            {
                throw new NotSupportedException($"Type {type} cannot contains fields that are pointers.");
            }

            if (type == typeof(IQueryable))
            {
                throw new NotSupportedException(type.ToString());
            }

            if (type == typeof(IEnumerable))
            {
                throw new NotSupportedException(type.ToString());
            }

            var enumerableType = IQueryableCloner.GetInterfaceType(type, typeof(IEnumerable <>));

            if (enumerableType != null)
            {
                var genericArgument = enumerableType.GetGenericArguments()[0];
                if (genericArgument.IsGenericType &&
                    genericArgument.GetGenericTypeDefinition() == typeof(IGrouping <,>))
                {
                    throw new NotSupportedException(type.ToString());
                }
            }

            if (Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false) &&
                type.IsGenericType && type.Name.Contains("AnonymousType") &&
                (type.Name.StartsWith("<>", StringComparison.OrdinalIgnoreCase)
                 ||
                 type.Name.StartsWith("VB$", StringComparison.OrdinalIgnoreCase)) &&
                (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic)
            {
                throw new NotSupportedException(type.ToString());
            }

            if (!type.IsArray &&
                type.Namespace != null &&
                (type.Namespace.StartsWith("System.") || type.Namespace.StartsWith("Microsoft.")) &&
                type.GetCustomAttribute <SerializableAttribute>() == null &&
                type != typeof(ExpandoObject) &&
                type != typeof(BigInteger))
            {
                throw new NotSupportedException(type.ToString());
            }
        }