コード例 #1
0
        /// <summary>
        /// Helper method to find the Property <c>get</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 get.</param>
        /// <returns>
        /// The <see cref="BasicGetter"/> for the Property <c>get</c> or <see langword="null" />
        /// if the Property could not be found.
        /// </returns>
        internal static BasicGetter GetGetterOrNull(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);
            }

            PropertyInfo property =
                type.GetProperty(propertyName,
                                 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);

            if (property != null && property.CanRead)
            {
                return(new BasicGetter(type, property, propertyName));
            }
            else
            {
                // recursively call this method for the base Type
                BasicGetter getter = GetGetterOrNull(type.BaseType, propertyName);

                // didn't find anything in the base class - check to see if there is
                // an explicit interface implementation.
                if (getter == null)
                {
                    System.Type[] interfaces = type.GetInterfaces();
                    for (int i = 0; getter == null && i < interfaces.Length; i++)
                    {
                        getter = GetGetterOrNull(interfaces[i], propertyName);
                    }
                }
                return(getter);
            }
        }
コード例 #2
0
        /// <summary>
        /// Create a <see cref="BasicGetter"/> 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="BasicGetter"/> to use to get the value of the Property from 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 IGetter GetGetter(System.Type type, string propertyName)
        {
            BasicGetter result = GetGetterOrNull(type, propertyName);

            if (result == null)
            {
                throw new PropertyNotFoundException(type, propertyName, "getter");
            }
            return(result);
        }