/// <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)); }
/// <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)); }