Exemplo n.º 1
0
        /// <inheritdoc/>
        protected override void OnSetup(Type type, ConfigValueAttribute attribute)
        {
            ConfigNodeAttribute node = type.GetCustomAttribute <ConfigNodeAttribute>();

            Id = node?.Id ?? type.Name;

            // there should only be 1 root node for one type
            if (node != null)
            {
                IsRootNode = node.IsRoot;
                ShouldSave = node.ShouldSave;
                if (node.IsRoot)
                {
                    AllowMultiple = node.AllowMultiple;
                }
            }

            // if this is a member of some type
            if (attribute != null)
            {
                FARLogger.AssertFormat(!string.IsNullOrEmpty(attribute.Name),
                                       "Nested nodes required ConfigValue.Name to be set");
                Name = attribute.Name;
            }

            // get all public fields
            var fields = new HashSet <FieldInfo>(type.GetFields(PublicFlags));

            // and add all the other fields that declare ConfigValueAttribute
            fields.UnionWith(type.GetFieldsWithAttribute <ConfigValueAttribute>(flags: AllFlags)
                             .Select(pair => pair.Second));

            // only get properties that declare ConfigValueAttribute
            var properties = new List <PropertyInfo>(type.GetPropertiesWithAttribute <ConfigValueAttribute>(AllFlags)
                                                     .Select(pair => pair.Second));

            FARLogger.TraceFormat("Found {0} fields and {1} properties in {2}",
                                  fields.Count.ToString(),
                                  properties.Count.ToString(),
                                  type);

            foreach (FieldInfo fi in fields)
            {
                SetupType(fi, fi.FieldType);
            }

            foreach (PropertyInfo pi in properties)
            {
                SetupType(pi, pi.PropertyType);
            }

            if (!Children.TryGetValue(type, out List <Type> list))
            {
                return;
            }
            foreach (Type subnode in list)
            {
                SetupType(null, subnode);
            }
        }
Exemplo n.º 2
0
        protected override void OnSetup(Type type, ConfigValueAttribute attribute)
        {
            ValueType = ReflectionUtils.ListType(ValueType) ?? ValueType;

            if (attribute != null)
            {
                Name = attribute.Name;
            }

            ConfigNodeAttribute node = ValueType.GetCustomAttribute <ConfigNodeAttribute>();

            NodeId ??= node?.Id;
        }
Exemplo n.º 3
0
        public static ListValueReflection Create(
            MemberInfo mi,
            ConfigValueAttribute attribute = null,
            Type valueType = null
            )
        {
            var reflection = new ListValueReflection();

            if (valueType != null)
            {
                reflection.ValueType = valueType;
            }
            return(Factory(reflection, mi, attribute));
        }
Exemplo n.º 4
0
        private void Setup(MemberInfo mi, Type type, ConfigValueAttribute attribute = null)
        {
            // try and get the attribute since it will override some of the options
            attribute ??= type?.GetCustomAttribute <ConfigValueAttribute>();

            DeclaringType = mi?.DeclaringType;

            // if name has not been yet setup, use the name from the attribute if it was specified, otherwise default to the member name
            Name ??= attribute?.Name ?? mi?.Name;

            // if value type has not been setup, try to deduce it from the member type, then try to use type from the attribute or fallback to the provided type
            ValueType ??= ReflectionUtils.ConfigValueType(type) ?? attribute?.Type ?? type;

            // delegate the remaining setup to children
            OnSetup(type, attribute);
        }
Exemplo n.º 5
0
        private void SetupType(MemberInfo mi, Type memberType)
        {
            // if ignored, nothing to do
            if (mi?.GetCustomAttribute <ConfigValueIgnoreAttribute>() != null)
            {
                return;
            }

            // check if the type is a node and contains ConfigValueAttribute
            ConfigNodeAttribute  node  = memberType.GetCustomAttribute <ConfigNodeAttribute>();
            ConfigValueAttribute value = mi?.GetCustomAttribute <ConfigValueAttribute>();

            // try to get the list value type
            Type listValueType = ReflectionUtils.ListType(ReflectionUtils.ConfigValueType(memberType) ?? memberType);

            if (listValueType != null)
            {
                // is a list
                var reflection = ListValueReflection.Create(mi, value, listValueType);

                ListValues.Add(reflection);
                FARLogger.TraceFormat("Added list value '{1} -> <{0}, {2}>'",
                                      reflection.Name ?? "{null}",
                                      reflection.NodeId ?? "{null}",
                                      reflection.ValueType);
            }
            else if (node == null)
            {
                // not a node or a list -> simple value
                ValueReflection reflection = Create(mi, value);
                Values.Add(reflection);
                FARLogger.TraceFormat("Added value '{0} -> {1}'", reflection.Name, reflection.ValueType);
            }
            else
            {
                // ConfigValue name
                string name = value?.Name;

                // get clone or create new reflection for the type
                NodeReflection nodeReflection = GetReflection(memberType, true, name, mi);
                Nodes.Add(nodeReflection);
                FARLogger.TraceFormat("Added node '{1} -> <{0}, {2}>'",
                                      name ?? "{null}",
                                      nodeReflection.Id,
                                      nodeReflection.ValueType);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// A factory method for value reflections since virtual methods cannot be called in constructors
        /// </summary>
        /// <param name="reflection">the reflection to setup</param>
        /// <param name="mi">member info this reflection will use</param>
        /// <param name="attribute">optional attribute of the field/property, if null will try to get it automatically</param>
        /// <typeparam name="T">child class of ValueReflection</typeparam>
        /// <returns>reflection ready to use</returns>
        protected static T Factory <T>(T reflection, MemberInfo mi, ConfigValueAttribute attribute = null)
            where T : ValueReflection
        {
            // mi may be null when reflected value is not a member of a type
            if (mi is null)
            {
                reflection.Info = reflection.ValueType is null ? null : new SingletonMember(reflection.ValueType);
            }
            else
            {
                reflection.Info = SetupInfo(mi);
            }

            // this involves a virtual call so cannot be used in constructors
            // if mi is null, use existing value type
            reflection.Setup(mi, reflection.Info?.ValueType ?? reflection.ValueType, attribute);

            return(reflection);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Finalize this reflection setup
 /// </summary>
 /// <param name="type">Type of this reflection</param>
 /// <param name="attribute">ConfigValueAttribute for this reflection, null if not found</param>
 protected virtual void OnSetup(Type type, ConfigValueAttribute attribute)
 {
 }
Exemplo n.º 8
0
 public static ValueReflection Create(MemberInfo mi, ConfigValueAttribute attribute = null)
 {
     return(Factory(new ValueReflection(), mi, attribute));
 }