Ejemplo n.º 1
0
        /// <summary>
        /// Searches for a particular implementation of the given interface type inside of the type.
        /// This is particularly useful if the interface type is an open type, ie, typeof(IFace{}),
        /// because this method will then return IFace{} but with appropriate type parameters
        /// inserted.
        /// </summary>
        /// <param name="type">The base type to search for interface</param>
        /// <param name="interfaceType">The interface type to search for. Can be an open generic
        /// type.</param>
        /// <returns>The actual interface type that the type contains, or null if there is no
        /// implementation of the given interfaceType on type.</returns>
        public static Type GetInterface(Type type, Type interfaceType)
        {
            if (interfaceType.Resolve().IsGenericType &&
                interfaceType.Resolve().IsGenericTypeDefinition == false) {

                throw new ArgumentException("GetInterface requires that if the interface " +
                    "type is generic, then it must be the generic type definition, not a " +
                    "specific generic type instantiation");
            };

            while (type != null) {
                foreach (var iface in type.GetInterfaces()) {
                    if (iface.Resolve().IsGenericType) {
                        if (interfaceType == iface.GetGenericTypeDefinition()) {
                            return iface;
                        }
                    }

                    else if (interfaceType == iface) {
                        return iface;
                    }
                }

                type = type.Resolve().BaseType;
            }

            return null;
        }
        public override bool CanProcess(Type type)
        {
            return
                typeof(UnityObject).Resolve().IsAssignableFrom(type.Resolve()) == false &&
                typeof(ISerializationCallbackReceiver).Resolve().IsAssignableFrom(type.Resolve()) &&

                // If we are BaseObject, we explicitly do not want to invoke the callback receivers. Doing
                // so will mess up the serialization pipeline. This does not present an issue because the
                // object will be serialized normally/as expected by the regular FS pipeline.
                typeof(BaseObject).Resolve().IsAssignableFrom(type.Resolve()) == false;
        }
Ejemplo n.º 3
0
        private static Type RunSanityCheck(Type type) {
            if ((type.Resolve().IsPublic || type.Resolve().IsNestedPublic) == false) {
                Throw("sanity check -- type is not public for type={0}", type);
            }

            if (type.Resolve().IsGenericTypeDefinition) {
                Throw("sanity check -- type is a generic definition for type={0}", type);
            }

            return type;
        }
 public override bool CanProcess(Type type)
 {
     return
         type.Resolve().IsPrimitive ||
         type == typeof(string) ||
         type == typeof(decimal);
 }
        public InspectorCollectionAddItemAttributesAttribute(Type attributes)
        {
            if (typeof(fiICollectionAttributeProvider).Resolve().IsAssignableFrom(attributes.Resolve()) == false) {
                throw new ArgumentException("Must be an instance of FullInspector.fiICollectionAttributeProvider", "attributes");
            }

            var instance = (fiICollectionAttributeProvider)Activator.CreateInstance(attributes);
            AttributeProvider = fiAttributeProvider.Create(instance.GetAttributes().ToArray());
        }
        public override bool CanProcess(Type type)
        {
            if (type.Resolve().IsArray ||
                typeof(ICollection).IsAssignableFrom(type)) {
                return false;
            }

            return true;
        }
        /// <summary>
        /// Returns the serializer type that the given behavior type uses.
        /// </summary>
        public static Type GetSerializerType(Type behaviorType)
        {
            for (int i = 0; i < _mappings.Count; ++i) {
                var mapping = _mappings[i];
                if (mapping.BehaviorType.Resolve().IsAssignableFrom(behaviorType.Resolve())) {
                    return mapping.SerializerType;
                }
            }

            throw new InvalidOperationException("Unable to determine serializer for " + behaviorType.CSharpName());
        }
        /// <summary>
        /// Returns the serializer type that the given behavior type uses.
        /// </summary>
        public static Type GetSerializerType(Type behaviorType)
        {
            for (int i = 0; i < _mappings.Count; ++i) {
                var mapping = _mappings[i];
                if (mapping.BehaviorType.Resolve().IsAssignableFrom(behaviorType.Resolve())) {
                    return mapping.SerializerType;
                }
            }

            // No custom serializer, use the default one
            return fiInstalledSerializerManager.DefaultMetadata.SerializerType;
        }
Ejemplo n.º 9
0
        private fsMetaType(fsConfig config, Type reflectedType)
        {
            ReflectedType = reflectedType;

            List<fsMetaProperty> properties = new List<fsMetaProperty>();
            CollectProperties(config, properties, reflectedType);
            Properties = properties.ToArray();

            #if UNITY_EDITOR || UNITY_STANDALONE
            try
            {
                if (!ReflectedType.Resolve().IsValueType && ReflectedType.GetDeclaredConstructor(fsPortableReflection.EmptyTypes) != null ){
                    Generator = Expression.Lambda<Func<object>>(Expression.New(reflectedType)).Compile();
                }
            }
            catch{Generator = null;}
            #endif
        }
 private static string GetTooltipText(Type type)
 {
     string tooltip;
     if (!s_cachedTypeTooltips.TryGetValue(type, out tooltip)) {
         tooltip = InspectorTooltipAttribute.GetTooltip(type.Resolve());
         s_cachedTypeTooltips[type] = tooltip;
     }
     return tooltip;
 }
Ejemplo n.º 11
0
 /// <summary>
 /// If true, then the serializer will include inheritance data for the given converter.
 /// </summary>
 /// <param name="storageType">The field/property type that is currently storing the object
 /// that is being serialized.</param>
 public virtual bool RequestInheritanceSupport(Type storageType) {
     return storageType.Resolve().IsSealed == false;
 }
Ejemplo n.º 12
0
 public static bool IsInIgnoredAssembly(Type type) {
     return IsAssemblyAllowed(type.Resolve().Assembly) == false;
 }
Ejemplo n.º 13
0
        private static void CollectProperties(List<fsMetaProperty> properties, Type reflectedType)
        {
            // do we require a [SerializeField] or [fsProperty] attribute?
            bool requireOptIn = fsConfig.DefaultMemberSerialization == fsMemberSerialization.OptIn;
            bool requireOptOut = fsConfig.DefaultMemberSerialization == fsMemberSerialization.OptOut;

            fsObjectAttribute attr = fsPortableReflection.GetAttribute<fsObjectAttribute>(reflectedType);
            if (attr != null) {
                requireOptIn = attr.MemberSerialization == fsMemberSerialization.OptIn;
                requireOptOut = attr.MemberSerialization == fsMemberSerialization.OptOut;
            }

            MemberInfo[] members = reflectedType.GetDeclaredMembers();
            foreach (var member in members) {
                // We don't serialize members annotated with any of the ignore serialize attributes
                if (fsConfig.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {
                    continue;
                }

                PropertyInfo property = member as PropertyInfo;
                FieldInfo field = member as FieldInfo;

                // If an opt-in annotation is required, then skip the property if it doesn't have one
                // of the serialize attributes
                if (requireOptIn &&
                    !fsConfig.SerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {

                    continue;
                }

                // If an opt-out annotation is required, then skip the property *only if* it has one of
                // the not serialize attributes
                if (requireOptOut &&
                    fsConfig.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {

                    continue;
                }

                if (property != null) {
                    if (CanSerializeProperty(property, members, requireOptOut)) {
                        properties.Add(new fsMetaProperty(property));
                    }
                }
                else if (field != null) {
                    if (CanSerializeField(field, requireOptOut)) {
                        properties.Add(new fsMetaProperty(field));
                    }
                }
            }

            if (reflectedType.Resolve().BaseType != null) {
                CollectProperties(properties, reflectedType.Resolve().BaseType);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// If true, then the serializer will support cyclic references with the given converted
        /// type.
        /// </summary>
        /// <param name="storageType">The field/property type that is currently storing the object
        /// that is being serialized.</param>
        public virtual bool RequestCycleSupport(Type storageType) {
            if (storageType == typeof(string)) return false;

            return storageType.Resolve().IsClass || storageType.Resolve().IsInterface;
        }
Ejemplo n.º 15
0
        private static void CollectProperties(fsConfig config, List<fsMetaProperty> properties, Type reflectedType)
        {
            // do we require a [SerializeField] or [fsProperty] attribute?
            bool requireOptIn = config.DefaultMemberSerialization == fsMemberSerialization.OptIn;
            bool requireOptOut = config.DefaultMemberSerialization == fsMemberSerialization.OptOut;

            fsObjectAttribute attr = fsPortableReflection.GetAttribute<fsObjectAttribute>(reflectedType);
            if (attr != null) {
                requireOptIn = attr.MemberSerialization == fsMemberSerialization.OptIn;
                requireOptOut = attr.MemberSerialization == fsMemberSerialization.OptOut;
            }

            MemberInfo[] members = reflectedType.GetDeclaredMembers();
            foreach (var member in members) {
                // We don't serialize members annotated with any of the ignore
                // serialize attributes
                if (config.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {
                    continue;
                }

                PropertyInfo property = member as PropertyInfo;
                FieldInfo field = member as FieldInfo;

                // Early out if it's neither a field or a property, since we
                // don't serialize anything else.
                if (property == null && field == null) {
                    continue;
                }

                // Skip properties if we don't want them, to avoid the cost of
                // checking attributes.
                if (property != null && !config.EnablePropertySerialization) {
                    continue;
                }

                // If an opt-in annotation is required, then skip the property if
                // it doesn't have one of the serialize attributes
                if (requireOptIn &&
                    !config.SerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {
                    continue;
                }

                // If an opt-out annotation is required, then skip the property
                // *only if* it has one of the not serialize attributes
                if (requireOptOut &&
                    config.IgnoreSerializeAttributes.Any(t => fsPortableReflection.HasAttribute(member, t))) {
                    continue;
                }

                if (property != null) {
                    if (CanSerializeProperty(config, property, members, requireOptOut)) {
                        properties.Add(new fsMetaProperty(config, property));
                    }
                }
                else if (field != null) {
                    if (CanSerializeField(config, field, requireOptOut)) {
                        properties.Add(new fsMetaProperty(config, field));
                    }
                }
            }

            if (reflectedType.Resolve().BaseType != null) {
                CollectProperties(config, properties, reflectedType.Resolve().BaseType);
            }
        }
 public override bool CanProcess(Type type) {
     return
         type.Resolve().IsGenericType &&
         type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>);
 }
 public override bool CanProcess(Type type)
 {
     return typeof(UnityObject).Resolve().IsAssignableFrom(type.Resolve());
 }
        /// <summary>
        /// A simple type is a type that is either primitive, a string, or a
        /// non-generic non-abstract class composed of other simple types.
        /// </summary>
        private static bool IsSimpleTypeThatUnityCanSerialize(Type type)
        {
            if (IsPrimitiveSkippedByUnity(type)) {
                return false;
            }

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

            if (type == typeof(string)) {
                return true;
            }

            // TODO: Enable more complex detection for types which Unity can
            //       serialize by uncommenting this block. First, test it,
            //       though.
            /*
            if (type.IsClass && type.IsGenericType == false && type.IsAbstract == false) {
                BindingFlags flags =
                        BindingFlags.Instance |
                        BindingFlags.FlattenHierarchy |
                        BindingFlags.Public |
                        BindingFlags.NonPublic;

                // The type might contain a property that should be serialized.
                if (type.GetProperties(flags).Length > 0) {
                    return false;
                }

                // The type can have fields composed only of other simple types.
                // TODO: will this recurse infinitely for recursive types?
                foreach (var field in type.GetFields(flags)) {
                    if (IsSimpleType(field.FieldType) == false) {
                        return false;
                    }
                }

                return true;
            }
            */

            return false;
        }
 private bool IsStack(Type type)
 {
     return type.Resolve().IsGenericType &&
            type.Resolve().GetGenericTypeDefinition() == typeof(Stack<>);
 }
Ejemplo n.º 20
0
        private static bool CompareTypes(Type a, Type b) {
            // one of them is a definition, so make both of them a definition
            if ((a.Resolve().IsGenericType && b.Resolve().IsGenericType) &&
                (a.Resolve().IsGenericTypeDefinition || b.Resolve().IsGenericTypeDefinition)) {

                a = a.GetGenericTypeDefinition();
                b = b.GetGenericTypeDefinition();
            }

            return a == b;
        }
 public override bool CanConvert(Type objectType)
 {
     return typeof(UnityObject).Resolve().IsAssignableFrom(objectType.Resolve());
 }
Ejemplo n.º 22
0
 public override bool CanProcess(Type type) {
     return
         type.Resolve().IsGenericType &&
         type.GetGenericTypeDefinition() == typeof(Nullable<>);
 }
 public override bool CanProcess(Type type) {
     return
         typeof(UnityObject).Resolve().IsAssignableFrom(type.Resolve()) == false &&
         typeof(ISerializationCallbackReceiver).Resolve().IsAssignableFrom(type.Resolve());
 }