示例#1
0
        public void LoadFromType(Type type, GR2Writer writer)
        {
            Type = type;

            var attrs = type.GetCustomAttributes(typeof(StructSerializationAttribute), true);

            if (attrs.Length > 0)
            {
                StructSerializationAttribute serialization = attrs[0] as StructSerializationAttribute;
                MixedMarshal = serialization.MixedMarshal;
            }

            foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                var member = MemberDefinition.CreateFromFieldInfo(field, writer);
                if (member.SerializationKind != SerializationKind.None)
                {
                    Members.Add(MemberDefinition.CreateFromFieldInfo(field, writer));
                }
            }
        }
示例#2
0
        internal StructDefinition LookupStructDefinition(Type type, object instance)
        {
            StructDefinition defn = null;

            if (Types.TryGetValue(type, out defn))
            {
                return(defn);
            }

            if (type.GetInterfaces().Contains(typeof(System.Collections.IList)) || type.IsArray || type.IsPrimitive)
            {
                throw new ArgumentException("Cannot create a struct definition for array or primitive types");
            }

            var attrs = type.GetCustomAttributes(typeof(StructSerializationAttribute), true);

            if (attrs.Length > 0)
            {
                StructSerializationAttribute serialization = attrs[0] as StructSerializationAttribute;
                if (serialization.TypeSelector != null)
                {
                    var selector = Activator.CreateInstance(serialization.TypeSelector) as StructDefinitionSelector;
                    defn = selector.CreateStructDefinition(instance);
                    Types.Add(type, defn);
                }
            }

            if (defn == null)
            {
                defn = new StructDefinition();
                Types.Add(type, defn);
                defn.LoadFromType(type, this);
            }

            return(defn);
        }