Exemplo n.º 1
0
        public static List <MetaTableInfo> GetMetaTableInfoList(string className)
        {
            var list = new List <MetaTableInfo>();

            Type[] types = Assembly.LoadFrom(SourceAssembly).GetTypes();
            foreach (var type in types)
            {
                if (type.Name.Equals(className))
                {
                    foreach (PropertyInfo properties in type.GetProperties())
                    {
                        var metaTableInfo = new MetaTableInfo();
                        try
                        {
                            XmlElement documentation = DocsByReflection.XMLFromMember(type.GetProperty(properties.Name));
                            metaTableInfo.Annotation = documentation["summary"].InnerText.Trim();
                        }
                        catch
                        {
                            metaTableInfo.Annotation = "";
                        }
                        metaTableInfo.Name = properties.Name;
                        if (properties.PropertyType == typeof(int))
                        {
                            metaTableInfo.PropertyType = "int";
                        }
                        else if (properties.PropertyType == typeof(int?))
                        {
                            metaTableInfo.PropertyType = "int?";
                        }
                        else if (properties.PropertyType == typeof(long))
                        {
                            metaTableInfo.PropertyType = "long";
                        }
                        else if (properties.PropertyType == typeof(long?))
                        {
                            metaTableInfo.PropertyType = "long?";
                        }
                        else if (properties.PropertyType == typeof(DateTime?))
                        {
                            metaTableInfo.PropertyType = "DateTime?";
                        }
                        else if (properties.PropertyType == typeof(decimal))
                        {
                            metaTableInfo.PropertyType = "decimal";
                        }
                        else if (properties.PropertyType == typeof(decimal?))
                        {
                            metaTableInfo.PropertyType = "decimal?";
                        }
                        else if (properties.PropertyType == typeof(string))
                        {
                            metaTableInfo.PropertyType = "string";
                        }
                        else if (properties.PropertyType == typeof(bool))
                        {
                            metaTableInfo.PropertyType = "bool";
                        }
                        else
                        {
                            metaTableInfo.PropertyType = properties.PropertyType.ToString().Split('.').Last().Replace("]", "");
                        }
                        list.Add(metaTableInfo);
                    }
                }
            }

            return(list);
        }
        /// <summary>
        /// 根据类名 反射得到类的信息
        /// </summary>
        /// <param name="className">类名</param>
        /// <returns></returns>
        public static List <MetaTableInfo> GetMetaTableInfoListForAssembly(string className)
        {
            var list = new List <MetaTableInfo>();

            Type[] types = Assembly.LoadFrom(Configuration.SourceAssembly).GetTypes();
            foreach (var type in types)
            {
                if (type.Name.Equals(className))
                {
                    var classAnnotation = string.Empty;
                    try
                    {
                        //获取类的注释
                        XmlElement xmlFromType = DocsByReflection.XMLFromType(type.GetTypeInfo());
                        classAnnotation = xmlFromType["summary"].InnerText.Trim();
                    }
                    catch
                    {
                    }

                    foreach (PropertyInfo properties in type.GetProperties())
                    {
                        var metaTableInfo = new MetaTableInfo();
                        try
                        {
                            //XmlElement documentation = DocsByReflection.XMLFromMember(type.GetProperty(properties.Name));
                            //metaTableInfo.Annotation = documentation["summary"].InnerText.Trim();

                            var customAttributes = properties.GetCustomAttributes();
                            var attr             = new List <string>();
                            if (customAttributes != null)
                            {
                                foreach (var attribute in customAttributes)
                                {
                                    switch (attribute.GetType().Name)
                                    {
                                    case "StringLengthAttribute":
                                    {
                                        attr.Add($"{attribute.GetType().Name}({className}.{properties.Name}MaxLength)");
                                    }
                                    break;

                                    default:
                                    {
                                        attr.Add(attribute.GetType().Name);
                                        break;
                                    }
                                    }
                                }
                                metaTableInfo.Annotation += "[" + String.Join("]\n[", attr.ToArray()) + "]";
                            }
                            metaTableInfo.Annotation      = metaTableInfo.Annotation.Replace("Attribute", "").Replace("[]", "");
                            metaTableInfo.ClassAnnotation = classAnnotation;
                        }
                        catch
                        {
                            metaTableInfo.Annotation = "";
                        }
                        metaTableInfo.Name         = properties.Name;
                        metaTableInfo.PropertyInfo = properties;
                        if (properties.PropertyType == typeof(int))
                        {
                            metaTableInfo.PropertyType = "int";
                        }
                        else if (properties.PropertyType == typeof(int?))
                        {
                            metaTableInfo.PropertyType = "int?";
                        }
                        else if (properties.PropertyType == typeof(long))
                        {
                            metaTableInfo.PropertyType = "long";
                        }
                        else if (properties.PropertyType == typeof(long?))
                        {
                            metaTableInfo.PropertyType = "long?";
                        }
                        else if (properties.PropertyType == typeof(DateTime?))
                        {
                            metaTableInfo.PropertyType = "DateTime?";
                        }
                        else if (properties.PropertyType == typeof(float))
                        {
                            metaTableInfo.PropertyType = "float";
                        }
                        else if (properties.PropertyType == typeof(float?))
                        {
                            metaTableInfo.PropertyType = "float?";
                        }
                        else if (properties.PropertyType == typeof(decimal))
                        {
                            metaTableInfo.PropertyType = "decimal";
                        }
                        else if (properties.PropertyType == typeof(decimal?))
                        {
                            metaTableInfo.PropertyType = "decimal?";
                        }
                        else if (properties.PropertyType == typeof(string))
                        {
                            metaTableInfo.PropertyType = "string";
                        }
                        else if (properties.PropertyType == typeof(bool))
                        {
                            metaTableInfo.PropertyType = "bool";
                        }
                        else
                        {
                            metaTableInfo.PropertyType = properties.PropertyType.ToString().Split('.').Last().Replace("]", "").Replace("+", ".");
                        }
                        list.Add(metaTableInfo);
                    }
                }
            }

            return(list);
        }
Exemplo n.º 3
0
        /// <summary>
        /// </summary>
        /// <param name="className">类名</param>
        /// <returns></returns>
        public static List <ClassInfoType> GetMetaTableInfoListForAssembly()
        {
            var resultList = new List <ClassInfoType>();

            var types = Assembly.LoadFrom(Configuration.SourceAssembly).GetTypes().Where(s =>
                                                                                         s.Namespace != null &&
                                                                                         s.Namespace.StartsWith(Configuration.EntityNamespace) &&
                                                                                         s.IsClass &&
                                                                                         (s.IsSubclassOf(typeof(Entity)) ||
                                                                                          s.BaseType.IsGenericType &&
                                                                                          s.BaseType.GetGenericTypeDefinition() == typeof(Entity <>))).ToArray();

            foreach (var type in types)
            {
                var classAnnotation = string.Empty;
                try
                {
                    //获取类的注释
                    XmlElement xmlFromType = DocsByReflection.XMLFromType(type.GetTypeInfo());
                    classAnnotation = xmlFromType["summary"].InnerText.Trim();
                }
                catch (Exception ex)
                {
                }
                var list = new List <MetaTableInfo>();
                foreach (PropertyInfo properties in type.GetProperties())
                {
                    var metaTableInfo = new MetaTableInfo();
                    try
                    {
                        XmlElement documentation = DocsByReflection.XMLFromMember(type.GetProperty(properties.Name));
                        metaTableInfo.Annotation = documentation["summary"].InnerText.Trim();

                        metaTableInfo.ClassAnnotation = classAnnotation;
                    }
                    catch
                    {
                        metaTableInfo.Annotation = "";
                    }
                    metaTableInfo.FieldProp = properties.PropertyType;
                    metaTableInfo.Name      = properties.Name;
                    if (properties.PropertyType == typeof(int))
                    {
                        metaTableInfo.PropertyType = "int";
                    }
                    else if (properties.PropertyType == typeof(int?))
                    {
                        metaTableInfo.PropertyType = "int?";
                    }
                    else if (properties.PropertyType == typeof(long))
                    {
                        metaTableInfo.PropertyType = "long";
                    }
                    else if (properties.PropertyType == typeof(long?))
                    {
                        metaTableInfo.PropertyType = "long?";
                    }
                    else if (properties.PropertyType == typeof(DateTime?))
                    {
                        metaTableInfo.PropertyType = "DateTime?";
                    }
                    else if (properties.PropertyType == typeof(decimal))
                    {
                        metaTableInfo.PropertyType = "decimal";
                    }
                    else if (properties.PropertyType == typeof(decimal?))
                    {
                        metaTableInfo.PropertyType = "decimal?";
                    }
                    else if (properties.PropertyType == typeof(string))
                    {
                        metaTableInfo.PropertyType = "string";
                    }
                    else if (properties.PropertyType == typeof(bool))
                    {
                        metaTableInfo.PropertyType = "bool";
                    }
                    else
                    {
                        metaTableInfo.PropertyType = properties.PropertyType.ToString().Split('.').Last().Replace("]", "");
                    }
                    list.Add(metaTableInfo);
                }
                resultList.Add(new ClassInfoType()
                {
                    ClassName      = type.Name,
                    MetaTableInfos = list,
                    ObjType        = type
                });
            }

            return(resultList);
        }