Ejemplo n.º 1
0
 public static bool IsRecursive(TypeReference declaringType, TypeReference fieldType)
 {
     // "built in" primitive .NET types are placed into itself... it is so stupid
     // field.FieldType.IsPrimitive || MonoType.IsString(field.FieldType) || MonoType.IsEnginePointer(field.FieldType) => return false
     if (MonoType.IsDelegate(fieldType))
     {
         return(false);
     }
     if (declaringType == fieldType)
     {
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
        private static TypeReference GetSerializedElementType(TypeReference type, IReadOnlyDictionary <GenericParameter, TypeReference> arguments)
        {
            TypeReference resolvedType = type.ContainsGenericParameter ? MonoUtils.ResolveGenericParameter(type, arguments) : type;

            if (resolvedType.IsArray)
            {
                ArrayType array = (ArrayType)resolvedType;
                return(array.ElementType);
            }
            if (MonoType.IsList(resolvedType))
            {
                GenericInstanceType generic = (GenericInstanceType)resolvedType;
                return(generic.GenericArguments[0]);
            }

            return(resolvedType);
        }
Ejemplo n.º 3
0
        public static bool IsFieldTypeSerializable(MonoSerializableScope scope)
        {
            TypeReference fieldType = scope.FieldType;

            // if it's generic parameter then get its real type
            if (fieldType.IsGenericParameter)
            {
                GenericParameter parameter = (GenericParameter)fieldType;
                fieldType = scope.Arguments[parameter];
            }

            if (fieldType.IsArray)
            {
                ArrayType array = (ArrayType)fieldType;
                // one dimention array only
                if (!array.IsVector)
                {
                    return(false);
                }

                // if it's generic parameter then get its real type
                TypeReference elementType = array.ElementType;
                if (elementType.IsGenericParameter)
                {
                    GenericParameter parameter = (GenericParameter)elementType;
                    elementType = scope.Arguments[parameter];
                }

                // array of arrays isn't serializable
                if (elementType.IsArray)
                {
                    return(false);
                }
                // array of generics isn't serializable
                if (MonoType.IsSerializableGeneric(elementType))
                {
                    return(false);
                }
                // check if array element is serializable
                MonoSerializableScope elementScope = new MonoSerializableScope(scope.DeclaringType, elementType, true, scope.Arguments);
                return(IsFieldTypeSerializable(elementScope));
            }

            if (MonoType.IsList(fieldType))
            {
                // list is serialized same way as array, so check its argument
                GenericInstanceType list        = (GenericInstanceType)fieldType;
                TypeReference       listElement = list.GenericArguments[0];

                // if it's generic parameter then get its real type
                if (listElement.IsGenericParameter)
                {
                    GenericParameter parameter = (GenericParameter)listElement;
                    listElement = scope.Arguments[parameter];
                }

                // list of arrays isn't serializable
                if (listElement.IsArray)
                {
                    return(false);
                }
                // list of buildin generics isn't serializable
                if (MonoType.IsBuiltinGeneric(listElement))
                {
                    return(false);
                }
                // check if list element is serializable
                MonoSerializableScope elementScope = new MonoSerializableScope(scope.DeclaringType, listElement, true, scope.Arguments);
                return(IsFieldTypeSerializable(elementScope));
            }

            if (fieldType.IsPrimitive)
            {
                return(true);
            }
            if (MonoType.IsString(fieldType))
            {
                return(true);
            }
            if (MonoType.IsEngineStruct(fieldType))
            {
                return(true);
            }
            if (MonoType.IsEnginePointer(fieldType))
            {
                return(true);
            }

            if (MonoType.IsObject(fieldType))
            {
                return(false);
            }
            if (fieldType.IsGenericInstance)
            {
                return(MonoType.IsSerializableGeneric(fieldType));
            }
            if (IsRecursive(scope.DeclaringType, fieldType))
            {
                return(scope.IsArrayElement);
            }

            TypeDefinition definition = fieldType.Resolve();

            if (definition.IsInterface)
            {
                return(false);
            }
            if (definition.IsAbstract)
            {
                return(false);
            }
            if (MonoType.IsCompilerGenerated(definition))
            {
                return(false);
            }
            if (definition.IsSerializable)
            {
                return(true);
            }
            if (definition.IsEnum)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
 public static bool IsSerializableArray(TypeReference type)
 {
     return(type.IsArray || MonoType.IsList(type));
 }
Ejemplo n.º 5
0
 private static bool IsArrayType(TypeReference type)
 {
     return(type.IsArray || MonoType.IsList(type));
 }