Exemple #1
0
        /// <summary>
        /// Sets the property value of the given object.
        /// </summary>
        /// <param name="obj">The target object.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="value">The property value.</param>
        public static void Set(object obj, string propertyName, object value)
        {
            Guard.ArgumentNotNullOrEmpty(obj, "obj");
            Guard.ArgumentNotNullOrEmpty(propertyName, "propertyName");
            PropertyAccessor    propertyAccessor;
            PropertyAccessorKey key = new PropertyAccessorKey(obj.GetType(), propertyName);

            if (propertyAccessors.ContainsKey(key))
            {
                propertyAccessor = propertyAccessors[key];
                propertyAccessor.Set(obj, value);
                return;
            }
            lock (synchHelper)
            {
                if (propertyAccessors.ContainsKey(key))
                {
                    propertyAccessor = propertyAccessors[key];
                    propertyAccessor.Set(obj, value);
                    return;
                }
                propertyAccessor       = new PropertyAccessor(obj.GetType(), propertyName);
                propertyAccessors[key] = propertyAccessor;
            }
            propertyAccessor.Set(obj, value);
        }
Exemple #2
0
        /// <summary>
        /// Gets the type of the property.
        /// </summary>
        /// <param name="targetType">Type of the target.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static Type GetPropertyType(Type targetType, string propertyName)
        {
            Guard.ArgumentNotNullOrEmpty(targetType, "targetType");
            Guard.ArgumentNotNullOrEmpty(propertyName, "propertyName");
            PropertyAccessorKey key = new PropertyAccessorKey(targetType, propertyName);

            if (propertyAccessors.ContainsKey(key))
            {
                return(propertyAccessors[key].PropertyType);
            }
            lock (synchHelper)
            {
                if (propertyAccessors.ContainsKey(key))
                {
                    return(propertyAccessors[key].PropertyType);
                }
                var propertyAccessor = new PropertyAccessor(targetType, propertyName);
                propertyAccessors[key] = propertyAccessor;
                return(propertyAccessor.PropertyType);
            }
        }