public override bool TryGetMember(GetMemberBinder binder, out object result) { PropertyInfo propertyInfo = this.model.GetType().GetProperty(binder.Name); if (propertyInfo == null) { result = null; return(false); } result = propertyInfo.GetValue(this.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); }
public string RunSub(RazorEnginePlus razor, ref dynamic viewBag, object model = null) { if (model != null && model.IsAnonymous()) { model = new AnonymousTypeWrapper(model); } var instance = (RazorEngineTemplateBase)Activator.CreateInstance(templateType); #if !DEBUG if (instance == null) { return(""); } #endif instance.Html.Initialize(razor); instance.Model = model; instance.ViewBag = ViewBagCombine.Combine(instance.ViewBag, viewBag); instance.ExecuteAsync().Wait(); viewBag = ViewBagCombine.Combine(viewBag, instance.ViewBag); //获取instance.Layout必须在 instance.ExecuteAsync().Wait();之后否则无法取值 if (!string.IsNullOrEmpty(instance.Layout)) { return(razor.RenderRawSub(instance.Layout, ref instance.ViewBag, instance.Model)); } var result = instance.Result(); return(result); }
public override bool TryGetMember(GetMemberBinder binder, out object result) { PropertyInfo propertyInfo = this.model.GetType().GetProperty(binder.Name); if (propertyInfo == null) { result = null; return(false); } result = propertyInfo.GetValue(this.model, null); if (result == null) { return(true); } var type = result.GetType(); if (result.IsAnonymous()) { result = new AnonymousTypeWrapper(result); } if (result is IDictionary dictionary) { List <object> keys = new List <object>(); foreach (object key in dictionary.Keys) { keys.Add(key); } foreach (object key in keys) { if (dictionary[key].IsAnonymous()) { dictionary[key] = new AnonymousTypeWrapper(dictionary[key]); } } } else if (result is IEnumerable enumer && !(result is string)) { result = enumer.Cast <object>() .Select(e => { if (e.IsAnonymous()) { return(new AnonymousTypeWrapper(e)); } return(e); }) .ToList(); } return(true); }
public string Run(object model) { if (model.IsAnonymous()) { model = new AnonymousTypeWrapper(model); } RazorEngineTemplateBase instance = (RazorEngineTemplateBase)Activator.CreateInstance(this.templateType); instance.Model = model; instance.ExecuteAsync().Wait(); return(instance.Result()); }
public async Task <string> RunAsync(object model = null) { if (model != null && model.IsAnonymous()) { model = new AnonymousTypeWrapper(model); } IRazorEngineTemplate instance = (IRazorEngineTemplate)Activator.CreateInstance(this.templateType); instance.Model = model; await instance.ExecuteAsync(); return(await instance.ResultAsync()); }
public override bool TryGetMember(GetMemberBinder binder, out object result) { PropertyInfo propertyInfo = this.model.GetType().GetProperty(binder.Name); if (propertyInfo == null) { result = null; return(false); } result = propertyInfo.GetValue(this.model, null); if (result == null) { return(true); } var type = result.GetType(); if (result.IsAnonymous()) { result = new AnonymousTypeWrapper(result); } bool 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); }
public static async Task <string> RunAsync <TTemplate>( this IRazorEngineCompiledTemplate <TTemplate> compiledTemplate, object model = null) where TTemplate : class, IRazorEngineTemplate { if (model?.IsAnonymous() == true) { model = new AnonymousTypeWrapper(model); } var templateType = compiledTemplate.GetReflectedTemplateType(); IRazorEngineTemplate instance = (IRazorEngineTemplate)Activator.CreateInstance(templateType); instance.Model = model; await instance.ExecuteAsync(); return(instance.Result()); }