internal static bool IsBlittable(Type elementType)
        {
            return(_isBlittableCache.GetOrAdd(elementType, _ =>
            {
                if (!elementType.IsValueType)
                {
                    return false;
                }

                if (!TypeFields.IsPrimitive(elementType))
                {
                    return false;
                }

                try
                {
                    WriteArrayOfValuesMethod1.MakeGenericMethod(elementType);
                    return true;
                }
                catch
                {
                    return false;
                }
            }));
        }
예제 #2
0
        internal bool IsTypeSerializable(Type type)
        {
            if (type.IsGenericType && !type.IsGenericTypeDefinition)
            {
                type = type.GetGenericTypeDefinition();
                if (_autoWhitelistedTypes.Contains(type))
                {
                    return(true);
                }
            }

            if (type == typeof(FieldInfoModifier.TestReadonly))
            {
                return(true);
            }

            if (type.IsArray)
            {
                return(true);
            }

            if (typeof(Delegate).IsAssignableFrom(type))
            {
                return(true);
            }

            if (typeof(Type).IsAssignableFrom(type))
            {
                return(true);
            }

            if (type.GetCustomAttribute(typeof(CompilerGeneratedAttribute)) != null)
            {
                return(true);
            }

            if (TypeFields.IsPrimitive(type))
            {
                return(true);
            }

            if (IsSpecialCoreType(type))
            {
                return(true);
            }

            if (_autoWhitelistedTypes.Contains(type))
            {
                return(true);
            }

            var declaringTypeIsSerializeable = type.DeclaringType != null && IsTypeSerializable(type.DeclaringType);

            return(declaringTypeIsSerializeable ||
                   WhitelistedTypes.Contains(type) ||
                   WhitelistFuncs.Any(x => x(type)));
        }