Exemplo n.º 1
0
        private static Type FindModelType(Block block, Type passedModelType)
        {
            var modelFinder = new ModelFinder();

            block.Accept(modelFinder);

            if (string.IsNullOrWhiteSpace(modelFinder.ModelTypeName))
            {
                return(passedModelType ?? typeof(object));
            }

            Type modelType;

            if (passedModelType != null)
            {
                modelType = passedModelType;
                while (modelType != null)
                {
                    if (modelType.FullName == modelFinder.ModelTypeName || modelType.Name == modelFinder.ModelTypeName)
                    {
                        return(modelType);
                    }

                    modelType = modelType.BaseType;
                }

                throw new NotSupportedException(string.Format("Unable to discover CLR Type for model by the name of {0}.  Ensure that the model passed to the view is assignable to the model declared in the view.", modelFinder.ModelTypeName));
            }

            modelType = Type.GetType(modelFinder.ModelTypeName);

            if (modelType != null)
            {
                return(modelType);
            }

            modelType = AppDomainAssemblyTypeScanner.Types.Where(t => t.FullName == modelFinder.ModelTypeName).FirstOrDefault();

            if (modelType != null)
            {
                return(modelType);
            }

            modelType = AppDomainAssemblyTypeScanner.Types.Where(t => t.Name == modelFinder.ModelTypeName).FirstOrDefault();

            if (modelType != null)
            {
                return(modelType);
            }

            throw new NotSupportedException(string.Format("Unable to discover CLR Type for model by the name of {0}. Try using a fully qualified type name and ensure that the assembly is added to the configuration file.", modelFinder.ModelTypeName));
        }
Exemplo n.º 2
0
        private static Type FindModelType(Block block, Type passedModelType)
        {
            var modelFinder = new ModelFinder();
            block.Accept(modelFinder);

            if (string.IsNullOrWhiteSpace(modelFinder.ModelTypeName))
            {
                return passedModelType ?? typeof(object);
            }

            Type modelType;

            if (passedModelType != null)
            {
                modelType = passedModelType;
                while (modelType != null)
                {
                    if (modelType.FullName == modelFinder.ModelTypeName || modelType.Name == modelFinder.ModelTypeName)
                    {
                        return modelType;
                    }

                    modelType = modelType.BaseType;
                }

                throw new NotSupportedException(string.Format("Unable to discover CLR Type for model by the name of {0}.  Ensure that the model passed to the view is assignable to the model declared in the view.", modelFinder.ModelTypeName));
            }

            modelType = Type.GetType(modelFinder.ModelTypeName);

            if (modelType != null)
            {
                return modelType;
            }

            modelType = AppDomainAssemblyTypeScanner.Types.Where(t => t.FullName == modelFinder.ModelTypeName).FirstOrDefault();

            if (modelType != null)
            {
                return modelType;
            }

            modelType = AppDomainAssemblyTypeScanner.Types.Where(t => t.Name == modelFinder.ModelTypeName).FirstOrDefault();

            if (modelType != null)
            {
                return modelType;
            }

            throw new NotSupportedException(string.Format("Unable to discover CLR Type for model by the name of {0}. Try using a fully qualified type name and ensure that the assembly is added to the configuration file.", modelFinder.ModelTypeName));
        }