示例#1
0
        private void AddModelDefinition(Type classToDef)
        {
            //When the class is an array redefine the classToDef as the array type
            if (classToDef.IsArray)
            {
                classToDef = classToDef.GetElementType();
            }
            //If the class has not been mapped then map into metadata
            if (!models.Any(c => c.Name.Equals(classToDef.Name)))
            {

                ModelDefinition model = new ModelDefinition();

                var properties = classToDef.GetProperties();

                model.Name = classToDef.Name;

                model.Properties = from property in properties
                                   select new ModelProperty
                                   {
                                       Name = property.Name,
                                       Type = ParseType(property.PropertyType)
                                   };

                models.Add(model);
            }
        }
        private void AddModelDefinition(Type classToDef)
        {
            var documentationProvider = config.Services.GetDocumentationProvider();
            //When the class is an array redefine the classToDef as the array type
            if (classToDef.IsArray)
            {
                classToDef = classToDef.GetElementType();
            }
            // Is is not a .NET Framework generic, then add to the models collection.
            if (classToDef.Namespace.StartsWith("System", StringComparison.OrdinalIgnoreCase))
            {
                AddTypeToIgnore(classToDef.Name);
                return;
            }
            //If the class has not been mapped then map into metadata
            if (!typesToIgnore.Contains(classToDef.Name))
            {
                ModelDefinition model = new ModelDefinition();
                model.Name = classToDef.Name;
                model.Description = GetDescription(classToDef);
                if (classToDef.IsGenericType)
                {
                    model.Name = GetGenericTypeDefineRepresentation(classToDef.GetGenericTypeDefinition());
                }
                model.Type = classToDef.IsEnum ? "enum" : "class";
                var constants = classToDef
                    .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
                    .Where(f => f.IsLiteral && !f.IsInitOnly)
                    .ToList();
                model.Constants = from constant in constants
                                  select new ConstantDefinition
                                  {
                                      Name = constant.Name,
                                      Type = ParseType(constant.FieldType),
                                      Value = GetConstantValue(constant),
                                      Description = GetDescription(constant)
                                  };

                var properties = classToDef.IsGenericType
                                     ? classToDef.GetGenericTypeDefinition().GetProperties()
                                     : classToDef.GetProperties();
            
                model.Properties = from property in properties
                                   select new ModelProperty
                                          {
                                              Name = property.Name,
                                              Type = ParseType(property.PropertyType, model),
                                              Description = GetDescription(property)
                                          };
                AddTypeToIgnore(model.Name);
                foreach (var p in properties)
                {
                    var type = p.PropertyType;

                    if (!models.Any(c => c.Name.Equals(type.Name)))// && !type.IsInterface)
                    {
                        ParseType(type);
                    }
                }
                models.Add(model);
            }
        }
        private string ParseType(Type type, ModelDefinition model = null)
        {
            string res;

            if (type == null)
                return "";

            // If the type is a generic type format to correct class name.
            if (type.IsGenericType)
            {
                res = GetGenericRepresentation(type, (t) => ParseType(t, model), model);

                AddModelDefinition(type);
            }
            else
            {
                if (type.ToString().StartsWith("System."))
                {
                    if (type.ToString().Equals("System.Void"))
                        res = "void";
                    else
                        res = type.Name;
                }
                else
                {
                    res = type.Name;

                    if (!type.IsGenericParameter)
                    {
                        AddModelDefinition(type);
                    }
                }
            }

            return res;
        }
        private string GetGenericRepresentation(Type type, Func<Type, string> getTypedParameterRepresentation, ModelDefinition model = null)
        {
            string res = type.Name;
            int index = res.IndexOf('`');
            if (index > -1)
                res = res.Substring(0, index);

            Type[] args = type.GetGenericArguments();

            res += "<";

            for (int i = 0; i < args.Length; i++)
            {
                if (i > 0)
                    res += ", ";
                //Recursivly find nested arguments

                var arg = args[i];
                if (model != null && model.IsGenericArgument(arg.Name))
                {
                    res += model.GetGenericArgument(arg.Name);
                }
                else
                {
                    res += getTypedParameterRepresentation(arg);
                }
            }
            res += ">";
            return res;
        }