Exemple #1
0
        /// <summary>
        /// Cria a expressão de acesso para o membro.
        /// </summary>
        /// <param name="token"></param>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static Expression CreateMemberAccessExpression(this IMemberAccessToken token, Expression instance)
        {
            System.Reflection.MemberInfo memberInfoForType = token.GetMemberInfoForType(instance.Type);
            if (memberInfoForType == null)
            {
                throw new ArgumentException(FormatInvalidTokenErrorMessage(token, instance.Type));
            }
            IndexerToken indexerToken = token as IndexerToken;

            if (indexerToken != null)
            {
                IEnumerable <Expression> indexerArguments = indexerToken.GetIndexerArguments();
                return(Expression.Call(instance, (System.Reflection.MethodInfo)memberInfoForType, indexerArguments));
            }
            return(Expression.MakeMemberAccess(instance, memberInfoForType));
        }
Exemple #2
0
        /// <summary>
        /// Recupera as informações do membro para o tipo informado.
        /// </summary>
        /// <param name="token"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        private static MemberInfo GetMemberInfoForType(this IMemberAccessToken token, Type targetType)
        {
            PropertyToken token2 = token as PropertyToken;

            if (token2 != null)
            {
                return(GetMemberInfoFromPropertyToken(token2, targetType));
            }
            IndexerToken token3 = token as IndexerToken;

            if (token3 == null)
            {
                throw new InvalidOperationException(token.GetType().GetTypeName() + " is not supported");
            }
            return(GetMemberInfoFromIndexerToken(token3, targetType));
        }
        /// <exception cref="T:System.InvalidOperationException"><c>InvalidOperationException</c>.</exception>
        private static MemberInfo GetMemberInfoForType(IMemberAccessToken token, Type targetType)
        {
            var token1 = token as PropertyToken;

            if (token1 != null)
            {
                return(GetMemberInfoFromPropertyToken(token1, targetType));
            }
            var token2 = token as IndexerToken;

            if (token2 != null)
            {
                return(GetMemberInfoFromIndexerToken(token2, targetType));
            }
            throw new InvalidOperationException(TypeExtensions.GetTypeName(token.GetType()) + " is not supported");
        }
        /// <exception cref="T:System.ArgumentException">
        ///     Invalid name for property or field; or indexer with the specified
        ///     arguments.
        /// </exception>
        public static Expression CreateMemberAccessExpression(IMemberAccessToken token, Expression instance)
        {
            MemberInfo memberInfoForType = GetMemberInfoForType(token, instance.Type);

            if (memberInfoForType == null)
            {
                throw new ArgumentException(FormatInvalidTokenErrorMessage(token, instance.Type));
            }
            var indexerToken = token as IndexerToken;

            if (indexerToken == null)
            {
                return(Expression.MakeMemberAccess(instance, memberInfoForType));
            }
            IEnumerable <Expression> indexerArguments = GetIndexerArguments(indexerToken);

            return(Expression.Call(instance, (MethodInfo)memberInfoForType, indexerArguments));
        }
        /// <exception cref="ArgumentException">
        /// Invalid name for property or field; or indexer with the specified arguments.
        /// </exception>
        public static Expression CreateMemberAccessExpression(this IMemberAccessToken token, Expression instance)
        {
            var memberInfo = token.GetMemberInfoForType(instance.Type);

            if (memberInfo == null)
            {
                throw new ArgumentException(FormatInvalidTokenErrorMessage(token, instance.Type));
            }

            var indexerToken = token as IndexerToken;

            if (indexerToken != null)
            {
                var arguments = indexerToken.GetIndexerArguments();
                return(Expression.Call(instance, (MethodInfo)memberInfo, arguments));
            }

            // Property or field
            return(Expression.MakeMemberAccess(instance, memberInfo));
        }
        private static string FormatInvalidTokenErrorMessage(IMemberAccessToken token, Type type)
        {
            var    propertyToken = token as PropertyToken;
            string str1;
            string str2;

            if (propertyToken != null)
            {
                str1 = "property or field";
                str2 = propertyToken.PropertyName;
            }
            else
            {
                str1 = "indexer with arguments";
                str2 = string.Join(",",
                                   ((IndexerToken)token).Arguments.Where(a => a != null).Select(a => a.ToString()).ToArray());
            }
            return(string.Format(CultureInfo.CurrentCulture, "Invalid {0} - '{1}' for type: {2}",
                                 (object)str1, (object)str2, (object)type.GetTypeName()));
        }
        private static string FormatInvalidTokenErrorMessage(IMemberAccessToken token, Type type)
        {
            string memberName;
            string memberType;

            PropertyToken propertyToken = token as PropertyToken;
            if (propertyToken != null)
            {
                memberType = "property or field";
                memberName = propertyToken.PropertyName;
            }
            else
            {
                memberType = "indexer with arguments";

                var argumentsAsString = ((IndexerToken) token).Arguments.Where(a => a != null).Select(a => a.ToString());
                memberName = string.Join(",", argumentsAsString.ToArray());
            }

            return string.Format(CultureInfo.CurrentCulture, "Invalid {0} - '{1}' for type: {2}", memberType, memberName, type.GetTypeName());
        }
        private static string FormatInvalidTokenErrorMessage(IMemberAccessToken token, Type type)
        {
            string memberName;
            string memberType;

            PropertyToken propertyToken = token as PropertyToken;

            if (propertyToken != null)
            {
                memberType = "property or field";
                memberName = propertyToken.PropertyName;
            }
            else
            {
                memberType = "indexer with arguments";

                var argumentsAsString = ((IndexerToken)token).Arguments.Where(a => a != null).Select(a => a.ToString());
                memberName = string.Join(",", argumentsAsString.ToArray());
            }

            return(string.Format(CultureInfo.CurrentCulture, "Invalid {0} - '{1}' for type: {2}", memberType, memberName, type.GetTypeName()));
        }
Exemple #9
0
        /// <summary>
        /// Formata a mensagem de error do token inválido.
        /// </summary>
        /// <param name="token"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static string FormatInvalidTokenErrorMessage(IMemberAccessToken token, Type type)
        {
            string        propertyName;
            string        str2;
            PropertyToken token2 = token as PropertyToken;

            if (token2 != null)
            {
                str2         = "property or field";
                propertyName = token2.PropertyName;
            }
            else
            {
                str2 = "indexer with arguments";
                IEnumerable <string> source = from a in ((IndexerToken)token).Arguments
                                              where a != null
                                              select a.ToString();

                propertyName = string.Join(",", source.ToArray <string>());
            }
            return(string.Format("Invalid {0} - '{1}' for type: {2}", str2, propertyName, type.GetTypeName()));
        }