Пример #1
0
        /// <summary>
        /// Returns the original, backing member of an alias member if the member is an alias.
        /// </summary>
        /// <param name="memberInfo">The member to check.</param>
        /// /// <param name="throwOnNotAliased">if set to <c>true</c> an exception will be thrown if the member is not aliased.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">The member was not aliased; this only occurs if throwOnNotAliased is true.</exception>
        public static MemberInfo DeAlias(this MemberInfo memberInfo, bool throwOnNotAliased = false)
        {
            MemberAliasFieldInfo aliasFieldInfo = memberInfo as MemberAliasFieldInfo;

            if (aliasFieldInfo != null)
            {
                return(aliasFieldInfo.AliasedField);
            }

            MemberAliasPropertyInfo aliasPropertyInfo = memberInfo as MemberAliasPropertyInfo;

            if (aliasPropertyInfo != null)
            {
                return(aliasPropertyInfo.AliasedProperty);
            }

            MemberAliasMethodInfo aliasMethodInfo = memberInfo as MemberAliasMethodInfo;

            if (aliasMethodInfo != null)
            {
                return(aliasMethodInfo.AliasedMethod);
            }

            if (throwOnNotAliased)
            {
                throw new ArgumentException("The member " + memberInfo.GetNiceName() + " was not aliased.");
            }

            return(memberInfo);
        }
        /// <summary>
        /// Returns the original, backing method of an alias method if the method is an alias.
        /// </summary>
        /// <param name="methodInfo">The method to check.</param>
        /// /// <param name="throwOnNotAliased">if set to <c>true</c> an exception will be thrown if the method is not aliased.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">The method was not aliased; this only occurs if throwOnNotAliased is true.</exception>
        public static MethodInfo DeAliasMethod(this MethodInfo methodInfo, bool throwOnNotAliased = false)
        {
            MemberAliasMethodInfo aliasMethodInfo = methodInfo as MemberAliasMethodInfo;

            if (aliasMethodInfo != null)
            {
                while (aliasMethodInfo.AliasedMethod is MemberAliasMethodInfo)
                {
                    aliasMethodInfo = aliasMethodInfo.AliasedMethod as MemberAliasMethodInfo;
                }

                return(aliasMethodInfo.AliasedMethod);
            }

            if (throwOnNotAliased)
            {
                throw new ArgumentException("The method " + methodInfo.GetNiceName() + " was not aliased.");
            }

            return(methodInfo);
        }