Ejemplo n.º 1
0
        /// <summary>
        /// Resolves member accesses that represent transparent scope property accesses to the corresponding resource set,
        /// iff the input resource set is enclosed in a transparent scope and the specified MemberExpression represents
        /// such a property access.
        /// </summary>
        /// <param name="m">MemberExpression expression to visit</param>
        /// <returns>
        /// An InputReferenceExpression if the member access represents a transparent scope property
        /// access that can be resolved to a resource set in the path that produces the input resource set;
        /// otherwise the same MemberExpression is returned.
        /// </returns>
        internal override Expression VisitMemberAccess(MemberExpression m)
        {
            // If the current input resource set is not enclosed in a transparent scope, then this
            // MemberExpression cannot represent a valid transparent scope access based on the input parameter.
            if (this.inputSet == null ||
                !this.inputSet.HasTransparentScope)
            {
                return(base.VisitMemberAccess(m));
            }

            ParameterExpression  innerParamRef  = null;
            Stack <PropertyInfo> nestedAccesses = new Stack <PropertyInfo>();
            MemberExpression     memberRef      = m;

            while (memberRef != null &&
                   PlatformHelper.IsProperty(memberRef.Member) &&
                   memberRef.Expression != null)
            {
                nestedAccesses.Push((PropertyInfo)memberRef.Member);

                if (memberRef.Expression.NodeType == ExpressionType.Parameter)
                {
                    innerParamRef = (ParameterExpression)memberRef.Expression;
                }

                memberRef = memberRef.Expression as MemberExpression;
            }

            // Only continue if the inner non-MemberExpression is the input reference ParameterExpression and
            // at least one property reference is present - otherwise this cannot be a transparent scope access.
            if (innerParamRef != this.inputParameter || nestedAccesses.Count == 0)
            {
                return(m);
            }

            ResourceExpression    target    = this.input;
            ResourceSetExpression targetSet = this.inputSet;
            bool transparentScopeTraversed  = false;

            // Process all the traversals through transparent scopes.
            while (nestedAccesses.Count > 0)
            {
                if (targetSet == null || !targetSet.HasTransparentScope)
                {
                    break;
                }

                // Peek the property; pop it once it's consumed
                // (it could be a non-transparent-identifier access).
                PropertyInfo currentProp = nestedAccesses.Peek();

                // If this is the accessor for the target, then the member
                // refers to the target itself.
                if (currentProp.Name.Equals(targetSet.TransparentScope.Accessor, StringComparison.Ordinal))
                {
                    target = targetSet;
                    nestedAccesses.Pop();
                    transparentScopeTraversed = true;
                    continue;
                }

                // This member could also be one of the in-scope sources of the target.
                Expression source;
                if (!targetSet.TransparentScope.SourceAccessors.TryGetValue(currentProp.Name, out source))
                {
                    break;
                }

                transparentScopeTraversed = true;
                nestedAccesses.Pop();
                Debug.Assert(source != null, "source != null -- otherwise ResourceBinder created an accessor to nowhere");
                InputReferenceExpression sourceReference = source as InputReferenceExpression;
                if (sourceReference == null)
                {
                    targetSet = source as ResourceSetExpression;
                    if (targetSet == null || !targetSet.HasTransparentScope)
                    {
                        target = (ResourceExpression)source;
                    }
                }
                else
                {
                    targetSet = sourceReference.Target as ResourceSetExpression;
                    target    = targetSet;
                }
            }

            // If no traversals were made, the original expression is OK.
            if (!transparentScopeTraversed)
            {
                return(m);
            }

            // Process traversals after the transparent scope.
            Expression result = this.CreateReference(target);

            while (nestedAccesses.Count > 0)
            {
                result = Expression.Property(result, nestedAccesses.Pop());
            }

            return(result);
        }
Ejemplo n.º 2
0
        internal override Expression VisitMemberAccess(MemberExpression m)
        {
            if ((this.inputSet == null) || !this.inputSet.HasTransparentScope)
            {
                return(base.VisitMemberAccess(m));
            }
            ParameterExpression  expression = null;
            Stack <PropertyInfo> stack      = new Stack <PropertyInfo>();

            for (MemberExpression expression2 = m; ((expression2 != null) && PlatformHelper.IsProperty(expression2.Member)) && (expression2.Expression != null); expression2 = expression2.Expression as MemberExpression)
            {
                stack.Push((PropertyInfo)expression2.Member);
                if (expression2.Expression.NodeType == ExpressionType.Parameter)
                {
                    expression = (ParameterExpression)expression2.Expression;
                }
            }
            if ((expression != this.inputParameter) || (stack.Count == 0))
            {
                return(m);
            }
            ResourceExpression    input    = this.input;
            ResourceSetExpression inputSet = this.inputSet;
            bool flag = false;

            while (stack.Count > 0)
            {
                if ((inputSet == null) || !inputSet.HasTransparentScope)
                {
                    break;
                }
                PropertyInfo info = stack.Peek();
                if (info.Name.Equals(inputSet.TransparentScope.Accessor, StringComparison.Ordinal))
                {
                    input = inputSet;
                    stack.Pop();
                    flag = true;
                }
                else
                {
                    Expression expression5;
                    if (!inputSet.TransparentScope.SourceAccessors.TryGetValue(info.Name, out expression5))
                    {
                        break;
                    }
                    flag = true;
                    stack.Pop();
                    InputReferenceExpression expression6 = expression5 as InputReferenceExpression;
                    if (expression6 == null)
                    {
                        inputSet = expression5 as ResourceSetExpression;
                        if ((inputSet == null) || !inputSet.HasTransparentScope)
                        {
                            input = (ResourceExpression)expression5;
                        }
                        continue;
                    }
                    inputSet = expression6.Target as ResourceSetExpression;
                    input    = inputSet;
                }
            }
            if (!flag)
            {
                return(m);
            }
            Expression expression7 = this.CreateReference(input);

            while (stack.Count > 0)
            {
                expression7 = Expression.Property(expression7, stack.Pop());
            }
            return(expression7);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Create an instance of primitive type from a string representation
 /// </summary>
 /// <param name="text">The string representation</param>
 /// <returns>An instance of primitive type</returns>
 internal override object Parse(String text)
 {
     return(PlatformHelper.ConvertStringToDateTimeOffset(text));
 }
Ejemplo n.º 4
0
 internal override object Parse(string text)
 {
     return(PlatformHelper.GetTypeOrThrow(text));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Convert an instance of primitive type to string
 /// </summary>
 /// <param name="instance">The instance</param>
 /// <returns>The string representation of the instance</returns>
 internal override string ToString(object instance)
 {
     return(PlatformHelper.ConvertDateTimeToString((DateTime)instance));
 }