Exemplo n.º 1
0
        public void MapType(object instance)
        {
            if (Type == null)
            {
                Type = instance.GetType();
                foreach (var field in Type.GetFields(BindingFlags.Instance | BindingFlags.Public))
                {
                    var name  = field.Name;
                    var attrs = field.GetCustomAttributes(typeof(SerializationAttribute), true);
                    if (attrs.Length > 0)
                    {
                        SerializationAttribute serialization = attrs[0] as SerializationAttribute;
                        if (serialization.Name != null)
                        {
                            name = serialization.Name;
                        }
                    }

                    foreach (var member in Members)
                    {
                        if (member.Name == name)
                        {
                            member.AssignFieldInfo(field);
                        }
                    }
                }
            }

            // If this assertion is triggered it most likely is because multiple C# types
            // were assigned to the same Granny type in different places in the class definitions
            Debug.Assert(Type == instance.GetType());
        }
Exemplo n.º 2
0
        private void LoadAttributes(FieldInfo info, GR2Writer writer)
        {
            var attrs = info.GetCustomAttributes(typeof(SerializationAttribute), true);

            if (attrs.Length > 0)
            {
                SerializationAttribute serialization = attrs[0] as SerializationAttribute;

                if (serialization.Section != SectionType.Invalid)
                {
                    PreferredSection = serialization.Section;
                }

                DataArea = serialization.DataArea;

                if (serialization.Type != MemberType.Invalid)
                {
                    Type = serialization.Type;
                }

                if (serialization.TypeSelector != null)
                {
                    TypeSelector = Activator.CreateInstance(serialization.TypeSelector) as VariantTypeSelector;
                }

                if (serialization.SectionSelector != null)
                {
                    SectionSelector = Activator.CreateInstance(serialization.SectionSelector) as SectionSelector;
                }

                if (serialization.Serializer != null)
                {
                    Serializer = Activator.CreateInstance(serialization.Serializer) as NodeSerializer;
                }

                if (writer != null && serialization.Prototype != null)
                {
                    WriteDefinition = writer.LookupStructDefinition(serialization.Prototype);
                }

                if (serialization.Name != null)
                {
                    GrannyName = serialization.Name;
                }

                Prototype         = serialization.Prototype;
                SerializationKind = serialization.Kind;
                ArraySize         = serialization.ArraySize;
                MinVersion        = serialization.MinVersion;
                MaxVersion        = serialization.MaxVersion;
            }
        }