setValue() 공개 정적인 메소드

public static setValue ( String field, String value, object o ) : void
field String
value String
o object
리턴 void
예제 #1
0
        private object readObject(KSP.IO.TextReader reader, String name)
        {
            Type   t   = Type.GetType(NamespacePrefix + name);
            object obj = Activator.CreateInstance(t);

            String n;

            // now parse the lines and put them into the dictionary.
            // if there is another object inside, parse it and invoke "add"
            while ((n = nextLine(reader)) != null && !n.Equals("}"))
            {
                if (n.IndexOf('=') != -1)
                {
                    string[] parts = n.Split('=');
                    string   vname = parts[0].Trim();
                    string   value = n.Substring(n.IndexOf('=') + 1).Trim().Replace("\\n", "\n");

                    ReflectionTools.setValue(vname, value, obj);
                }
                else
                {
                    object inner = readObject(reader, n);
                    t.GetMethod("add", new Type[] { inner.GetType() }).Invoke(obj, new object[] { inner });
                }
            }
            return(obj);
        }
예제 #2
0
        /// <summary>
        /// Executes the instructions
        /// </summary>
        /// <returns>The instructions.</returns>
        /// <param name="seed">used seed</param>
        public void executeInstructions(System.Random random)
        {
            foreach (Instruction i in instructions)
            {
                String    value = "";
                FieldInfo info  = this.GetType().GetField(i.field, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

                // If the value starts with RANDOM(x, y)
                // We have to generate a new number
                if (i.value.StartsWith("RANDOM") && info.FieldType.Equals(typeof(double)))
                {
                    Match m = randRegex.Match(i.value);
                    if (m.Success)
                    {
                        double f1 = float.Parse(m.Groups[1].Value);
                        double f2 = float.Parse(m.Groups[2].Value);

                        value = "" + (random.NextDouble() * (f2 - f1) + f1);
                    }
                }

                // If the value starts with ADD(fieldName, floating point)
                // we have to get the requested value and add the second parameter
                if (i.value.StartsWith("ADD") && info.FieldType.Equals(typeof(double)))
                {
                    Match m = addRegex.Match(i.value);
                    if (m.Success)
                    {
                        String fname = m.Groups [1].Value;
                        double f2    = double.Parse(m.Groups[2].Value);

                        FieldInfo finfo = this.GetType().GetField(fname);
                        if (finfo == null || !finfo.FieldType.Equals(typeof(double)))
                        {
                            continue;
                        }

                        value = "" + ((double)finfo.GetValue(this) + f2);
                    }
                }

                if (i.value.StartsWith("TIME") && info.FieldType.Equals(typeof(double)))
                {
                    Match m = timeRegex.Match(i.value);
                    if (m.Success)
                    {
                        double ys = m.Groups [1].Success ? double.Parse(m.Groups[1].Value) : 0.0;
                        double ds = m.Groups [2].Success ? double.Parse(m.Groups[2].Value) : 0.0;
                        double hs = m.Groups [3].Success ? double.Parse(m.Groups[3].Value) : 0.0;
                        double ms = m.Groups [4].Success ? double.Parse(m.Groups[4].Value) : 0.0;
                        double ss = m.Groups [5].Success ? double.Parse(m.Groups[5].Value) : 0.0;

                        value = "" + (ys * (365.0 * 24.0 * 60.0 * 60.0) + ds * (24.0 * 60.0 * 60.0) + hs * (60.0 * 60.0) + ms * 60.0 + ss);
                    }
                }

                if (!value.Equals(""))
                {
                    ReflectionTools.setValue(i.field, value, this);
                }
            }

            foreach (FieldInfo info in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
                object obj = info.GetValue(this);
                if (obj is InstructionSet)
                {
                    ((InstructionSet)obj).executeInstructions(random);
                }

                if (obj != null && obj.GetType().GetInterface("IList") != null)
                {
                    IList ilist = (IList)obj;

                    foreach (object v in ilist)
                    {
                        if (v is InstructionSet)
                        {
                            ((InstructionSet)v).executeInstructions(random);
                        }
                    }
                }
            }
        }