Пример #1
0
 static void ForceSerialization(TypeConfig typeConfig)
 {
     typeConfig.TypeConstruction      = TypeConstruction.ByUninitialized();
     typeConfig.ReadonlyFieldOverride = ReadonlyFieldHandling.ForcedOverwrite;
     typeConfig.TargetMembers         = TargetMember.PrivateFields;
     typeConfig.CustomResolver        = ForceDynamicResolver;
 }
Пример #2
0
        static void ApplyConstructorDefaults(TypeConfig typeConfig)
        {
            if (typeConfig.TypeConstruction != null)
            {
                return;
            }


            if (typeConfig.TypeConstruction == null)
            {
                if (CerasSerializer.IsFormatterConstructed(typeConfig.Type) || (typeConfig.Type.IsStatic()))
                {
                    typeConfig.TypeConstruction = TypeConstruction.Null();
                    return;
                }
            }


            var        type = typeConfig.Type;
            MethodBase ctor;

            // Try hint by attribute
            IEnumerable <MethodBase> methods = type.GetMethods(BindingFlagsStatic).Cast <MethodBase>().Concat(type.GetConstructors(BindingFlagsCtor));
            var cerasCtors = methods.Where(m => m.GetCustomAttribute <CerasConstructorAttribute>() != null).ToArray();

            if (cerasCtors.Length > 1)
            {
                // Multiple matches
                throw new InvalidConfigException($"There are multiple constructors on your type '{typeConfig.Type.Name}' that have the '[CerasConstructor]' attribute, so its unclear which one to use. Only one constructor in this type can have the attribute.");
            }
            else if (cerasCtors.Length == 1)
            {
                // Single match
                ctor = cerasCtors[0];
            }
            else
            {
                // No match, try default ctor
                ctor = type.GetConstructors(BindingFlagsCtor).FirstOrDefault(c => c.GetParameters().Length == 0);
            }

            // Apply this ctor or factory
            if (ctor != null)
            {
                if (ctor is ConstructorInfo constructorInfo)
                {
                    typeConfig.TypeConstruction = TypeConstruction.ByConstructor(constructorInfo);
                }
                else if (ctor is MethodInfo methodInfo)
                {
                    typeConfig.TypeConstruction = TypeConstruction.ByStaticMethod(methodInfo);
                }
            }


            if (type.IsValueType && typeConfig.TypeConstruction == null)
            {
                typeConfig.TypeConstruction = ConstructNull.Instance;
            }
        }
Пример #3
0
        // Built-in support for some specific types
        internal static void ApplySpecializedDefaults(TypeConfig typeConfig)
        {
            var type = typeConfig.Type;

            // All structs
            if (!type.IsPrimitive && type.IsValueType)
            {
                if (typeConfig.TypeConstruction == null)
                {
                    typeConfig.TypeConstruction = ConstructNull.Instance;
                }

                typeConfig.TargetMembers = TargetMember.AllFields;
                return;
            }

            if (type.Assembly == typeof(Expression).Assembly)
            {
                if (!type.IsAbstract && type.IsSubclassOf(typeof(Expression)))
                {
                    typeConfig.TypeConstruction      = TypeConstruction.ByUninitialized();
                    typeConfig.ReadonlyFieldOverride = ReadonlyFieldHandling.ForcedOverwrite;
                    typeConfig.TargetMembers         = TargetMember.AllFields;
                    typeConfig.CustomResolver        = ForceDynamicResolver;
                    return;
                }

                if (type.FullName.StartsWith("System.Runtime.CompilerServices.TrueReadOnlyCollection"))
                {
                    typeConfig.TypeConstruction      = TypeConstruction.ByUninitialized();
                    typeConfig.ReadonlyFieldOverride = ReadonlyFieldHandling.ForcedOverwrite;
                    typeConfig.TargetMembers         = TargetMember.PrivateFields;
                    typeConfig.CustomResolver        = ForceDynamicResolver;
                    return;
                }
            }

            if (type.Assembly == typeof(ReadOnlyCollection <>).Assembly)
            {
                if (type.FullName.StartsWith("System.Collections.ObjectModel.ReadOnlyCollection"))
                {
                    typeConfig.TypeConstruction      = TypeConstruction.ByUninitialized();
                    typeConfig.ReadonlyFieldOverride = ReadonlyFieldHandling.ForcedOverwrite;
                    typeConfig.TargetMembers         = TargetMember.PrivateFields;
                    typeConfig.CustomResolver        = ForceDynamicResolver;
                    return;
                }
            }
        }