Пример #1
0
 public TypedObjectAccessor(Type targetType, MemberFilterDelegate filter, MemberRenamerDelegate renamer)
 {
     _type    = targetType ?? throw new ArgumentNullException(nameof(targetType));
     _filter  = filter;
     _renamer = renamer ?? StandardMemberRenamer.Default;
     _members = new Dictionary <string, MemberInfo>();
     PrepareMembers();
 }
Пример #2
0
        public static void Import(this IScriptObject script, object obj, MemberFilterDelegate filter = null, MemberRenamerDelegate renamer = null)
        {
            if (obj is IScriptObject)
            {
                // TODO: Add support for filter, member renamer
                script.Import((IScriptObject)obj);
                return;
            }

            if (obj is IDictionary)
            {
                // TODO: Add support for filter, member renamer
                script.ImportDictionary((IDictionary)obj);
                return;
            }

            script.Import(obj, ScriptMemberImportFlags.All, filter, renamer);
        }
Пример #3
0
        public string Render(object model = null, MemberRenamerDelegate memberRenamer = null, MemberFilterDelegate memberFilter = null)
        {
            var scriptObject = new ScriptObject();

            if (model != null)
            {
                scriptObject.Import(model, renamer: memberRenamer, filter: memberFilter);
            }

            var context = LexerOptions.Lang == ScriptLang.Liquid ? new LiquidTemplateContext() : new TemplateContext();

            context.MemberRenamer = memberRenamer;
            context.MemberFilter  = memberFilter;
            context.PushGlobal(scriptObject);
            return(Render(context));
        }
Пример #4
0
        public object Evaluate(object model = null, MemberRenamerDelegate memberRenamer = null, MemberFilterDelegate memberFilter = null)
        {
            var scriptObject = new ScriptObject();

            if (model != null)
            {
                scriptObject.Import(model, renamer: memberRenamer, filter: memberFilter);
            }

            var context = LexerOptions.Lang == ScriptLang.Liquid ? new LiquidTemplateContext() : new TemplateContext();

            context.EnableOutput  = false;
            context.MemberRenamer = memberRenamer;
            context.MemberFilter  = memberFilter;
            context.UseScientific = LexerOptions.Lang == ScriptLang.Scientific;
            context.PushGlobal(scriptObject);
            var result = Evaluate(context);

            context.PopGlobal();
            return(result);
        }
Пример #5
0
        public static object Evaluate(string expression, object model, MemberRenamerDelegate memberRenamer = null, MemberFilterDelegate memberFilter = null)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            var lexerOption = new LexerOptions()
            {
                Mode = ScriptMode.ScriptOnly
            };
            var template = Parse(expression, lexerOptions: lexerOption);

            return(template.Evaluate(model, memberRenamer, memberFilter));
        }
Пример #6
0
        public static void Import(this IScriptObject script, object obj, ScriptMemberImportFlags flags, MemberFilterDelegate filter = null, MemberRenamerDelegate renamer = null)
        {
            if (obj == null)
            {
                return;
            }
            if (!ScriptObject.IsImportable(obj))
            {
                throw new ArgumentOutOfRangeException(nameof(obj), $"Unsupported object type `{obj.GetType()}`. Expecting plain class or struct");
            }

            var  typeInfo    = (obj as Type ?? obj.GetType());
            bool useStatic   = false;
            bool useInstance = false;

            if (obj is Type)
            {
                useStatic = true;
                obj       = null;
            }
            else
            {
                useInstance = true;
            }

            renamer = renamer ?? StandardMemberRenamer.Default;

            var typeToImports = new Stack <Type>();

            while (typeInfo != null)
            {
                typeToImports.Push(typeInfo);
                if (typeInfo.BaseType == typeof(object))
                {
                    break;
                }

                typeInfo = typeInfo.BaseType;
            }

            var scriptObj = script as ScriptObject;

            while (typeToImports.Count > 0)
            {
                typeInfo = typeToImports.Pop();

                if ((flags & ScriptMemberImportFlags.Field) != 0)
                {
                    foreach (var field in typeInfo.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly))
                    {
                        if (!field.IsPublic || field.IsLiteral)
                        {
                            continue;
                        }
                        if (filter != null && !filter(field))
                        {
                            continue;
                        }

                        var keep = field.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                        if (keep && ((field.IsStatic && useStatic) || useInstance))
                        {
                            var newFieldName = renamer(field);
                            if (String.IsNullOrEmpty(newFieldName))
                            {
                                newFieldName = field.Name;
                            }

                            // If field is init only or literal, it cannot be set back so we mark it as read-only
                            if (scriptObj == null)
                            {
                                script.TrySetValue(null, new SourceSpan(), newFieldName, field.GetValue(obj), field.IsInitOnly || field.IsLiteral);
                            }
                            else
                            {
                                scriptObj.SetValue(newFieldName, field.GetValue(obj), field.IsInitOnly || field.IsLiteral);
                            }
                        }
                    }
                }

                if ((flags & ScriptMemberImportFlags.Property) != 0)
                {
                    foreach (var property in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly))
                    {
                        // Workaround with .NET Core, extension method is not working (retuning null despite doing property.GetMethod), so we need to inline it here
                        var getMethod = property.GetMethod;
                        if (!property.CanRead || !getMethod.IsPublic)
                        {
                            continue;
                        }

                        if (filter != null && !filter(property))
                        {
                            continue;
                        }

                        var keep = property.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                        if (keep && (((getMethod.IsStatic && useStatic) || useInstance)))
                        {
                            var newPropertyName = renamer(property);
                            if (String.IsNullOrEmpty(newPropertyName))
                            {
                                newPropertyName = property.Name;
                            }

                            // Initially, we were setting readonly depending on the precense of a set method, but this is not compatible with liquid implems, so we remove readonly restriction
                            //script.SetValue(null, new SourceSpan(), newPropertyName, property.GetValue(obj), property.GetSetMethod() == null || !property.GetSetMethod().IsPublic);
                            if (scriptObj == null)
                            {
                                script.TrySetValue(null, new SourceSpan(), newPropertyName, property.GetValue(obj), false);
                            }
                            else
                            {
                                if (property.GetIndexParameters().Length == 0)
                                {
                                    scriptObj.SetValue(newPropertyName, property.GetValue(obj), false);
                                }
                            }
                        }
                    }
                }

                if ((flags & ScriptMemberImportFlags.Method) != 0 && useStatic)
                {
                    foreach (var method in typeInfo.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly))
                    {
                        if (filter != null && !filter(method))
                        {
                            continue;
                        }

                        var keep = method.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                        if (keep && method.IsPublic && method.IsStatic && !method.IsSpecialName)
                        {
                            var newMethodName = renamer(method);
                            if (String.IsNullOrEmpty(newMethodName))
                            {
                                newMethodName = method.Name;
                            }

                            if (scriptObj == null)
                            {
                                script.TrySetValue(null, new SourceSpan(), newMethodName, DynamicCustomFunction.Create(obj, method), true);
                            }
                            else
                            {
                                scriptObj.SetValue(newMethodName, DynamicCustomFunction.Create(obj, method), true);
                            }
                        }
                    }
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Evaluates the template using the specified context
        /// </summary>
        /// <param name="model">An object model to use with the evaluation.</param>
        /// <param name="memberRenamer">The member renamer used to import this .NET object and transitive objects. See member renamer documentation for more details.</param>
        /// <param name="memberFilter">The member filter used to filter members for .NET objects being accessed through the template, including the model being passed to this method.</param>
        /// <exception cref="System.InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        /// <returns>Returns the result of the last statement</returns>
        public object Evaluate(object model = null, MemberRenamerDelegate memberRenamer = null, MemberFilterDelegate memberFilter = null)
        {
            var scriptObject = new ScriptObject();

            if (model != null)
            {
                scriptObject.Import(model, renamer: memberRenamer, filter: memberFilter);
            }

            var context = new TemplateContext
            {
                EnableOutput  = false,
                MemberRenamer = memberRenamer,
                MemberFilter  = memberFilter
            };

            context.PushGlobal(scriptObject);
            var result = Evaluate(context);

            context.PopGlobal();
            return(result);
        }
Пример #8
0
        /// <summary>
        /// Imports the specified object.
        /// </summary>
        /// <param name="script">The script object to import into</param>
        /// <param name="obj">The object.</param>
        /// <param name="flags">The import flags.</param>
        /// <param name="filter">A filter applied on each member</param>
        /// <param name="renamer">The member renamer.</param>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public static void Import(this IScriptObject script, object obj, ScriptMemberImportFlags flags, MemberFilterDelegate filter = null, MemberRenamerDelegate renamer = null)
        {
            if (obj == null)
            {
                return;
            }
            if (!ScriptObject.IsImportable(obj))
            {
                throw new ArgumentOutOfRangeException(nameof(obj), $"Unsupported object type `{obj.GetType()}`. Expecting plain class or struct");
            }

            var  typeInfo          = (obj as Type ?? obj.GetType()).GetTypeInfo();
            bool useStatic         = false;
            bool useInstance       = false;
            bool useMethodInstance = false;

            if (obj is Type)
            {
                useStatic = true;
                obj       = null;
            }
            else
            {
                useInstance       = true;
                useMethodInstance = (flags & ScriptMemberImportFlags.MethodInstance) != 0;
            }

            renamer = renamer ?? StandardMemberRenamer.Default;

            if ((flags & ScriptMemberImportFlags.Field) != 0)
            {
                foreach (var field in typeInfo.GetDeclaredFields())
                {
                    if (!field.IsPublic)
                    {
                        continue;
                    }
                    if (filter != null && !filter(field))
                    {
                        continue;
                    }

                    var keep = field.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                    if (keep && ((field.IsStatic && useStatic) || useInstance))
                    {
                        var newFieldName = renamer(field);
                        if (String.IsNullOrEmpty(newFieldName))
                        {
                            newFieldName = field.Name;
                        }

                        // If field is init only or literal, it cannot be set back so we mark it as read-only
                        script.SetValue(null, new SourceSpan(), newFieldName, field.GetValue(obj), field.IsInitOnly || field.IsLiteral);
                    }
                }
            }

            if ((flags & ScriptMemberImportFlags.Property) != 0)
            {
                foreach (var property in typeInfo.GetDeclaredProperties())
                {
                    if (!property.CanRead || !property.GetGetMethod().IsPublic)
                    {
                        continue;
                    }

                    if (filter != null && !filter(property))
                    {
                        continue;
                    }

                    var keep = property.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                    if (keep && (((property.GetGetMethod().IsStatic&& useStatic) || useInstance)))
                    {
                        var newPropertyName = renamer(property);
                        if (String.IsNullOrEmpty(newPropertyName))
                        {
                            newPropertyName = property.Name;
                        }

                        // Initially, we were setting readonly depending on the precense of a set method, but this is not compatible with liquid implems, so we remove readonly restriction
                        //script.SetValue(null, new SourceSpan(), newPropertyName, property.GetValue(obj), property.GetSetMethod() == null || !property.GetSetMethod().IsPublic);
                        script.SetValue(null, new SourceSpan(), newPropertyName, property.GetValue(obj), false);
                    }
                }
            }

            if ((flags & ScriptMemberImportFlags.Method) != 0 && (useStatic || useMethodInstance))
            {
                foreach (var method in typeInfo.GetDeclaredMethods())
                {
                    if (filter != null && !filter(method))
                    {
                        continue;
                    }

                    var keep = method.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                    if (keep && method.IsPublic && ((useMethodInstance && !method.IsStatic) || (useStatic && method.IsStatic)) && !method.IsSpecialName)
                    {
                        var newMethodName = renamer(method);
                        if (String.IsNullOrEmpty(newMethodName))
                        {
                            newMethodName = method.Name;
                        }

                        script.SetValue(null, new SourceSpan(), newMethodName, DynamicCustomFunction.Create(obj, method), true);
                    }
                }
            }
        }
Пример #9
0
 public TypedObjectAccessor(Type targetType, MemberFilterDelegate filter, MemberRenamerDelegate renamer) : this(targetType, null, filter, renamer)
 {
 }
Пример #10
0
        /// <summary>
        /// Imports the specified object.
        /// </summary>
        /// <param name="scriptObject">The script object to import into</param>
        /// <param name="obj">The object.</param>
        /// <param name="flags">The import flags.</param>
        /// <param name="filter">A filter applied on each member</param>
        /// <param name="renamer">The member renamer.</param>
        /// <exception cref="ArgumentOutOfRangeException">The object is not importable.</exception>
        public static void Import(this IScriptObject scriptObject, object obj, ScriptMemberImportFlags flags, MemberFilterDelegate filter = null, MemberRenamerDelegate renamer = null)
        {
            if (obj == null)
            {
                return;
            }

            if (!ScriptObject.IsImportable(obj))
            {
                throw new ArgumentOutOfRangeException(nameof(obj), string.Format(RS.InvalidObjectType, obj.GetType()));
            }

#if NETSTANDARD
            TypeInfo typeInfo = (obj as Type ?? obj.GetType()).GetTypeInfo();
#else
            Type typeInfo = (obj as Type ?? obj.GetType()).GetTypeInfo();
#endif

            bool useStatic   = false;
            bool useInstance = false;
            if (obj is Type)
            {
                useStatic = true;
                obj       = null;
            }
            else
            {
                useInstance = true;
            }

            renamer = renamer ?? StandardMemberRenamer.Default;

            while (typeInfo != null)
            {
                if ((flags & ScriptMemberImportFlags.Field) != 0)
                {
                    foreach (FieldInfo field in typeInfo.GetDeclaredFields())
                    {
                        if (!field.IsPublic)
                        {
                            continue;
                        }
                        if (filter != null && !filter(field))
                        {
                            continue;
                        }

                        bool keep = field.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                        if (keep && ((field.IsStatic && useStatic) || useInstance))
                        {
                            string newFieldName = renamer(field);
                            if (string.IsNullOrEmpty(newFieldName))
                            {
                                newFieldName = field.Name;
                            }

                            // If field is init only or literal, it cannot be set back so we mark it as read-only
                            scriptObject.SetValue(null, new SourceSpan(), newFieldName, field.GetValue(obj), field.IsInitOnly || field.IsLiteral);
                        }
                    }
                }

                if ((flags & ScriptMemberImportFlags.Property) != 0)
                {
                    foreach (PropertyInfo property in typeInfo.GetDeclaredProperties())
                    {
                        // Workaround with .NET Core, extension method is not working (retuning null)
#if NETFX
                        MethodInfo getMethod = property.GetGetMethod();
#else
                        MethodInfo getMethod = property.GetMethod;
#endif

                        if (!property.CanRead || !getMethod.IsPublic)
                        {
                            continue;
                        }

                        if (filter != null && !filter(property))
                        {
                            continue;
                        }

                        bool keep = property.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                        if (keep && ((getMethod.IsStatic && useStatic) || useInstance))
                        {
                            string newPropertyName = renamer(property);
                            if (string.IsNullOrEmpty(newPropertyName))
                            {
                                newPropertyName = property.Name;
                            }

                            // Initially, we were setting readonly depending on the precense of a set method, but this is not compatible with liquid implems, so we remove readonly restriction
                            //script.SetValue(null, new SourceSpan(), newPropertyName, property.GetValue(obj), property.GetSetMethod() == null || !property.GetSetMethod().IsPublic);
                            scriptObject.SetValue(null, new SourceSpan(), newPropertyName, property.GetValue(obj), false);
                        }
                    }
                }

                if ((flags & ScriptMemberImportFlags.Method) != 0 && useStatic)
                {
                    foreach (MethodInfo method in typeInfo.GetDeclaredMethods())
                    {
                        if (filter != null && !filter(method))
                        {
                            continue;
                        }

                        bool keep = method.GetCustomAttribute <ScriptMemberIgnoreAttribute>() == null;
                        if (keep && method.IsPublic && method.IsStatic && !method.IsSpecialName)
                        {
                            string newMethodName = renamer(method);
                            if (string.IsNullOrEmpty(newMethodName))
                            {
                                newMethodName = method.Name;
                            }

                            scriptObject.SetValue(null, new SourceSpan(), newMethodName, DynamicCustomFunction.Create(obj, method), true);
                        }
                    }
                }

                if (typeInfo.BaseType == typeof(object))
                {
                    break;
                }

                typeInfo = typeInfo.BaseType.GetTypeInfo();
            }
        }