Esempio n. 1
0
        //--------------------------------------------------------------------------------------
        /// <summary>
        /// SetValue - Set a property to a literal value
        /// </summary>
        //--------------------------------------------------------------------------------------
        internal void SetValue(string propertyName, string valueText, bool throwOnMissingProperty)
        {
            var eventInfo = GetType().GetTypeInfo().GetEvent(propertyName, _publicInstance);

            if (eventInfo != null)
            {
                AddBinding(propertyName, valueText);
                return;
            }
            var propertyInfo = GetType().GetTypeInfo().GetProperty(propertyName);

            if (propertyInfo == null)
            {
                if (throwOnMissingProperty)
                {
                    throw new ApplicationException("Cannot find property " + propertyName + " on type " + GetType().Name);
                }
                return;
            }

            // If the property is a dictionary, then we'll try to add this value, otherwise, we set the property
            if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.Name == "Dictionary`2")
            {
                // Break up string into separate key/Value pairs
                var keyValuePairs = valueText.Split('`');
                foreach (var keyValuePair in keyValuePairs)
                {
                    // Break up name=value into actual key and value objects
                    var valueParts = keyValuePair.Split(new[] { '=' }, 2);
                    if (valueParts.Length != 2)
                    {
                        throw new ApplicationException("Expected format of Name=Value");
                    }
                    var types = propertyInfo.PropertyType.GenericTypeArguments;
                    var key   = UIHelpers.GetValueFromText(types[0], valueParts[0]);
                    var value = UIHelpers.GetValueFromText(types[1], valueParts[1]);

                    // Create dictionary object if not there
                    var dictionary = propertyInfo.GetValue(this);
                    if (dictionary == null)
                    {
                        dictionary = Activator.CreateInstance(propertyInfo.PropertyType);
                        propertyInfo.SetValue(this, dictionary);
                    }

                    // Add to the dictionary
                    var add = propertyInfo.PropertyType.GetMethod("Add", types);
                    add.Invoke(dictionary, new[] { key, value });
                }
            }
            else
            {
                if (propertyName == "Size")
                {
                    var value = UIHelpers.GetValueFromText(typeof(Tuple <float?, float?>), valueText);
                    SpecifiedSize = (Tuple <float?, float?>)value;
                }
                else
                {
                    var value = UIHelpers.GetValueFromText(propertyInfo.PropertyType, valueText);
                    propertyInfo.SetValue(this, value);
                }
            }
        }