예제 #1
0
        private static void SetValueWithCoercion(object target, DynamicMemberHandle handle, object value)
        {
            if (value == null)
            {
                return;
            }

            Type   pType = handle.MemberType;
            Type   vType = GetUnderlyingType(value.GetType());
            object v     = CoerceValue(pType, vType, value);

            if (v != null)
            {
                handle.DynamicMemberSet(target, v);
            }
        }
예제 #2
0
        /// <summary>
        /// Gets an object's property value by name.
        /// </summary>
        /// <param name="target">Object containing the property to get.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="useCache">if set to <c>true</c> use dynamic cache.</param>
        /// <returns>The value of the property.</returns>
        public static object GetPropertyValue(object target, string propertyName, bool useCache)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (String.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            if (useCache)
            {
                DynamicMemberHandle handle = MethodCaller.GetCachedProperty(target.GetType(), propertyName);
                return(handle.DynamicMemberGet(target));
            }

            PropertyInfo propertyInfo = MethodCaller.FindProperty(target.GetType(), propertyName);

            return(propertyInfo.GetValue(target, null));
        }
예제 #3
0
        internal static DynamicMemberHandle GetCachedField(Type objectType, string fieldName)
        {
            var key = new MethodCacheKey(objectType.FullName, fieldName, GetParameterTypes(null));
            DynamicMemberHandle mh = null;

            if (_memberCache.TryGetValue(key, out mh))
            {
                return(mh);
            }

            lock (_memberCache)
            {
                if (!_memberCache.TryGetValue(key, out mh))
                {
                    FieldInfo info = objectType.GetField(fieldName, fieldFlags);
                    if (info != null)
                    {
                        mh = new DynamicMemberHandle(info);
                    }
                    _memberCache.Add(key, mh);
                }
            }
            return(mh);
        }
예제 #4
0
        internal static DynamicMemberHandle GetCachedProperty(Type objectType, string propertyName)
        {
            var key = new MethodCacheKey(objectType.FullName, propertyName, GetParameterTypes(null));
            DynamicMemberHandle mh = null;

            if (_memberCache.TryGetValue(key, out mh))
            {
                return(mh);
            }

            lock (_memberCache)
            {
                if (!_memberCache.TryGetValue(key, out mh))
                {
                    PropertyInfo info = FindProperty(objectType, propertyName);
                    if (info != null)
                    {
                        mh = new DynamicMemberHandle(info);
                    }
                    _memberCache.Add(key, mh);
                }
            }
            return(mh);
        }
예제 #5
0
        /// <summary>
        /// Sets an object's property with the specified value,
        /// converting that value to the appropriate type if possible.
        /// </summary>
        /// <param name="target">Object containing the property to set.</param>
        /// <param name="propertyName">Name of the property to set.</param>
        /// <param name="value">Value to set into the property.</param>
        /// <param name="useCache">if set to <c>true</c> use dynamic cache.</param>
        public static void SetPropertyValue(object target, string propertyName, object value, bool useCache)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target", "Target object can not be Null.");
            }

            if (useCache)
            {
                DynamicMemberHandle handle = MethodCaller.GetCachedProperty(target.GetType(), propertyName);
                if (handle != null)
                {
                    SetValueWithCoercion(target, handle, value);
                }
            }
            else
            {
                PropertyInfo propertyInfo = MethodCaller.FindProperty(target.GetType(), propertyName);
                if (propertyInfo != null)
                {
                    SetValueWithCoercion(target, propertyInfo, value);
                }
            }
        }
예제 #6
0
        internal static DynamicMemberHandle GetCachedProperty(Type objectType, string propertyName)
        {
            var key = new MethodCacheKey(objectType.FullName, propertyName, GetParameterTypes(null));
            DynamicMemberHandle mh = null;
            if (_memberCache.TryGetValue(key, out mh))
                return mh;

            lock (_memberCache)
            {
                if (!_memberCache.TryGetValue(key, out mh))
                {
                    PropertyInfo info = FindProperty(objectType, propertyName);
                    if (info != null)
                        mh = new DynamicMemberHandle(info);
                    _memberCache.Add(key, mh);
                }
            }
            return mh;
        }
예제 #7
0
        internal static DynamicMemberHandle GetCachedField(Type objectType, string fieldName)
        {
            var key = new MethodCacheKey(objectType.FullName, fieldName, GetParameterTypes(null));
            DynamicMemberHandle mh = null;
            if (_memberCache.TryGetValue(key, out mh))
                return mh;

            lock (_memberCache)
            {
                if (!_memberCache.TryGetValue(key, out mh))
                {
                    FieldInfo info = objectType.GetField(fieldName, fieldFlags);
                    if (info != null)
                        mh = new DynamicMemberHandle(info);
                    _memberCache.Add(key, mh);
                }
            }
            return mh;
        }
예제 #8
0
        private static void SetValueWithCoercion(object target, DynamicMemberHandle handle, object value)
        {
            if (value == null)
                return;

            Type pType = handle.MemberType;
            Type vType = GetUnderlyingType(value.GetType());
            object v = CoerceValue(pType, vType, value);
            if (v != null)
                handle.DynamicMemberSet(target, v);
        }
예제 #9
0
        /// <summary>
        /// Invokes a property setter using dynamic
        /// method invocation.
        /// </summary>
        /// <param name="obj">Target object.</param>
        /// <param name="property">Property to invoke.</param>
        /// <param name="value">New value for property.</param>
        public static void CallPropertySetter(object obj, string property, object value)
        {
            DynamicMemberHandle mh = GetCachedProperty(obj.GetType(), property);

            mh.DynamicMemberSet(obj, value);
        }
예제 #10
0
        /// <summary>
        /// Invokes a property getter using dynamic
        /// method invocation.
        /// </summary>
        /// <param name="obj">Target object.</param>
        /// <param name="property">Property to invoke.</param>
        /// <returns></returns>
        public static object CallPropertyGetter(object obj, string property)
        {
            DynamicMemberHandle mh = GetCachedProperty(obj.GetType(), property);

            return(mh.DynamicMemberGet(obj));
        }