Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of <see cref="TemplateContext"/> wih a model and option regiter its properties.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="registerModelProperties">Whether to register the model properties or not.</param>
        public TemplateContext(object model, bool registerModelProperties = true)
        {
            Model = model ?? throw new ArgumentNullException(nameof(model));

            if (registerModelProperties)
            {
                MemberAccessStrategy.Register(model.GetType());
            }

            LocalScope = new Scope(GlobalScope);
        }
        /// <summary>
        /// Registers a limited set of properties in a type.
        /// </summary>
        /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
        /// <param name="type">The type to register.</param>
        /// <param name="names">The names of the properties in the type to register.</param>
        public static void Register(this MemberAccessStrategy strategy, Type type, params string[] names)
        {
            var accessors = new Dictionary <string, IMemberAccessor>();

            foreach (var name in names)
            {
                accessors[name] = GetNamedAccessor(type, name, strategy.MemberNameStrategy);
            }

            strategy.Register(type, accessors);
        }
 /// <summary>
 /// Registers a type and all its public properties.
 /// </summary>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="type">The type to register.</param>
 public static void Register(this MemberAccessStrategy strategy, Type type)
 {
     strategy.Register(type, GetTypeMembers(type, strategy.MemberNameStrategy));
 }
 /// <summary>
 /// Registers a type and all its public properties.
 /// </summary>
 /// <typeparam name="T">The type to register.</typeparam>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 public static void Register <T>(this MemberAccessStrategy strategy)
 {
     Register(strategy, typeof(T));
 }
 /// <summary>
 /// Registers a type with a <see cref="Func{T, TemplateContext, TResult}"/> to retrieve the property specified.
 /// </summary>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="name">The name of the property.</param>
 /// <param name="accessor">The <see cref="Func{T, TemplateContext, TResult}"/> instance used to retrieve the value.</param>
 public static void Register <T, TResult>(this MemberAccessStrategy strategy, string name, Func <T, TemplateContext, TResult> accessor)
 {
     strategy.Register(typeof(T), new[] { new KeyValuePair <string, IMemberAccessor>(name, new DelegateAccessor <T, TResult>((obj, propertyName, ctx) => accessor(obj, ctx))) });
 }
 /// <summary>
 /// Registers a type with a <see cref="Func{T, TResult}"/> to retrieve the property specified.
 /// </summary>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="name">The name of the property.</param>
 /// <param name="accessor">The <see cref="Func{T, TResult}"/> instance used to retrieve the value.</param>
 public static void Register <T, TResult>(this MemberAccessStrategy strategy, string name, Func <T, TResult> accessor)
 {
     Register <T, TResult>(strategy, name, (obj, ctx) => accessor(obj));
 }
 /// <summary>
 /// Registers a type with a <see cref="T:Func{T, string, TemplateContext, Task{TResult}}"/> to retrieve any of
 /// its property values.
 /// </summary>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="accessor">The <see cref="T:Func{T, string, TemplateContext, Task{TResult}}"/> instance used to retrieve the value.</param>
 public static void Register <T, TResult>(this MemberAccessStrategy strategy, Func <T, string, TemplateContext, Task <TResult> > accessor)
 {
     strategy.Register(typeof(T), new[] { new KeyValuePair <string, IMemberAccessor>("*", new AsyncDelegateAccessor <T, TResult>(accessor)) });
 }
 /// <summary>
 /// Registers a type with a <see cref="T:Func{T, string, Task{TResult}}"/> to retrieve any of
 /// its property values.
 /// </summary>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="accessor">The <see cref="T:Func{T, string, Task{Object}}"/> instance used to retrieve the value.</param>
 public static void Register <T, TResult>(this MemberAccessStrategy strategy, Func <T, string, Task <TResult> > accessor)
 {
     Register <T, TResult>(strategy, (obj, name, ctx) => accessor(obj, name));
 }
 /// <summary>
 /// Registers a type using a <see cref="IMemberAccessor"/> to retrieve any of
 /// its property values.
 /// </summary>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="type">The type to register.</param>
 /// <param name="getter">The <see cref="IMemberAccessor"/> instance used to retrieve the value.</param>
 public static void Register(this MemberAccessStrategy strategy, Type type, IMemberAccessor getter)
 {
     strategy.Register(type, new[] { new KeyValuePair <string, IMemberAccessor>("*", getter) });
 }
 /// <summary>
 /// Registers a named property when accessing a type using a <see cref="IMemberAccessor"/>
 /// to retrieve the value. The name of the property doesn't have to exist on the object.
 /// </summary>
 /// <typeparam name="T">The type to register.</typeparam>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="name">The name of the property to intercept.</param>
 /// <param name="getter">The <see cref="IMemberAccessor"/> instance used to retrieve the value.</param>
 public static void Register <T>(this MemberAccessStrategy strategy, string name, IMemberAccessor getter)
 {
     strategy.Register(typeof(T), new[] { new KeyValuePair <string, IMemberAccessor>(name, getter) });
 }
 /// <summary>
 /// Registers a limited set of properties in a type.
 /// </summary>
 /// <typeparam name="T">The type to register.</typeparam>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="names">The property's expressions in the type to register.</param>
 public static void Register <T>(this MemberAccessStrategy strategy, params Expression <Func <T, object> >[] names)
 {
     strategy.Register <T>(names.Select(ExpressionHelper.GetPropertyName).ToArray());
 }
 /// <summary>
 /// Registers a limited set of properties in a type.
 /// </summary>
 /// <typeparam name="T">The type to register.</typeparam>
 /// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
 /// <param name="names">The names of the properties in the type to register.</param>
 public static void Register <T>(this MemberAccessStrategy strategy, params string[] names)
 {
     strategy.Register(typeof(T), names);
 }