. Net core custom feature operations,. netcore custom features

Source: Internet
Author: User

. Net core custom feature operations,. netcore custom features

Some changes have been made to the feature operations since the previous classes were transplanted.

Directly look at the code, create a table and field feature class, add a user table, and set the features.

1 using System; 2 3 namespace TestDemo 4 {5 /// <summary> 6 /// table entity features 7 /// </summary> 8 [AttributeUsage (AttributeTargets. class, Inherited = false)] 9 public class TableAttribute: attribute10 {11 /// <summary> 12 // database table name 13 /// </summary> 14 public string TableName {get; set ;} 15 /// <summary> 16 /// database table comment 17 /// </summary> 18 public string Description {get; set;} 19 20 public TableAttribute () 21 {22 TableName = string. empty; 23 Description = string. empty; 24} 25} 26}
Table features
1 using System; 2 3 namespace TestDemo 4 {5 /// <summary> 6 /// column features 7 /// </summary> 8 [AttributeUsage (AttributeTargets. property, AllowMultiple = false)] 9 public class tablecolumnattriple: attribute10 {11 /// <summary> 12 // column name 13 /// </summary> 14 public string ColumnName {get; set ;} 15 /// <summary> 16 /// field Description 17 /// </summary> 18 public string Description {get; set ;} 19 /// <summary> 20 /// whether the primary key is 21 /// </summary> 22 public bool IsPrimaryKey {get; set ;} 23 /// <summary> 24 /// whether the primary key automatically increases by 25 /// </summary> 26 public bool IsPrimaryKeyAuto {get; set ;} 27 /// <summary> 28 // database data type 29 /// </summary> 30 public string DbDataType {get; set ;} 31 /// <summary> 32 /// maximum String Length 33 /// </summary> 34 public int MaxLength {get; set ;} 35 /// <summary> 36 /// cannot be empty 37 // </summary> 38 public bool NotNull {get; set;} 39 40 public tablecolumnattriull () 41 {42 ColumnName = string. empty; 43 Description = string. empty; 44 IsPrimaryKey = false; 45 IsPrimaryKeyAuto = false; 46 DbDataType = "varchar"; 47 MaxLength = 50; 48 NotNull = false; 49} 50} 51}
Table Field Features
1 namespace TestDemo 2 {3 [Table (Description = "USER Table", TableName = "User")] 4 public class USER 5 {6 [TableColumn (ColumnName = "ID ", dbDataType = "int", Description = "primary key", IsPrimaryKey = true, IsPrimaryKeyAuto = true, MaxLength = 0, NotNull = true)] 7 public int Id {get; set ;} 8 9 [TableColumn (ColumnName = "LOGINNO", DbDataType = "varchar", Description = "Login Name", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = 20, NotNull = true)] 10 public string LoginNo {get; set;} 11 12 [TableColumn (ColumnName = "USERNAME", DbDataType = "varchar", Description = "User Name", IsPrimaryKey = false, isPrimaryKeyAuto = false, MaxLength = 20, NotNull = false)] 13 public string UserName {get; set;} 14 15 [TableColumn (ColumnName = "NICKNAME", DbDataType = "varchar ", description = "NickName", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = 30, NotNull = false)] 16 public string NickName {get; set ;} 17 18 [TableColumn (ColumnName = "TEL", DbDataType = "varchar", Description = "TEL", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = 11, NotNull = false)] 19 public string Tel {get; set;} 20} 21}
User table

Obtain the features of user tables and table fields.

1 using System; 2 using System. reflection; 3 using System. text; 4 5 namespace TestDemo 6 {7 public class Program 8 {9 public static void Main (string [] args) 10 {11 Encoding. registerProvider (CodePagesEncodingProvider. instance); 12 13 TableAttribute table = GetTableAttribute <User> (); 14 Console. writeLine ("User (TableName:" + table. tableName + "\ r \ n, Description:" + table. description + ")"); 15 Console. wr IteLine (); 16 17 TableColumnAttribute colId = GetTableColumnAttribute <User> ("Id"); 18 Console. writeLine ("Id (ColumnName:" + colId. columnName 19 + "\ r \ n, DbDataType:" + colId. dbDataType20 + "\ r \ n, Description:" + colId. description21 + "\ r \ n, IsPrimaryKey:" + colId. isPrimaryKey22 + "\ r \ n, IsPrimaryKeyAuto:" + colId. isPrimaryKeyAuto23 + "\ r \ n, MaxLength:" + colId. maxLength24 + "\ r \ n, NotNull:" + colId. notNull + ")"); 25 Console. writeLine (); 26 27 TableColumnAttribute colName = GetTableColumnAttribute <User> ("UserName"); 28 Console. writeLine ("UserName (ColumnName:" + colName. columnName29 + "\ r \ n, DbDataType:" + colName. dbDataType30 + "\ r \ n, Description:" + colName. description31 + "\ r \ n, IsPrimaryKey:" + colName. isPrimaryKey32 + "\ r \ n, IsPrimaryKeyAuto:" + colName. isPrimaryKeyAuto33 + "\ r \ n, MaxLength:" + colName. MaxLength34 + "\ r \ n, NotNull:" + colName. notNull + ")"); 35 36 Console. readLine (); 37} 38 39 // <summary> 40 // obtain table features 41 // </summary> 42 // <typeparam name = "T"> </typeparam> 43 // <returns> </returns> 44 public static TableAttribute GetTableAttribute <T> () 45 {46 Type t = typeof (T); 47 TableAttribute m = t. getTypeInfo (). getCustomAttribute <TableAttribute> (); 48 return m; 49} 50 51 // <summary> 52 // get the column feature 53 // </summary> 54 // <typeparam name = "T"> </typeparam> 55 // <param name = "propertyName"> </param> 56 /// <returns> </returns> 57 public static TableColumnAttribute GetTableColumnAttribute <T> (string propertyName) 58 {59 TableColumnAttribute m = null; 60 61 Type t = typeof (T); 62 PropertyInfo [] arryProperty = t. getProperties (); 63 if (arryProperty! = Null) 64 {65 foreach (PropertyInfo p in arryProperty) 66 {67 if (p. name = propertyName) 68 {69 m = p. getCustomAttribute <TableColumnAttribute> (); 70} 71} 72} 73 74 return m; 75} 76} 77}
Program

Run the command to check the information!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.