. NET using custom class properties

Source: Internet
Author: User

In. NET you can use Type.GetCustomAttributes to get custom properties on a class, and you can use Propertyinfo.getcustomattributes to get custom properties on the property information.

The following is a simple database table mapping entity class to illustrate the relevant usage, based on custom class properties and custom attributes of the properties in the class, you can easily make class tags and properties in the class tag

Create a custom property of a class that identifies the table name in the database and needs to inherit from the attribute class:

[AttributeUsage (AttributeTargets.Class, inherited = False, AllowMultiple = False)]
public sealed class Tableattribute:attribute
{
Private readonly string _tablename = "";
Public Tableattribute (String tableName)
{
This._tablename = TableName;
}
public string TableName
{
get {return this._tablename;}
}
}

Create a custom property of a property that identifies the name of the field in the database table and needs to inherit from the attribute class

[AttributeUsage (Attributetargets.property, inherited = False, AllowMultiple = False)]
public class Fieldattribute:attribute
{
Private readonly string _fieldname = ""; Field name of the database

Private System.Data.DbType _type = System.Data.DbType.String; field type of the database

Public Fieldattribute (String fieldName)

{

This._fieldname=fieldname;

}

Public Fieldattribute (string fieldname,system.data.dbtype type)

{

This._fieldname=fieldname;

This._type=type;

}

public string FieldName
{
get {return this._fieldname;}
}

Public System.Data.DbType Type

{

Get{return This._type;}

}

}

Create a data entity base class:

public class BaseEntity
{
Public BaseEntity ()
{
}

<summary>
Get table name
</summary>
<returns></returns>
public string Gettablename ()
{
Type type = this. GetType ();
object[] Objs = type. GetCustomAttributes (typeof (Tableattribute), true);
if (OBJS. Length <= 0)
{
throw new Exception ("entity class does not identify Tableattribute attribute");
}
Else
{
Object obj = Objs[0];
Tableattribute ta = (tableattribute) obj;
Return TA.                            TableName; Get table name
}
}
<summary>
Get Fieldattribute on a data entity class
</summary>
<param name= "PropertyName" ></param>
<returns></returns>
Public Fieldattribute Getfieldattribute (String propertyname)
{
PropertyInfo field = this. GetType (). GetProperty (PropertyName);
if (field = = null)
{
throw new Exception ("attribute name" + propertyname + "does not exist");
}
object[] objs = field. GetCustomAttributes (typeof (Fieldattribute), true);
if (OBJS. Length <= 0)
{
throw New Exception ("Class body attribute name" + propertyname + "No identity fieldattribute attribute");
}
Else
{
Object obj = Objs[0];
Fieldattribute fieldattribute= (fieldattribute) obj;
Fieldattribute.fieldvalue=field. GetValue (This,null);
return fieldattribute;
}
}

}

Create a data entity

[Table ("Wincms_dictionary")]///wincms_dictionary table mapped to database
public class Wincms_dictionary:baseentity
{

private int _dictionaryid;

Public Wincms_dictionary ()

{

}

[Field ("Dictionaryid", Dbtype.int32)]///fields in the Wincms_dictionary table mapped to the database
public int Dictionaryid
{
get {return This._dictionaryid;}
Set
{
This._dictionaryid = value;
}
}

}

Get the table name and field name for an entity based on the entity class

public class Test

{

public static void Main (string[] args)

{

Wincms_dictionary dict=new wincms_dictionary ();

Console.WriteLine ("Table name:" +gettablename (Dict));

Console.WriteLine ("Field Name:" +getfieldname (Dict, "Dictionaryid"));

Console.read ();

}

Get Entity Table name

public static string Gettablename (BaseEntity entity)

{

return entity. Gettablename ();

}

Get entity Field name

public static string GetFieldName (BaseEntity entity,string PropertyName)

{

Fieldattribute fieldattribute=entity. Getfieldattribute (PropertyName);

return fieldattribute.fieldname;

}

}

The output is:

Table Name: Wincms_dictionary

Field Name: Dictionaryid

. NET using custom class properties

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.