Example #1
0
        /// <summary>
        /// Add a converter as general converter.
        /// </summary>
        /// <param name="sourceType">The source type of this converter</param>
        /// <param name="targetType">The target type of this converter</param>
        /// <param name="converter">The converter instance</param>
        public void AddConverter(Type sourceType, Type targetType, IValueConverter converter)
        {
            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }

            if (sourceType == null)
            {
                throw new ArgumentNullException("sourceType");
            }

            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }

            if (sourceType == targetType)
            {
                throw new ArgumentException("source type and the target type must be different");
            }

            // check key
            if (typeDictionary.ContainsKey(sourceType, targetType))
            {
                Debug.LogErrorFormat("Converter ({0}, {1}) already exist", sourceType, targetType);
                return;
            }

            // add it
            typeDictionary.Add(sourceType, targetType, converter);
        }
Example #2
0
        public PropertyAccessor GetPropertyAccessor(Type sourceType, string propertyName)
        {
            // check if accessor exists
            PropertyAccessor accessor;

            if (propertyAccessorDictionary.TryGetValue(sourceType, propertyName, out accessor))
            {
                // just return it
                return(accessor);
            }

            // get property info
            var propertyInfo = sourceType.GetProperty(propertyName);

            if (propertyInfo == null)
            {
                // invalid property name
                return(null);
            }

            // create new accessor
            accessor = new PropertyAccessor(propertyInfo);

            // add it
            propertyAccessorDictionary.Add(sourceType, propertyName, accessor);

            return(accessor);
        }