Exemple #1
0
            private static ViewDataInfo EvalComplexExpression(object indexableObject, string expression)
            {
                foreach (ExpressionPair expressionPair in GetRightToLeftExpressions(expression))
                {
                    string subExpression  = expressionPair.Left;
                    string postExpression = expressionPair.Right;

                    ViewDataInfo subTargetInfo = GetPropertyValue(indexableObject, subExpression);
                    if (subTargetInfo != null)
                    {
                        if (String.IsNullOrEmpty(postExpression))
                        {
                            return(subTargetInfo);
                        }

                        if (subTargetInfo.Value != null)
                        {
                            ViewDataInfo potential = EvalComplexExpression(subTargetInfo.Value, postExpression);
                            if (potential != null)
                            {
                                return(potential);
                            }
                        }
                    }
                }
                return(null);
            }
            private static ViewDataInfo GetPropertyValue(object container, string propertyName)
            {
                ViewDataInfo indexedPropertyValue = ViewDataDictionary.ViewDataEvaluator.GetIndexedPropertyValue(container, propertyName);

                if (indexedPropertyValue != null)
                {
                    return(indexedPropertyValue);
                }
                ViewDataDictionary viewDataDictionary = container as ViewDataDictionary;

                if (viewDataDictionary != null)
                {
                    container = viewDataDictionary.Model;
                }
                if (container == null)
                {
                    return((ViewDataInfo)null);
                }
                PropertyDescriptor descriptor = TypeDescriptor.GetProperties(container).Find(propertyName, true);

                if (descriptor == null)
                {
                    return((ViewDataInfo)null);
                }
                return(new ViewDataInfo((Func <object>)(() => descriptor.GetValue(container)))
                {
                    Container = container,
                    PropertyDescriptor = descriptor
                });
            }
 private static ViewDataInfo EvalComplexExpression(object indexableObject, string expression)
 {
     foreach (ViewDataDictionary.ViewDataEvaluator.ExpressionPair expressionPair in ViewDataDictionary.ViewDataEvaluator.GetRightToLeftExpressions(expression))
     {
         string       propertyName  = expressionPair.Left;
         string       expression1   = expressionPair.Right;
         ViewDataInfo propertyValue = ViewDataDictionary.ViewDataEvaluator.GetPropertyValue(indexableObject, propertyName);
         if (propertyValue != null)
         {
             if (string.IsNullOrEmpty(expression1))
             {
                 return(propertyValue);
             }
             if (propertyValue.Value != null)
             {
                 ViewDataInfo viewDataInfo = ViewDataDictionary.ViewDataEvaluator.EvalComplexExpression(propertyValue.Value, expression1);
                 if (viewDataInfo != null)
                 {
                     return(viewDataInfo);
                 }
             }
         }
     }
     return((ViewDataInfo)null);
 }
        internal static ModelMetadata FromStringExpression(string expression, ViewDataDictionary viewData, ModelMetadataProvider metadataProvider)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (viewData == null)
            {
                throw new ArgumentNullException("viewData");
            }
            if (expression.Length == 0)
            {
                // Empty string really means "model metadata for the current model"
                return(FromModel(viewData, metadataProvider));
            }

            ViewDataInfo  vdi           = viewData.GetViewDataInfo(expression);
            object        container     = null;
            Type          containerType = null;
            Type          modelType     = null;
            Func <object> modelAccessor = null;
            string        propertyName  = null;

            if (vdi != null)
            {
                if (vdi.Container != null)
                {
                    container     = vdi.Container;
                    containerType = vdi.Container.GetType();
                }

                modelAccessor = () => vdi.Value;

                if (vdi.PropertyDescriptor != null)
                {
                    propertyName = vdi.PropertyDescriptor.Name;
                    modelType    = vdi.PropertyDescriptor.PropertyType;
                }
                else if (vdi.Value != null)
                {
                    // We only need to delay accessing properties (for LINQ to SQL)
                    modelType = vdi.Value.GetType();
                }
            }
            else if (viewData.ModelMetadata != null)
            {
                //  Try getting a property from ModelMetadata if we couldn't find an answer in ViewData
                ModelMetadata propertyMetadata = viewData.ModelMetadata.Properties.Where(p => p.PropertyName == expression).FirstOrDefault();
                if (propertyMetadata != null)
                {
                    return(propertyMetadata);
                }
            }

            return(GetMetadataFromProvider(modelAccessor, modelType ?? typeof(string), propertyName, container, containerType, metadataProvider));
        }
        public object Eval(string expression)
        {
            ViewDataInfo viewDataInfo = this.GetViewDataInfo(expression);

            if (viewDataInfo == null)
            {
                return((object)null);
            }
            else
            {
                return(viewDataInfo.Value);
            }
        }
Exemple #6
0
            public static ViewDataInfo Eval(ViewDataDictionary vdd, string expression)
            {
                //Given an expression "foo.bar.baz" we look up the following (pseudocode):
                //  this["foo.bar.baz.quux"]
                //  this["foo.bar.baz"]["quux"]
                //  this["foo.bar"]["baz.quux]
                //  this["foo.bar"]["baz"]["quux"]
                //  this["foo"]["bar.baz.quux"]
                //  this["foo"]["bar.baz"]["quux"]
                //  this["foo"]["bar"]["baz.quux"]
                //  this["foo"]["bar"]["baz"]["quux"]

                ViewDataInfo evaluated = EvalComplexExpression(vdd, expression);

                return(evaluated);
            }
            private static ViewDataInfo GetPropertyValue(object container, string propertyName)
            {
                // This method handles one "segment" of a complex property expression

                // First, we try to evaluate the property based on its indexer
                ViewDataInfo value = GetIndexedPropertyValue(container, propertyName);

                if (value != null)
                {
                    return(value);
                }

                // If the indexer didn't return anything useful, continue...

                // If the container is a ViewDataDictionary then treat its Model property
                // as the container instead of the ViewDataDictionary itself.
                ViewDataDictionary vdd = container as ViewDataDictionary;

                if (vdd != null)
                {
                    container = vdd.Model;
                }

                // If the container is null, we're out of options
                if (container == null)
                {
                    return(null);
                }

                // Second, we try to use PropertyDescriptors and treat the expression as a property name
                PropertyDescriptor descriptor = TypeDescriptor
                                                .GetProperties(container)
                                                .Find(propertyName, true);

                if (descriptor == null)
                {
                    return(null);
                }

                return(new ViewDataInfo(() => descriptor.GetValue(container))
                {
                    Container = container,
                    PropertyDescriptor = descriptor
                });
            }
Exemple #8
0
        public object Eval(string expression)
        {
            ViewDataInfo info = GetViewDataInfo(expression);

            return((info != null) ? info.Value : null);
        }