public static ModelMetadata FromStringExpression(string expression, TemplateData templateData, IModelMetadataProvider provider) { if (string.IsNullOrWhiteSpace(expression)) { return(templateData.ModelMetadata); } var templateDataInfo = TemplateDataEvaluator.Eval(templateData, expression); Type containerType = null; Type modelType = null; Func <object> modelAccessor = null; string propertyName = null; bool valid = (templateDataInfo != null); if (templateDataInfo != null) { if (templateDataInfo.Container != null) { containerType = templateDataInfo.Container.GetType(); } modelAccessor = () => templateDataInfo.Value; if (templateDataInfo.PropertyInfo != null) { propertyName = templateDataInfo.PropertyInfo.Name; modelType = templateDataInfo.PropertyInfo.PropertyType; } else if (templateDataInfo.Value != null) { modelType = templateDataInfo.Value.GetType(); } } else if (templateData.ModelMetadata != null) { var propertyMetadata = templateData.ModelMetadata.Properties.FirstOrDefault(p => p.PropertyName == expression); if (propertyMetadata != null) { return(propertyMetadata); } } var metadata = GetMetadataFromProvider(modelAccessor, modelType ?? typeof(string), propertyName, containerType, provider); if (!valid) { metadata.Valid = false; } return(metadata); }
public static ModelMetadata FromStringExpression(string expression, TemplateData templateData, IModelMetadataProvider provider) { if (string.IsNullOrWhiteSpace(expression)) { return templateData.ModelMetadata; } var templateDataInfo = TemplateDataEvaluator.Eval(templateData, expression); Type containerType = null; Type modelType = null; Func<object> modelAccessor = null; string propertyName = null; if (templateDataInfo != null) { if (templateDataInfo.Container != null) { containerType = templateDataInfo.Container.GetType(); } modelAccessor = () => templateDataInfo.Value; if (templateDataInfo.PropertyInfo != null) { propertyName = templateDataInfo.PropertyInfo.Name; modelType = templateDataInfo.PropertyInfo.PropertyType; } else if (templateDataInfo.Value != null) { modelType = templateDataInfo.Value.GetType(); } } else if (templateData.ModelMetadata != null) { var propertyMetadata = templateData.ModelMetadata.Properties.FirstOrDefault(p => p.PropertyName == expression); if (propertyMetadata != null) { return propertyMetadata; } } return GetMetadataFromProvider(modelAccessor, modelType ?? typeof(string), propertyName, containerType, provider); }
public static TemplateDataInfo Eval(TemplateData templateData, string expression) { return(EvalComplexExpression(templateData, expression)); }
public static TemplateDataInfo Eval(TemplateData templateData, string expression) { return EvalComplexExpression(templateData, expression); }
/// <summary> /// Resolves the value for the given expression. /// </summary> /// <param name="context">The render context.</param> /// <param name="templateData">The template data.</param> /// <param name="expression">The expression.</param> /// <param name="isVariableLookup">True if this is a variable lookup, otherwise false.</param> /// <returns>The resolved value.</returns> public static object ResolveValue(RenderContext context, TemplateData templateData, string expression, bool isVariableLookup) { if (isVariableLookup && !string.IsNullOrEmpty(expression) && !expression.StartsWith("root.")) { return context.GetVariable(expression); } if (isVariableLookup && !string.IsNullOrEmpty(expression) && expression.StartsWith("root.")) { context = context.RootRenderContext ?? context; templateData = context.TemplateData; expression = expression.Substring(5); } var modelMetadata = ExpressionMetadataProvider.FromStringExpression(expression, templateData, context.ModelMetadataProvider); if (modelMetadata == null) { return null; } return modelMetadata.Model; }