/// <summary>
        /// Gets the member accessor for the specified object if not already cached. This method can have an override.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        protected virtual IObjectAccessor GetMemberAccessorImpl(object target)
        {
            var             type = target.GetType();
            IObjectAccessor accessor;

            if (target is IScriptObject)
            {
                accessor = ScriptObjectAccessor.Default;
            }
            else if (target is string)
            {
                accessor = StringAccessor.Default;
            }
            else if (type.IsPrimitiveOrDecimal())
            {
                accessor = PrimitiveAccessor.Default;
            }
            else if (DictionaryAccessor.TryGet(target, out accessor))
            {
            }
            else if (type.IsArray)
            {
                accessor = ArrayAccessor.Default;
            }
            else if (target is IList)
            {
                accessor = ListAccessor.Default;
            }
            else
            {
                accessor = new TypedObjectAccessor(type, MemberFilter, MemberRenamer);
            }
            return(accessor);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the member accessor for the specified object if not already cached. This method can have an override.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        protected virtual IObjectAccessor GetMemberAccessorImpl(object target)
        {
            Type            type = target.GetType();
            IObjectAccessor accessor;

            if (target is IScriptObject)
            {
                accessor = ScriptObjectAccessor.Default;
            }
            else if (DictionaryAccessor.TryGet(type, out accessor))
            {
                // do nothing
            }
            else if (type.GetTypeInfo().IsArray)
            {
                accessor = ArrayAccessor.Default;
            }
            else if (target is IList)
            {
                accessor = ListAccessor.Default;
            }
            else
            {
                accessor = new TypedObjectAccessor(type, MemberFilter, MemberRenamer);
            }

            return(accessor);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the member accessor for the specified object if not already cached. This method can have an override.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        protected virtual IObjectAccessor GetMemberAccessorImpl(object target)
        {
            var             type = target.GetType();
            IObjectAccessor accessor;

            if (target is IScriptObject)
            {
                accessor = ScriptObjectAccessor.Default;
            }
            else if (!DictionaryAccessor.TryGet(type, out accessor))
            {
                accessor = new TypedObjectAccessor(type, MemberRenamer);
            }
            return(accessor);
        }
        protected override IObjectAccessor GetMemberAccessorImpl(object target)
        {
            // Duplicated from Scriban.TemplateContext#GetMemberAccessorImpl

            var             type = target.GetType();
            IObjectAccessor accessor;

            if (target is IScriptObject)
            {
                accessor = ScriptObjectAccessor.Default;
            }
            else if (target is string)
            {
                accessor = StringAccessor.Default;
            }
            else if (type.IsPrimitiveOrDecimal())
            {
                throw new NotImplementedException("member access of a primitive or decimal");
                //accessor = PrimitiveAccessor.Default;
            }
            else if (DictionaryAccessor.TryGet(target, out accessor))
            {
            }
            else if (type.IsArray)
            {
                accessor = ArrayAccessor.Default;
            }
            else if (target is IList)
            {
                accessor = ListAccessor.Default;
            }
            else
            {
                accessor = new NScribeTypedObjectAccessor(type, MemberFilter, MemberRenamer);
            }

            return(accessor);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the member accessor for the specified object.
        /// </summary>
        /// <param name="target">The target object to get a member accessor.</param>
        /// <returns>A member accessor</returns>
        public IMemberAccessor GetMemberAccessor(object target)
        {
            if (target == null)
            {
                return(NullAccessor.Default);
            }

            var             type = target.GetType();
            IMemberAccessor accessor;

            if (!memberAccessors.TryGetValue(type, out accessor))
            {
                if (target is IScriptObject)
                {
                    accessor = ScriptObjectExtensions.Accessor;
                }
                else if (!DictionaryAccessor.TryGet(type, out accessor))
                {
                    accessor = new TypedMemberAccessor(type, MemberRenamer);
                }
                memberAccessors.Add(type, accessor);
            }
            return(accessor);
        }