コード例 #1
0
        /// <summary>
        /// Helper method to find the Property <c>set</c>.
        /// </summary>
        /// <param name="type">The <see cref="System.Type"/> to find the Property in.</param>
        /// <param name="propertyName">The name of the mapped Property to set.</param>
        /// <returns>
        /// The <see cref="BasicSetter"/> for the Property <c>set</c> or <see langword="null" />
        /// if the Property could not be found.
        /// </returns>
        internal static BasicSetter GetSetterOrNull(System.Type type, string propertyName)
        {
            if (type == typeof(object) || type == null)
            {
                // the full inheritance chain has been walked and we could
                // not find the Property get
                return(null);
            }

            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;

            PropertyInfo property = type.GetProperty(propertyName, bindingFlags);

            if (property != null && property.CanWrite)
            {
                return(new BasicSetter(type, property, propertyName));
            }

            // recursively call this method for the base Type
            BasicSetter setter = GetSetterOrNull(type.BaseType, propertyName);

            // didn't find anything in the base class - check to see if there is
            // an explicit interface implementation.
            if (setter == null)
            {
                System.Type[] interfaces = type.GetInterfaces();
                for (int i = 0; setter == null && i < interfaces.Length; i++)
                {
                    setter = GetSetterOrNull(interfaces[i], propertyName);
                }
            }
            return(setter);
        }
コード例 #2
0
        /// <summary>
        /// Create a <see cref="BasicSetter"/> for the mapped property.
        /// </summary>
        /// <param name="type">The <see cref="System.Type"/> to find the Property in.</param>
        /// <param name="propertyName">The name of the mapped Property to get.</param>
        /// <returns>
        /// The <see cref="BasicSetter"/> to use to set the value of the Property on an
        /// instance of the <see cref="System.Type"/>.
        /// </returns>
        /// <exception cref="PropertyNotFoundException" >
        /// Thrown when a Property specified by the <c>propertyName</c> could not
        /// be found in the <see cref="System.Type"/>.
        /// </exception>
        public ISetter GetSetter(System.Type type, string propertyName)
        {
            BasicSetter result = GetSetterOrNull(type, propertyName);

            if (result == null)
            {
                throw new PropertyNotFoundException(type, propertyName, "setter");
            }
            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Helper method to find the Property <c>set</c>.
        /// </summary>
        /// <param name="type">The <see cref="System.Type"/> to find the Property in.</param>
        /// <param name="propertyName">The name of the mapped Property to set.</param>
        /// <returns>
        /// The <see cref="BasicSetter"/> for the Property <c>set</c> or <see langword="null" />
        /// if the Property could not be found.
        /// </returns>
        internal static BasicSetter GetSetterOrNull(System.Type type, string propertyName)
        {
            if (type == typeof(object) || type == null)
            {
                // the full inheritance chain has been walked and we could
                // not find the Property get
                return(null);
            }

            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;

            if (type.IsValueType)
            {
                // the BindingFlags.IgnoreCase is important here because if type is a struct, the GetProperty method does
                // not ignore case by default. If type is a class, it _does_ ignore case... we're better off explicitly
                // stating that casing should be ignored so we get the same behavior for both structs and classes
                bindingFlags = bindingFlags | BindingFlags.IgnoreCase;
            }

            PropertyInfo property = type.GetProperty(propertyName, bindingFlags);

            if (property != null && property.CanWrite)
            {
                return(new BasicSetter(type, property, propertyName));
            }

            // recursively call this method for the base Type
            BasicSetter setter = GetSetterOrNull(type.BaseType, propertyName);

            // didn't find anything in the base class - check to see if there is
            // an explicit interface implementation.
            if (setter == null)
            {
                System.Type[] interfaces = type.GetInterfaces();
                for (int i = 0; setter == null && i < interfaces.Length; i++)
                {
                    setter = GetSetterOrNull(interfaces[i], propertyName);
                }
            }
            return(setter);
        }