/// <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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the original, backing field of an alias field if the field is an alias.
        /// </summary>
        /// <param name="fieldInfo">The field to check.</param>
        /// /// <param name="throwOnNotAliased">if set to <c>true</c> an exception will be thrown if the field is not aliased.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">The field was not aliased; this only occurs if throwOnNotAliased is true.</exception>
        public static FieldInfo DeAliasField(this FieldInfo fieldInfo, bool throwOnNotAliased = false)
        {
            MemberAliasFieldInfo aliasFieldInfo = fieldInfo as MemberAliasFieldInfo;

            if (aliasFieldInfo != null)
            {
                while (aliasFieldInfo.AliasedField is MemberAliasFieldInfo)
                {
                    aliasFieldInfo = aliasFieldInfo.AliasedField as MemberAliasFieldInfo;
                }

                return(aliasFieldInfo.AliasedField);
            }

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

            return(fieldInfo);
        }