private bool IsTypeValid(TypeReference reference) { if (m_validTypes.TryGetValue(reference.FullName, out bool isValid)) { return(isValid); } // set value at the beginning to prevent loop referencing m_validTypes[reference.FullName] = true; if (MonoType.IsPrime(reference)) { return(true); } if (reference.Module == null) { m_validTypes[reference.FullName] = false; return(false); } TypeDefinition definition = reference.Resolve(); isValid = IsTypeValid(definition.BaseType); if (!isValid) { m_validTypes[reference.FullName] = false; return(false); } foreach (FieldDefinition field in definition.Fields) { if (!MonoField.IsSerializableField(field)) { continue; } isValid = IsTypeValid(field.FieldType); if (!isValid) { m_validTypes[reference.FullName] = false; return(false); } } return(true); }
public static bool IsSerializableField(FieldDefinition field) { if (field.IsPublic) { if (field.IsNotSerialized) { return(false); } return(MonoType.IsSerializableType(field.FieldType)); } else { foreach (CustomAttribute attr in field.CustomAttributes) { if (IsSerializeFieldAttrribute(attr)) { return(MonoType.IsSerializableType(field.FieldType)); } } } return(false); }
private static IScriptStructure CreateBase(TypeDefinition type, IReadOnlyDictionary <GenericParameter, TypeReference> arguments) { if (MonoType.IsPrime(type.BaseType)) { return(null); } if (type.BaseType.IsGenericInstance) { Dictionary <GenericParameter, TypeReference> templateArguments = new Dictionary <GenericParameter, TypeReference>(); GenericInstanceType instance = (GenericInstanceType)type.BaseType; TypeDefinition template = instance.ElementType.Resolve(); for (int i = 0; i < instance.GenericArguments.Count; i++) { templateArguments.Add(template.GenericParameters[i], instance.GenericArguments[i].Resolve()); } return(new MonoStructure(template, templateArguments)); } TypeDefinition definition = type.BaseType.Resolve(); return(new MonoStructure(definition)); }
private static bool IsArrayType(TypeReference type) { return(type.IsArray || MonoType.IsList(type)); }
public static bool IsFieldTypeSerializable(TypeReference type, IReadOnlyDictionary <GenericParameter, TypeReference> arguments) { // if it's generic parameter then get its real type if (type.IsGenericParameter) { GenericParameter parameter = (GenericParameter)type; type = arguments[parameter]; } if (type.IsArray) { ArrayType array = (ArrayType)type; // 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 = arguments[parameter]; } // array of arrays isn't serializable if (elementType.IsArray) { return(false); } // array of lists isn't serializable if (MonoType.IsList(elementType)) { return(false); } // check if element is serializable return(IsFieldTypeSerializable(elementType, arguments)); } if (MonoType.IsList(type)) { // list is serialized same way as array, so check its argument GenericInstanceType list = (GenericInstanceType)type; TypeReference listElement = list.GenericArguments[0]; // if it's generic parameter then get its real type if (listElement.IsGenericParameter) { GenericParameter parameter = (GenericParameter)listElement; listElement = arguments[parameter]; } // list of arrays isn't serializable if (listElement.IsArray) { return(false); } // list of lists isn't serializable if (MonoType.IsList(listElement)) { return(false); } // check if element is serializable return(IsFieldTypeSerializable(listElement, arguments)); } if (type.IsPrimitive) { return(true); } if (MonoType.IsString(type)) { return(true); } if (MonoType.IsEngineStruct(type)) { return(true); } if (MonoType.IsEnginePointer(type)) { return(true); } if (type.IsGenericInstance) { return(false); } if (MonoType.IsObject(type)) { return(false); } TypeDefinition definition = type.Resolve(); if (definition.IsInterface) { return(false); } if (MonoType.IsCompilerGenerated(definition)) { return(false); } if (definition.IsSerializable) { return(true); } if (definition.IsEnum) { return(true); } return(false); }