Ejemplo n.º 1
0
        /// <summary>
        /// 根据一个实体的所有属性,找到符合特性条件的所有列
        /// </summary>
        /// <param name="properites"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        protected virtual List <string> GetColumnsByAttribute(PropertyInfo[] properites, ColumnAttribute type)
        {
            List <string> columnList = new List <string>();

            foreach (var item in properites)
            {
                //获取当前属性中所定义的特性,然后再次遍历这个特性
                var attributes = item.GetCustomAttributes(false);
                foreach (var attribute in attributes)
                {
                    //判断自定义的特性和我们要找的特性是不是一致
                    if (attribute.GetType().Name.Equals(type.ToString()))
                    {
                        columnList.Add(item.Name);//将找到的列名称添加到集合
                        break;
                    }
                }
            }
            return(columnList);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            Console.WriteLine("Your model assembly name:");
            var assemblyName = Console.ReadLine();

            var asm     = Assembly.Load(assemblyName);
            var classes = asm.GetTypes().Where(p => p.IsClass).ToList();

            foreach (var item in classes)
            {
                                // Just grabbing this to get hold of the type name:
                                var type = item.GetType();

                                // Get the PropertyInfo object:
                                var properties = item.GetProperties();

                if (HasEFDataAnnotaion(properties))
                {
                    Console.WriteLine("");
                    Console.WriteLine("Found Data Annotations attributes at {0} ...", item.FullName);
                    foreach (var property in properties)
                    {
                        var attributes = property.GetCustomAttributes(false);

                        // Using reflection.
                        Attribute[] attrs = System.Attribute.GetCustomAttributes(property);

                        // Displaying output.
                        foreach (Attribute attr in attrs)
                        {
                            if (attr is KeyAttribute)
                            {
                                KeyAttribute a = (KeyAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is ForeignKeyAttribute)
                            {
                                ForeignKeyAttribute a = (ForeignKeyAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is IndexAttribute)
                            {
                                IndexAttribute a = (IndexAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.GetType().FullName + a.ToString(), property.Name);
                            }

                            if (attr is RequiredAttribute)
                            {
                                RequiredAttribute a = (RequiredAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is TimestampAttribute)
                            {
                                TimestampAttribute a = (TimestampAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is ConcurrencyCheckAttribute)
                            {
                                ConcurrencyCheckAttribute a = (ConcurrencyCheckAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is MinLengthAttribute)
                            {
                                MinLengthAttribute a = (MinLengthAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is MaxLengthAttribute)
                            {
                                MaxLengthAttribute a = (MaxLengthAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is StringLengthAttribute)
                            {
                                StringLengthAttribute a = (StringLengthAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is TableAttribute)
                            {
                                TableAttribute a = (TableAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is ColumnAttribute)
                            {
                                ColumnAttribute a = (ColumnAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is DatabaseGeneratedAttribute)
                            {
                                DatabaseGeneratedAttribute a = (DatabaseGeneratedAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }

                            if (attr is ComplexTypeAttribute)
                            {
                                ComplexTypeAttribute a = (ComplexTypeAttribute)attr;
                                Console.WriteLine("attribute {0} on {1} ", a.ToString(), property.Name);
                            }
                        }
                    }
                }
            }
        }