Ejemplo n.º 1
0
        protected virtual Dictionary <string, XMLFieldHandler> IndexFields()
        {
            // Gather fields and create field handlers for them.
            PropertyInfo[] properties    = Type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            FieldInfo[]    fields        = Type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            var            fieldHandlers = new Dictionary <string, XMLFieldHandler>(properties.Length + fields.Length);

            for (var i = 0; i < properties.Length; i++)
            {
                PropertyInfo property = properties[i];

                // Only serialize properties with public get; set; and who aren't marked as "DontSerialize"
                MethodInfo readMethod  = property.GetMethod;
                MethodInfo writeMethod = property.SetMethod;
                if (!property.CanRead || !property.CanWrite || readMethod == null || writeMethod == null || !readMethod.IsPublic || !writeMethod.IsPublic ||
                    property.CustomAttributes.Any(x => x.AttributeType == XMLHelpers.DontSerializeAttributeType))
                {
                    continue;
                }

                XMLFieldHandler handler = XMLHelpers.ResolveFieldHandler(property.PropertyType, new XMLReflectionHandler(property));
                if (handler == null)
                {
                    continue;
                }
                fieldHandlers.TryAdd(property.Name, handler);
            }

            for (var i = 0; i < fields.Length; i++)
            {
                FieldInfo field = fields[i];

                // Exclude fields marked as "DontSerialize"
                if (field.CustomAttributes.Any(x => x.AttributeType == XMLHelpers.DontSerializeAttributeType))
                {
                    continue;
                }

                XMLFieldHandler handler = XMLHelpers.ResolveFieldHandler(field.FieldType, new XMLReflectionHandler(field));
                if (handler == null)
                {
                    continue;
                }
                fieldHandlers.TryAdd(field.Name, handler);
            }

            return(fieldHandlers);
        }
Ejemplo n.º 2
0
        public XMLKeyValueTypeHandler(Type type) : base(type)
        {
            PropertyInfo keyProperty = Type.GetProperty("Key");

            _keyHandler = new Lazy <XMLFieldHandler>(() => XMLHelpers.ResolveFieldHandler(keyProperty.PropertyType, new XMLReflectionHandler(keyProperty)));
            PropertyInfo valueProperty = Type.GetProperty("Value");

            _valueHandler = new Lazy <XMLFieldHandler>(() => XMLHelpers.ResolveFieldHandler(valueProperty.PropertyType, new XMLReflectionHandler(valueProperty)));

            Type opaqueKeyType = XMLHelpers.GetOpaqueType(keyProperty.PropertyType, out bool opaque);

            _keyDefault = opaque && opaqueKeyType.IsValueType ? Activator.CreateInstance(opaqueKeyType) : null;
            Type opaqueValueType = XMLHelpers.GetOpaqueType(valueProperty.PropertyType, out opaque);

            _valueDefault = opaque && opaqueValueType.IsValueType ? Activator.CreateInstance(opaqueValueType) : null;
        }