예제 #1
0
        /// <summary>
        /// 获取成员信息
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var propertyInfo = model.GetType().GetProperty(binder.Name);

            if (propertyInfo == null)
            {
                result = null;
                return(false);
            }

            result = propertyInfo.GetValue(model, null);

            if (result == null)
            {
                return(true);
            }

            var type = result.GetType();

            if (result.IsAnonymous())
            {
                result = new AnonymousTypeWrapper(result);
            }

            if (type.IsArray)
            {
                result = ((IEnumerable <object>)result).Select(e => new AnonymousTypeWrapper(e)).ToList();
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// 执行编译
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <string> RunAsync(object model = null)
        {
            if (model != null && model.IsAnonymous())
            {
                model = new AnonymousTypeWrapper(model);
            }

            var instance = (IViewEngineModel)Activator.CreateInstance(templateType);

            instance.Model = model;

            await instance.ExecuteAsync();

            return(await instance.ResultAsync());
        }
예제 #3
0
        /// <summary>
        /// 获取成员信息
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var propertyInfo = model.GetType().GetProperty(binder.Name);

            if (propertyInfo == null)
            {
                result = null;
                return(false);
            }

            result = propertyInfo.GetValue(model, null);

            if (result == null)
            {
                return(true);
            }

            var type = result.GetType();

            if (result.IsAnonymous())
            {
                result = new AnonymousTypeWrapper(result);
            }

            var isEnumerable = typeof(IEnumerable).IsAssignableFrom(type);

            if (isEnumerable && !(result is string))
            {
                result = ((IEnumerable <object>)result)
                         .Select(e =>
                {
                    if (e.IsAnonymous())
                    {
                        return(new AnonymousTypeWrapper(e));
                    }

                    return(e);
                })
                         .ToList();
            }

            return(true);
        }