Пример #1
0
        public NodeReflection(NodeReflection other, string name = null, MemberInfo mi = null) : base(other, mi)
        {
            Name          = name;
            Id            = other.Id;
            AllowMultiple = other.AllowMultiple;

            // copy reflected values
            Values.Capacity = other.Values.Count;
            foreach (ValueReflection reflection in other.Values)
            {
                Values.Add(reflection);
            }

            // copy reflected list values
            ListValues.Capacity = other.ListValues.Count;
            foreach (ListValueReflection list in other.ListValues)
            {
                ListValues.Add(list);
            }

            // copy reflected nodes
            Nodes.Capacity = other.Nodes.Count;
            foreach (NodeReflection node in other.Nodes)
            {
                Nodes.Add(node);
            }
        }
 /// <inheritdoc />
 public override void VisitNode(object owner, NodeReflection reflection)
 {
     if (Saver.OnNode(reflection.GetMember(owner), reflection, out object newValue))
     {
         reflection.SetMember(owner, newValue, true);
     }
 }
Пример #3
0
        public void Initialize(bool force = false)
        {
            if (initialized && !force)
            {
                return;
            }

            try
            {
                var allNodes =
                    new List <Pair <ConfigNodeAttribute, Type> >(ReflectionUtils
                                                                 .FindAttribute <ConfigNodeAttribute>(false));

                foreach (Pair <ConfigNodeAttribute, Type> pair in allNodes)
                {
                    if (!pair.First.IsRoot)
                    {
                        continue;
                    }
                    var node = NodeReflection.GetReflection(pair.Second);
                    nodes.Add(node.Id,
                              new ReflectedConfig
                    {
                        Instance   = ReflectionUtils.FindInstance(pair.Second),
                        Reflection = node
                    });
                }

                FARLogger.TraceFormat("Config nodes found: {0}", string.Join(", ", nodes.Select(p => p.Key)));
            }
            finally
            {
                initialized = true;
            }
        }
 /// <inheritdoc />
 public override void VisitNode(object owner, NodeReflection reflection)
 {
     if (Loader.OnNode(reflection.GetMember(owner), reflection, out object value) && value != null)
     {
         reflection.SetMember(owner, value, true);
     }
 }
 /// <inheritdoc />
 public override void VisitNodeList(
     object owner,
     ListValueReflection reflection,
     NodeReflection nodeReflection
     )
 {
     if (Loader.OnNodeList(reflection, nodeReflection, out IList list))
     {
         CopyList(list, reflection.GetMember(owner) as IList, reflection.ValueType);
     }
 }
Пример #6
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);
            }
        }
            /// <inheritdoc />
            public override void VisitNodeList(
                object owner,
                ListValueReflection reflection,
                NodeReflection nodeReflection
                )
            {
                if (!(reflection.GetMember(owner) is IList member))
                {
                    return;
                }

                for (int i = 0; i < member.Count; i++)
                {
                    if (Saver.OnListNode(i, member[i], reflection, nodeReflection, out object newValue))
                    {
                        member[i] = newValue;
                    }
                }
            }
Пример #8
0
        private int Apply(ref object owner, NodeVisitor visitor)
        {
            int count = 0;

            FARLogger.TraceFormat("Applying visitor to config node {0}[{1}]", Id, Name ?? "{null}");
            foreach (ValueReflection value in Values)
            {
                FARLogger.TraceFormat("Visiting value {0}[{1}].{2}", Id, Name, value.Name);
                try
                {
                    visitor.VisitValue(owner, value);
                }
                catch (Exception e)
                {
                    FARLogger.ExceptionFormat(e, "Exception loading value {0} in {1}", value.Name, value.DeclaringType);
                    count++;
                }
            }

            foreach (ListValueReflection reflection in ListValues)
            {
                if (reflection.IsNodeValue)
                {
                    NodeReflection nodeReflection = GetReflection(reflection.ValueType);

                    FARLogger.TraceFormat("Visiting list nodes {0}[{1}].{2}[{3}]",
                                          Id,
                                          Name ?? "{null}",
                                          reflection.NodeId,
                                          reflection.Name ?? "{null}");
                    try
                    {
                        visitor.VisitNodeList(owner, reflection, nodeReflection);
                    }
                    catch (Exception e)
                    {
                        FARLogger.ExceptionFormat(e,
                                                  "Exception loading node ({2}) list {0} in {1}",
                                                  reflection.Name,
                                                  reflection.DeclaringType,
                                                  reflection.NodeId);
                        count++;
                    }
                }
                else
                {
                    FARLogger.TraceFormat("Visiting list values {0}[{1}].{2}", Id, Name ?? "{null}", reflection.Name);
                    try
                    {
                        visitor.VisitValueList(owner, reflection);
                    }
                    catch (Exception e)
                    {
                        FARLogger.ExceptionFormat(e,
                                                  "Exception loading value list {0} in {1}",
                                                  reflection.Name,
                                                  reflection.DeclaringType);
                        count++;
                    }
                }
            }

            foreach (NodeReflection node in Nodes)
            {
                FARLogger.TraceFormat("Visiting subnode {0}[{1}].{2}[{3}]",
                                      Id,
                                      Name ?? "{null}",
                                      node.Id,
                                      node.Name ?? "{null}");
                try
                {
                    visitor.VisitNode(owner, node);
                }
                catch (Exception e)
                {
                    FARLogger.ExceptionFormat(e,
                                              "Exception loading node {2}[{0}] in {1}",
                                              node.Name,
                                              node.DeclaringType,
                                              node.Id);
                    count++;
                }
            }

            return(count);
        }
 public abstract void VisitNode(object owner, NodeReflection reflection);
 public abstract void VisitNodeList(
     object owner,
     ListValueReflection reflection,
     NodeReflection nodeReflection
     );