Ejemplo n.º 1
0
        /// <summary>
        /// Perform a single change
        /// </summary>
        /// <param name="property">Property to change</param>
        /// <param name="value">Value to set the property to</param>
        /// <param name="obj">The object to perform the change on</param>
        /// <returns>A string with either errors or succes messages.</returns>
        private static ChangeResult PerformSingleChange(PropertyInfo property, string value, object obj)
        {
            object val = null;

            if (property.PropertyType == typeof(int))
            {
                int v;
                if (int.TryParse(value, out v))
                {
                    val = v;
                }
            }
            else if (property.PropertyType == typeof(double))
            {
                double v;
                if (double.TryParse(value, out v))
                {
                    val = v;
                }
            }
            else if (property.PropertyType == typeof(float))
            {
                float v;
                if (float.TryParse(value, out v))
                {
                    val = v;
                }
            }
            else if (property.PropertyType == typeof(string))
            {
                val = value;
            }
            else
            {
                Console.WriteLine($"To Change.cs you gotta add the type {value.GetType()}. To test this create a {obj} {property.Name}:\"{value}\n");
                return(ChangeResult.Error(obj, value, property, "Type not found, author of this bot have been notified"));
            }

            if (val == null)
            {
                return(ChangeResult.Error(obj, value, property, "Invalid value"));
            }

            property.SetValue(obj, val);
            return(ChangeResult.Succes(obj, value, property));
        }