/// <summary> /// Checks if the given model-type has a reference to an anonymous type and throws. /// </summary> /// <param name="modelType">the type to check</param> internal static void CheckModelType(Type modelType) { if (modelType == null) { return; } if (CompilerServicesUtility.IsAnonymousTypeRecursive(modelType)) { throw new ArgumentException(@"We cannot support anonymous model types as those are internal! However you can just use 'dynamic' (modelType == null) and we try to make it work for you (at the cost of performance)."); } }
private Type CheckModelType(Type modelType) { if (modelType == null) { return(null); } if (CompilerServicesUtility.IsAnonymousTypeRecursive(modelType)) { //throw new ArgumentException("Cannot use anonymous type as model type."); modelType = null; } if (modelType != null && CompilerServicesUtility.IsDynamicType(modelType)) { modelType = null; } return(modelType); }
/// <summary> /// Checks if we need to wrap the given model in /// an <see cref="RazorDynamicObject"/> instance and wraps it. /// </summary> /// <param name="modelType">the model-type</param> /// <param name="original">the original model</param> /// <param name="allowMissing">true when we should allow missing properties on dynamic models.</param> /// <returns>the original model or an wrapper object.</returns> internal static object GetDynamicModel(Type modelType, object original, bool allowMissing) { object result = original; if (modelType == null && original != null) { // We try to make some things work: if (CompilerServicesUtility.IsAnonymousTypeRecursive(original.GetType())) { // TODO: we should handle Configuration.AllowMissingPropertiesOnDynamic result = RazorDynamicObject.Create(original, allowMissing); } else if (allowMissing) { result = RazorDynamicObject.Create(original, allowMissing); } } return(result); }
private Tuple <object, Type> CheckModel(object model) { if (model == null) { return(Tuple.Create((object)null, (Type)null)); } Type modelType = (model == null) ? typeof(object) : model.GetType(); bool isAnon = CompilerServicesUtility.IsAnonymousTypeRecursive(modelType); if (isAnon || CompilerServicesUtility.IsDynamicType(modelType)) { modelType = null; if (isAnon || Configuration.AllowMissingPropertiesOnDynamic) { model = RazorDynamicObject.Create(model, Configuration.AllowMissingPropertiesOnDynamic); } } return(Tuple.Create(model, modelType)); }