/// <summary>
 /// Checks if the given type is a anonymous type or a generic type containing a
 /// reference type as generic type argument
 /// </summary>
 /// <param name="t">the type to check</param>
 /// <returns>true when there exists a reference to an anonymous type.</returns>
 public static bool IsAnonymousTypeRecursive(Type t)
 {
     return(t != null && (CompilerServicesUtility.IsAnonymousType(t) ||
                          // part of generic
                          t.GetGenericArguments().Any(arg => IsAnonymousTypeRecursive(arg)) ||
                          // Array is special
                          (t.IsArray && IsAnonymousTypeRecursive(t.GetElementType()))));
 }
示例#2
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (binder == null)
            {
                throw new ArgumentNullException("binder");
            }

            var dynamicObject = Model as RazorDynamicObject;

            if (dynamicObject != null)
            {
                return(dynamicObject.TryGetMember(binder, out result));
            }

            Type modelType = Model.GetType();
            var  prop      = modelType.GetProperty(binder.Name);

            if (prop == null)
            {
                if (!AllowMissingPropertiesOnDynamic)
                {
                    result = null;
                    return(false);
                }

                result = new RazorDynamicObject()
                {
                    AllowMissingPropertiesOnDynamic = AllowMissingPropertiesOnDynamic, Model = new object()
                };
                return(true);
            }

            object value = prop.GetValue(Model, null);

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

            Type valueType = value.GetType();

            result = (CompilerServicesUtility.IsAnonymousType(valueType))
                         ? new RazorDynamicObject {
                Model = value
            }
                         : value;
            return(true);
        }
示例#3
0
        public CodeCompileUnit GetCodeCompileUnit(string className, string template, ISet <string> namespaceImports, Type templateType, Type modelType)
        {
            if (string.IsNullOrEmpty(className))
            {
                throw new ArgumentException("Class name is required.");
            }

            if (string.IsNullOrEmpty(template))
            {
                throw new ArgumentException("Template is required.");
            }

            namespaceImports = namespaceImports ?? new HashSet <string>();
            templateType     = templateType ?? ((modelType == null) ? typeof(TemplateBase) : typeof(TemplateBase <>));

            // Create the RazorEngineHost
            var host = CreateHost(templateType, modelType, className);

            // Add any required namespace imports
            foreach (string ns in GetNamespaces(templateType, namespaceImports))
            {
                host.NamespaceImports.Add(ns);
            }

            // Gets the generator result.
            GeneratorResults result = GetGeneratorResult(host, template);

            // Add the dynamic model attribute if the type is an anonymous type.
            var type = result.GeneratedCode.Namespaces[0].Types[0];

            if (modelType != null && CompilerServicesUtility.IsAnonymousType(modelType))
            {
                type.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(HasDynamicModelAttribute))));
            }

            // Generate any constructors required by the base template type.
            GenerateConstructors(CompilerServicesUtility.GetConstructors(templateType), type);

            // Despatch any inspectors
            Inspect(result.GeneratedCode);

            return(result.GeneratedCode);
        }